8000 [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
Open
@Illviljan

Description

@Illviljan

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0