Skip to content

Correct return type annotations for Axes in visualization functions - #6504

Merged
not522 merged 1 commit into
optuna:masterfrom
kvr06-ai:fix/6048-correct-axes-return-types
Mar 13, 2026
Merged

not522 merged 1 commit into
optuna:masterfrom
kvr06-ai:fix/6048-correct-axes-return-types

Conversation

@kvr06-ai

Copy link
Copy Markdown
Contributor

Motivation

Several matplotlib visualization functions (plot_contour, plot_rank, plot_slice) annotate their return type as Axes, but actually return np.ndarray when plt.subplots() is called with multiple rows or columns. This causes false positives with type checkers like mypy.

See discussion: #6028 (comment)

Fixes #6048

Description of the changes

Updated return type annotations from Axes to Axes | np.ndarray in three files:

  • _contour.py: plot_contour() and _get_contour_plot() return np.ndarray when n_params > 2
  • _rank.py: plot_rank() and _get_rank_plot() return np.ndarray when n_params >= 3
  • _slice.py: plot_slice() and _get_slice_plot() return np.ndarray when len(info.subplots) > 1

Also updated the docstring Returns section in each public function to document both possible return types.

When plt.subplots() is called with multiple rows/columns, it returns
np.ndarray instead of Axes. Update plot_contour, plot_rank, and
plot_slice to annotate the return type as Axes | np.ndarray.

Fixes #6048
@kvr06-ai

Copy link
Copy Markdown
Contributor Author

@not522 Would you mind reviewing when you get a chance? This updates the return type annotations for plot_contour, plot_rank, and plot_slice per #6048.

@c-bata

c-bata commented Mar 11, 2026

Copy link
Copy Markdown
Member

@not522 @sawa3030 Could you review this PR if you have time?

@c-bata c-bata added the code-fix Change that does not change the behavior, such as code refactoring. label Mar 11, 2026

@sawa3030 sawa3030 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.

LGTM!

@sawa3030 sawa3030 removed their assignment Mar 13, 2026

@not522 not522 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM!

@not522 not522 changed the title fix: correct return type annotations for Axes in visualization functions Correct return type annotations for Axes in visualization functions Mar 13, 2026
@not522 not522 added this to the v4.9.0 milestone Mar 13, 2026
@not522
not522 merged commit 2393d82 into optuna:master Mar 13, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

code-fix Change that does not change the behavior, such as code refactoring.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Correct return type annotations for Axes in visualization functions

4 participants