Skip to content

[flake8-pyi] Avoid false positives on singledispatch functions (PYI041) - #27335

Merged
ntBre merged 2 commits into
mainfrom
brent/pyi041-singledispatch
Jul 31, 2026
Merged

ntBre merged 2 commits into
mainfrom
brent/pyi041-singledispatch

Conversation

@ntBre

@ntBre ntBre commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #27333. The fixed annotation isn't actually equivalent in this case.

>>> import functools
...
... @functools.singledispatch
... def int_or_float(value: object) -> str:
...     return False
...
... @int_or_float.register
... def _(value: int | float):
...     return True
...
... assert int_or_float(3)
...
>>> import functools
...
... @functools.singledispatch
... def int_or_float(value: object) -> str:
...     return False
...
... @int_or_float.register
... def _(value: float):
...     return True
...
... assert int_or_float(3)
...
Traceback (most recent call last):
  File "<python-input-1>", line 11, in <module>
    assert int_or_float(3)
           ~~~~~~~~~~~~^^^
AssertionError

We had an existing helper for testing for singledispatch functions, which we use to skip the first
parameter of such functions since this is the dispatching parameter.

Test Plan

New mdtests

…PYI041`)

Summary
--

Fixes #27333. The fixed annotation isn't actually equivalent in this case.

```pycon
>>> import functools
...
... @functools.singledispatch
... def int_or_float(value: object) -> str:
...     return False
...
... @int_or_float.register
... def _(value: int | float):
...     return True
...
... assert int_or_float(3)
...
>>> import functools
...
... @functools.singledispatch
... def int_or_float(value: object) -> str:
...     return False
...
... @int_or_float.register
... def _(value: int ���at):
...     return True
...
... assert int_or_float(3)
KeyboardInterrupt
>>> import functools
...
... @functools.singledispatch
... def int_or_float(value: object) -> str:
...     return False
...
... @int_or_float.register
... def _(value: float):
...     return True
...
... assert int_or_float(3)
...
Traceback (most recent call last):
  File "<python-input-1>", line 11, in <module>
    assert int_or_float(3)
           ~~~~~~~~~~~~^^^
AssertionError
```

We had an existing helper for testing for `singledispatch` functions, which we use to skip the first
parameter of such functions since this is the dispatching parameter.

Test Plan
--

New mdtests
@ntBre ntBre added the bug An issue describing something that isn't working, or a PR that fixes a bug label Jul 30, 2026
@astral-sh-bot

astral-sh-bot Bot commented Jul 30, 2026

Copy link
Copy Markdown

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@ntBre
ntBre merged commit 31cb63a into main Jul 31, 2026
47 checks passed
@ntBre
ntBre deleted the brent/pyi041-singledispatch branch July 31, 2026 16:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug An issue describing something that isn't working, or a PR that fixes a bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

redundant-numeric-union (PYI041) --fix incorrect fix for functools.singledispatch registered function

2 participants