Skip to content

Widen return type of pyplot.subplot_tool to object | None#31997

Open
karlhillx wants to merge 6 commits into
matplotlib:mainfrom
karlhillx:fix-subplot-tool-return-type
Open

Widen return type of pyplot.subplot_tool to object | None#31997
karlhillx wants to merge 6 commits into
matplotlib:mainfrom
karlhillx:fix-subplot-tool-return-type

Conversation

@karlhillx

@karlhillx karlhillx commented Jul 4, 2026

Copy link
Copy Markdown

Widen the return annotation of pyplot.subplot_tool and NavigationToolbar2.configure_subplots from SubplotTool | None to object | None. The Qt backends return a QDialog and the toolmanager branch already returns None, so the annotations were misleading. Direction per timhoffm on #31979. Docstring updated to describe the backend-dependent return value.

Also drop the now-unused TYPE_CHECKING import of SubplotTool, and alphabetize the typing import.

Closes #31979

…re_subplots to Any

Per timhoffm on matplotlib#31979. The Qt backends return QDialog subclasses and
the toolmanager branch already returns None, so the prior annotation
was misleading. Update docstring to describe the backend-dependent
return value. Also drop the now-unused TYPE_CHECKING import of
SubplotTool and alphabetize the typing import.

Closes matplotlib#31979
Copilot AI review requested due to automatic review settings July 4, 2026 21:45
@github-actions

github-actions Bot commented Jul 4, 2026

Copy link
Copy Markdown

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a week or so, please leave a new comment below and that should bring it to our attention. Most of our reviewers are volunteers and sometimes things fall through the cracks. We also ask that you please finish addressing any review comments on this PR and wait for it to be merged (or closed) before opening a new one, as it can be a valuable learning experience to go through the review process.

You can also join us on discourse chat for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide.
Please let us know if (and how) you use AI, it will help us give you better feedback on your PR.

We strive to be a welcoming and open project. Please follow our Code of Conduct.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR updates Matplotlib’s typing/doc surface to reflect that pyplot.subplot_tool() / NavigationToolbar2.configure_subplots() return backend-dependent objects (e.g., a SubplotTool on classic backends vs a native dialog on Qt), rather than always returning a SubplotTool.

Changes:

  • Widened the return annotation of pyplot.subplot_tool to a backend-agnostic type.
  • Widened the return annotation of NavigationToolbar2.configure_subplots in the type stubs.
  • Updated the subplot_tool docstring to describe backend-dependent return values.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
lib/matplotlib/pyplot.py Adjusts subplot_tool() return annotation and updates its docstring to describe backend-dependent returns.
lib/matplotlib/backend_bases.pyi Updates the stubbed return type of NavigationToolbar2.configure_subplots.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/matplotlib/pyplot.py Outdated
Comment thread lib/matplotlib/pyplot.py Outdated
Comment thread lib/matplotlib/backend_bases.pyi Outdated
karlhillx and others added 2 commits July 4, 2026 17:51
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Comment thread lib/matplotlib/pyplot.py Outdated
Address review feedback: keep the public return annotation as object | None
(safer than Any, which disables type checking for callers) and match the
configure_subplots stub to object. Rewrite the Returns description to cover
backends without a native dialog (e.g. macosx) as an implementation-specific
value. Drop the unnecessary top-level Any import move.
@karlhillx karlhillx force-pushed the fix-subplot-tool-return-type branch from 5c8ee32 to e1e96ec Compare July 6, 2026 01:18
Comment thread lib/matplotlib/backend_bases.pyi Outdated

@eeshsaxena eeshsaxena left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The change itself looks right - I confirmed that after removing the TYPE_CHECKING import, SubplotTool has no remaining references in pyplot.py (the only three occurrences were the import, this annotation, and the docstring line, all updated here), and there's no pyplot.pyi that also annotates subplot_tool, so nothing is left dangling. The docstring rewrite describing the backend-dependent return is a nice improvement.

Two small things:

  • Title vs. code: the PR title and description say the return type is widened to Any, but the code actually uses object (object | None in pyplot.py, object in backend_bases.pyi). Those aren't equivalent - Any opts out of type checking, while object is the restrictive top type - so it's worth aligning the title with what landed (which, given "Direction per timhoffm on #31979", looks like object is the intended choice). Just so the merge-commit message isn't misleading.

  • object | None is redundant: since NoneType is a subclass of object, object | None normalizes to just object for a type checker. backend_bases.pyi already uses plain object for configure_subplots, so subplot_tool could match that for consistency - though object | None does document the None case more explicitly, so this is minor either way.

Otherwise this looks good.

The .pyi stub now matches pyplot.py's object | None annotation,
documenting the None case explicitly per the maintainer's request.
@karlhillx karlhillx changed the title Widen return type of pyplot.subplot_tool to Any Widen return type of pyplot.subplot_tool to object | None Jul 9, 2026
@karlhillx

Copy link
Copy Markdown
Author

Thanks @eeshsaxena — both points addressed:

Title/description: Updated. The title and body now say object | None instead of Any, matching what landed.

object | None redundancy: Fair point that it normalizes to object for a type checker. I kept object | None per @timhoffm's suggestion on the .pyi stub (comment) — it documents the None case more explicitly for readers. Both pyplot.py and backend_bases.pyi now use object | None consistently.

@eeshsaxena

Copy link
Copy Markdown
Contributor

Thanks for the quick turnaround. Both look resolved to me:

  • Title/description now say object | None, matching what landed.
  • Keeping object | None per @timhoffm's call on the stub is fine - and I see configure_subplots in backend_bases.pyi was updated to object | None too, so the two stubs are consistent with each other now.

Looks good from my side - deferring to the maintainers for the final review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: pyplot.subplot_tool doesn't always return a SubplotTool

6 participants