crates/ruff_linter/resources/mdtest/flake8-pyi/redundant-numeric-union.md
redundant-numeric-union (PYI041)target-version = "py311"
[lint]
select = ["PYI041"]
Numeric unions are redundant when they are used only for static typing.
def function(value: int | float) -> None: ... # error: [redundant-numeric-union]
The first annotated parameter determines the concrete types registered at runtime, so its numeric union is not redundant.
import functools
@functools.singledispatch
def dispatch(value: object) -> None: ...
@dispatch.register
def _(value: int | float) -> None: ...
The generic function's annotation does not register concrete types, so its numeric union remains redundant even when a registered implementation needs the same union.
import functools
@functools.singledispatch
def dispatch(value: int | float) -> None: ... # error: [redundant-numeric-union]
@dispatch.register
def _(value: int | float) -> None: ...
Numeric unions remain redundant for parameters that do not determine dispatch registration.
import functools
@functools.singledispatch
def dispatch(value: object) -> None: ...
@dispatch.register
def _(value: float | complex, other: int | float) -> None: ... # snapshot: redundant-numeric-union
error[PYI041]: Use `float` instead of `int | float`
--> src/mdtest_snippet.py:7:38
|
7 | def _(value: float | complex, other: int | float) -> None: ... # snapshot: redundant-numeric-union
| ^^^^^^^^^^^
help: Remove redundant type
|
6 | @dispatch.register
- def _(value: float | complex, other: int | float) -> None: ... # snapshot: redundant-numeric-union
7 + def _(value: float | complex, other: float) -> None: ... # snapshot: redundant-numeric-union
|
The dispatch parameter comes after the unannotated instance parameter.
import functools
class Dispatch:
@functools.singledispatchmethod
def dispatch(self, value: object) -> None: ...
@dispatch.register
def _(self, value: int | float) -> None: ...
An explicit registration does not inspect the implementation's parameter annotation.
import functools
@functools.singledispatch
def dispatch(value: object) -> None: ...
@dispatch.register(int | float)
def _(value: int | float) -> None: ... # error: [redundant-numeric-union]
Registered dispatch implementations retain their numeric unions in stub files as well.
import functools
@functools.singledispatch
def dispatch(value: object) -> None: ...
@dispatch.register
def _(value: int | float) -> None: ...