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
commented
Apr 29, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
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.
seisman
changed the title
Refactor Figure method attachments
Figure: Refactor how plotting methods are attached to the Figure class
Apr 29, 2026
seisman
commented
Apr 29, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
|
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.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
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).
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
|
|
wiggle,
|
|
|
)
|
|
|
# Attach plotting functions implemented in pygmt.src as Figure methods.
|
|
|
basemap = _basemap
|
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
|
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.
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.
Comment on lines
+445
to
+446
|
|
# Attach plotting functions implemented in pygmt.src as Figure methods.
|
|
|
basemap = _basemap
|
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.
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.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
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.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Reviewers
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
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
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.
|