8000 [Bug/TYP]: misc type errors · Issue #26858 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content
[Bug/TYP]: misc type errors #26858
Open
Open
@twoertwein

Description

@twoertwein

Bug summary

Pandas uses mypy/pyright to check its internal code. We currently pin matplotlib <3.8 to avoid many type errors pandas-dev/pandas#55210 Many of these errors are definitely on pandas's side :) but a few might be bugs in matplotlib.

Code for reproduction

# Simplified examples from pandas that trigger type errors

def format_date_labels(ax: Axes, rot) -> None:
    for label in ax.get_xticklabels():
        label.set_ha("right") # error: "Text" has no attribute "set_ha"  [attr-defined]


def test(cellText: np.ndarray):
    # error: Argument "cellText" to "table" has incompatible type "ndarray[Any, Any]"; expected "Sequence[Sequence[str]] | None"  [arg-type]
    # maybe use a Protocol such as Iterable?
    matplotlib.table.table(ax, cellText=cellText **kwargs)


def decorate_axes(ax: Axes, freq, kwargs) -> None:
    # The following might just be bad pandas code
    # error: "Axes" has no attribute "freq"  [attr-defined]
    ax.freq = freq
    xaxis = ax.get_xaxis()
    xaxis.freq = freq
    if not hasattr(ax, "legendlabels"):
        ax.legendlabels = [kwargs.get("label", None)]
    else:
        ax.legendlabels.append(kwargs.get("label", None))

# error: Argument 1 to "FixedLocator" has incompatible type "ndarray[Any, Any]"; expected "Sequence[float]"  [arg-type]
FixedLocator(ax.get_xticks()) 

# same also for set_ylabel, maybe use a protocol for str-coercible types instead of requiring str
def test(ax: Axes, label: Hashable): # Hashable might be too wide, but str seems to strict
    ax.set_xlabel(label)

def test(ax: Axes):
    axes = np.array([ax])
    # error: Argument 1 to "set_ticks_props" has incompatible type "ndarray[Any, dtype[Any]]"; expected "Axes | Sequence[Axes]"  [arg-type]
    set_ticks_props(axes, **kwargs)

Actual outcome

the above type errors

Expected outcome

Fewer errors, but still some errors - some of the pandas code is definitely to be blamed!

Additional information

No response

Operating system

No response

Matplotlib Version

3.8

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

None

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

      0