10000 [Bug]: Type hints for ax argument in scale_factory does not allow None · Issue #29455 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

[Bug]: Type hints for ax argument in scale_factory does not allow None #29455

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Illviljan opened this issue Jan 11, 2025 · 3 comments
Open

Comments

@Illviljan
Copy link
Contributor
Illviljan commented Jan 11, 2025

Bug summary

scale_factory requires Axis according to the type hints:

def scale_factory(scale: str, axis: Axis, **kwargs) -> ScaleBase: ...

But ax is even discouraged in the code:

def __init__(self, axis):
r"""
Construct a new scale.
Notes
-----
The following note is for scale implementers.
For back-compatibility reasons, scales take an `~matplotlib.axis.Axis`
object as first argument. However, this argument should not
be used: a single scale object should be usable by multiple
`~matplotlib.axis.Axis`\es at the same time.
"""

class ScaleBase:
def __init__(self, axis: Axis | None) -> None: ...

Code for reproduction

from matplotlib.scale import scale_factory


scale_factory("linear", None)
# pyright:
# error: Argument of type "None" cannot be assigned to parameter "axis" of type "Axis" in function "scale_factory"
#   "None" is not assignable to "Axis" (reportArgumentType)

# mypy:
# error: Argument 2 to "scale_factory" has incompatible type "None"; expected "Axis"  [arg-type]

Actual outcome

# pyright:
# error: Argument of type "None" cannot be assigned to parameter "axis" of type "Axis" in function "scale_factory"
#   "None" is not assignable to "Axis" (reportArgumentType)

# mypy:
# error: Argument 2 to "scale_factory" has incompatible type "None"; expected "Axis"  [arg-type]

Expected outcome

mypy and pyright passing

Additional information

Using the same type hints as ScaleBase should solve this.

Operating system

No response

Matplotlib Version

3.9.2

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

None

@timhoffm
Copy link
Member
timhoffm commented Jan 11, 2025

Thanks for the report. The type hint is correct for now. An axis parameter must still be passed to all Scales by the caller for backwards compatibility. Maybe the note is not clear. The recommendation is that the scale does not make use of the parameter.

There’s a plan to remove the parameter, but that’ll take a while to make the transition smooth. See #29349.

@Illviljan
Copy link
Contributor Author
Illviljan commented Jan 11, 2025

I wasn't suggesting to remove the parameter, just to add None as well:

def scale_factory(scale: str, axis: Axis | None, **kwargs) -> ScaleBase: ...

@timhoffm
Copy link
Member
timhoffm commented Jan 11, 2025

While discouraged, and not used in Matplotlibs own scales anymore, 3rd party libs can still register scales that make use of this parameter. This would fail if somebody would pass None. It's a judgement call whether we allow None and let this error path slip through the type checker, or whether we still always require the Axis.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
0