GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Figure: Refactor how plotting methods are attached to the Figure class by seisman · Pull Request #4612 · GenericMappingTools/pygmt

Conversation

@seisman

@seisman seisman commented Apr 29, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Motivation

In PR #3849, we're seeing many static type check errors (https://github.com/GenericMappingTools/pygmt/actions/runs/25118862756/job/73613589767?pr=3849), mainly because in pygmtlogo, we're trying to re-import the Figure class, which causes some circular imports.

Codex recommends a different way to attach the plotting wrappers to the Figure class, which can solve the static type error found in PR #3849. Changes in this PR are made by Codex.

Summary

This PR refactors how plotting methods are attached to pygmt.Figure and clarifies the role of pygmt.src.

Why

Previously, Figure methods were attached through a class-scoped from pygmt.src import (...) block in pygmt/figure.py. That worked at runtime, but it had a few drawbacks:

  • It depended on package-level re-exports from pygmt/src/__init__.py just to populate Figure.
  • Static analysis could misinterpret some imported names as modules instead of callables. (see the mypy errors in found https://github.com/GenericMappingTools/pygmt/actions/runs/25118862756/job/73613589767?pr=3849)
  • The class-scoped import block was harder to read and maintain than explicit assignments.
  • It blurred two separate concepts:
    • standalone functions that are part of the functional API
    • plotting functions that are intended to be used as methods of Figure

What changed

pygmt/figure.py

  • Import each plotting callable directly from its concrete pygmt.src.* module.
  • Attach these callables explicitly on the Figure class body, e.g. plot = _plot.
  • Add a short comment above the attachment block to clarify the pattern.

pygmt/src/__init__.py

  • Keep only standalone functions that are intended to be called directly.
  • Stop re-exporting Figure-bound plotting methods from pygmt.src.
  • Add a short comment explaining the distinction between standalone functions and Figure methods.

Why this is better

This makes the API structure clearer:

  • pygmt.src becomes a cleaner namespace for standalone operations.
  • Figure is the clear home for plotting methods.

It also improves maintainability:

  • Each attached Figure method points to one explicit source module.
  • The attachment logic is easier to scan and modify.
  • Figure no longer depends on package-level re-export behavior for method attachment.
  • The structure is friendlier to static analysis and code review.

Behavioral intent

This is intended as an internal API-structure cleanup, not a plotting behavior change. The plotting implementations themselves are unchanged; only the way they are attached/exported has been reorganized.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@seisman
Refactor Figure method attachments
9e33485
@seisman seisman changed the title Refactor Figure method attachments Figure: Refactor how plotting methods are attached to the Figure class Apr 29, 2026
@seisman

seisman commented Apr 29, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

This PR is backward-compatible, but it's a big low-level refactor. Ping @GenericMappingTools/pygmt-maintainers for reviews.

Edit: Previously, it was possible to do from pygmt.src import plot, but it makes no sense to call plot as as standalone function. After this PR, from pygmt.src import plot no longer works.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@seisman seisman added the maintenance Boring but important stuff for the core devs label Apr 29, 2026
@seisman seisman added this to the 0.19.0 milestone Apr 29, 2026
@seisman
seisman requested a review from a team April 29, 2026 17:53
@seisman seisman added the needs review This PR has higher priority and needs review. label Apr 29, 2026
@seisman
Rename text_ to text
d74da28
@seisman

seisman commented Apr 30, 2026

Copy link
Copy Markdown
Member Author

This PR also renames the text_ method to text in commit d74da28. The name text_ was initially introduced to suppress a warning mentioned in #808 (comment).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

yvonnefroehlich
yvonnefroehlich reviewed Apr 30, 2026
View reviewed changes
Comment thread pygmt/src/__init__.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

weiji14
weiji14 reviewed Apr 30, 2026
View reviewed changes
Comment thread pygmt/figure.py
wiggle,
)
# Attach plotting functions implemented in pygmt.src as Figure methods.
basemap = _basemap

@weiji14 weiji14 Apr 30, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't we just directly do

Suggested change
basemap = _basemap
from pygmt.src.basemap import basemap

So that the _basemap is not needed?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@weiji14 weiji14 May 1, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok, if we did this, we would still need to handle:

error: Unsupported class scoped import  [misc]
PLC0415 `import` should be at the top-level of a file

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
seisman and others added 2 commits May 1, 2026 09:50
@seisman @yvonnefroehlich
Fix docstring
62d15ee
Co-authored-by: Yvonne Fröhlich <[email protected]>
@seisman
Merge branch 'main' into figure/class
2153a5c
weiji14
weiji14 reviewed May 1, 2026
View reviewed changes
Comment thread pygmt/figure.py Outdated
Comment on lines +445 to +446
# Attach plotting functions implemented in pygmt.src as Figure methods.
basemap = _basemap

@weiji14 weiji14 May 1, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can these go into the __init__ method? Might need to rewrite as self.basemap = _basemap?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@seisman seisman May 1, 2026

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in c5035c9

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@weiji14 weiji14 May 1, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oo, doesn't actually work. Getting TypeError("coast() missing 1 required positional argument: 'self'") 😅

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@weiji14 weiji14 May 1, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe revert that commit. We actually do want the figure methods as class variables, not instance variables (https://docs.python.org/3/tutorial/classes.html#class-and-instance-variables).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
seisman added 2 commits May 1, 2026 10:27
@seisman
Attach plotting methods in the Figure.__init__ method
c5035c9
@seisman
Revert "Attach plotting methods in the Figure.__init__ method"
3343f80
This reverts commit c5035c9.
weiji14
weiji14 approved these changes May 1, 2026
View reviewed changes

@weiji14 weiji14 left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have some ideas using importlib or Mixins (following ideas in https://stackoverflow.com/questions/47561840/how-can-i-separate-the-functions-of-a-class-into-multiple-files), but the diff might be even bigger, so will just settle with this for now. Thanks @seisman.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
seisman
seisman commented May 1, 2026
View reviewed changes
Comment thread pygmt/figure.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@seisman
Fix a typo
cfe6b2c
@seisman
seisman merged commit 8559120 into main May 1, 2026
16 of 18 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@seisman
seisman deleted the figure/class branch May 1, 2026 03:25
@seisman seisman removed the needs review This PR has higher priority and needs review. label May 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@yvonnefroehlich yvonnefroehlich yvonnefroehlich left review comments
@weiji14 weiji14 weiji14 approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

maintenance Boring but important stuff for the core devs

Projects

None yet

Milestone

0.19.0

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@seisman @weiji14 @yvonnefroehlich
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.