Skip to content
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

False positive TCH003 for singledispatchmethod #13924

Closed
last-partizan opened this issue Oct 25, 2024 · 2 comments · Fixed by #13941
Closed

False positive TCH003 for singledispatchmethod #13924

last-partizan opened this issue Oct 25, 2024 · 2 comments · Fixed by #13941
Labels
bug Something isn't working

Comments

@last-partizan
Copy link

This is almost the same as #11520 , but for @singledispatchmethod

from __future__ import annotations

from collections.abc import MutableMapping
from functools import singledispatchmethod
from typing import Any


class Foo:
    @singledispatchmethod
    def foo(self, x: Any) -> int:
        raise NotImplementedError

    @foo.register
    def _(self, x: MutableMapping) -> int:
        return 0
> ruff check --select TCH app/test.py

app/test.py:3:29: TCH003 Move standard library import `collections.abc.MutableMapping` into a type-checking block
  |
1 | from __future__ import annotations
2 |
3 | from collections.abc import MutableMapping
  |                             ^^^^^^^^^^^^^^ TCH003
4 | from functools import singledispatchmethod
5 | from typing import Any
  |
  = help: Move into type-checking block

Found 1 error.
No fixes available (1 hidden fix can be enabled with the `--unsafe-fixes` option).
> python --version
Python 3.12.7
> ruff --version
ruff 0.7.1
@MichaReiser
Copy link
Member

I don't think I understand this issue. Could you explain me why MutableMapping can't be moved into a type checking block?

@last-partizan
Copy link
Author

Because @simgledispatchmethod is using runtime types, just like normal @singledispatch from #11520

Example:

> python /tmp/test.py
Traceback (most recent call last):
  File "/tmp/test.py", line 10, in <module>
    class Foo:
  File "/tmp/test.py", line 15, in Foo
    @foo.register
     ^^^^^^^^^^^^
  File "/usr/lib/python3.12/functools.py", line 939, in register
    return self.dispatcher.register(cls, func=method)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/functools.py", line 877, in register
    argname, cls = next(iter(get_type_hints(func).items()))
                             ^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/typing.py", line 2310, in get_type_hints
    hints[name] = _eval_type(value, globalns, localns, type_params)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/typing.py", line 415, in _eval_type
    return t._evaluate(globalns, localns, type_params, recursive_guard=recursive_guard)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/typing.py", line 947, in _evaluate
    eval(self.__forward_code__, globalns, localns),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 1, in <module>
NameError: name 'MutableMapping' is not defined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants