Skip to content

[Bug]: revealed type for Figure.subfigures is not correct #30313

Open
@MarcoGorelli

Description

@MarcoGorelli

Bug summary

The overloads for subfigures aren't quite correct

The issue is that

def subfigures(self, nrows=1, ncols=1, squeeze=True,

has squeeze=True as the default, but the first overload incorrectly notes False as the default

@overload
def subfigures(
self,
nrows: int = ...,
ncols: int = ...,
squeeze: Literal[False] = ...,
wspace: float | None = ...,
hspace: float | None = ...,
width_ratios: ArrayLike | None = ...,
height_ratios: ArrayLike | None = ...,
**kwargs
) -> np.ndarray: ...

Code for reproduction

import matplotlib.pyplot as plt
from typing import reveal_type

fig = plt.figure(layout='constrained', figsize=(10, 4))

reveal_type(fig.subfigures(1, 2, wspace=0.07))
reveal_type(fig.subfigures(1, 1, wspace=0.07))

reveal_type(fig.subfigures(1, 2, wspace=0.07, squeeze=True))
reveal_type(fig.subfigures(1, 1, wspace=0.07, squeeze=True))

Actual outcome

(running the above with mypy)

t.py:6: note: Revealed type is "numpy.ndarray[builtins.tuple[Any, ...], numpy.dtype[Any]]"
t.py:7: note: Revealed type is "numpy.ndarray[builtins.tuple[Any, ...], numpy.dtype[Any]]"
t.py:9: note: Revealed type is "Union[numpy.ndarray[builtins.tuple[Any, ...], numpy.dtype[Any]], matplotlib.figure.SubFigure]"
t.py:10: note: Revealed type is "Union[numpy.ndarray[builtins.tuple[Any, ...], numpy.dtype[Any]], matplotlib.figure.SubFigure]"

Note that the reveal types differ, even though squeeze=True is the default

Expected outcome

t.py:6: note: Revealed type is "Union[numpy.ndarray[builtins.tuple[Any, ...], numpy.dtype[Any]], matplotlib.figure.SubFigure]"
t.py:7: note: Revealed type is "Union[numpy.ndarray[builtins.tuple[Any, ...], numpy.dtype[Any]], matplotlib.figure.SubFigure]"
t.py:9: note: Revealed type is "Union[numpy.ndarray[builtins.tuple[Any, ...], numpy.dtype[Any]], matplotlib.figure.SubFigure]"
t.py:10: note: Revealed type is "Union[numpy.ndarray[builtins.tuple[Any, ...], numpy.dtype[Any]], matplotlib.figure.SubFigure]"

Additional information

No response

Operating system

No response

Matplotlib Version

3.10.3

Matplotlib Backend

No response

Python version

3.12

Jupyter version

No response

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions