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

No error for singledispatch on generic types #11875

Open
wrobell opened this issue Dec 30, 2021 · 3 comments
Open

No error for singledispatch on generic types #11875

wrobell opened this issue Dec 30, 2021 · 3 comments
Labels
bug mypy got something wrong topic-type-form TypeForm might fix this

Comments

@wrobell
Copy link

wrobell commented Dec 30, 2021

Please consider

from functools import singledispatch
import typing as tp

@singledispatch
def f(x: tp.Any) -> None: pass

@f.register
def _f_list(x: list[int]) -> None: pass

if __name__ == '__main__':
    f([1, 2, 3])

Then

$ mypy sp.py
Success: no issues found in 1 source file

# on import no error as well, so Mypy raising an error would be very useful
$ python -c 'import sp'

On runtime

$ python sp.py
Traceback (most recent call last):
  File "/usr/lib/python3.10/functools.py", line 831, in dispatch
    impl = dispatch_cache[cls]
  File "/usr/lib/python3.10/weakref.py", line 416, in __getitem__
    return self.data[ref(key)]
KeyError: <weakref at 0x7f390d605c60; to 'type' at 0x7f390e07bd00 (list)>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.10/functools.py", line 834, in dispatch
    impl = registry[cls]
KeyError: <class 'list'>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/wrobell/projects/rbfly/sp.py", line 11, in <module>
    f([1, 2, 3])
  File "/usr/lib/python3.10/functools.py", line 878, in wrapper
    return dispatch(args[0].__class__)(*args, **kw)
  File "/usr/lib/python3.10/functools.py", line 836, in dispatch
    impl = _find_impl(cls, registry)
  File "/usr/lib/python3.10/functools.py", line 783, in _find_impl
    mro = _compose_mro(cls, registry.keys())
  File "/usr/lib/python3.10/functools.py", line 744, in _compose_mro
    types = [n for n in types if is_related(n)]
  File "/usr/lib/python3.10/functools.py", line 744, in <listcomp>
    types = [n for n in types if is_related(n)]
  File "/usr/lib/python3.10/functools.py", line 743, in is_related
    and issubclass(cls, typ))
TypeError: issubclass() argument 2 cannot be a parameterized generic

Some context (in general we need to be better at distinguishing between Python types and annotation types)

@AlexWaygood
Copy link
Member

^Note that the runtime error message has been recently improved by python/cpython#30050, which will appear in the next micro versions of 3.9, 3.10 and 3.11.

The topic of whether to ever support generic aliases in singledispatch was also recently discussed, and rejected, in a third BPO issue here: https://bugs.python.org/issue46191

@AlexWaygood
Copy link
Member

AlexWaygood commented Dec 30, 2021

Also, note that three pseudo-types will be supported by singledispatch in 3.11: types.UnionType, typing.Union, and typing.Optional: https://bugs.python.org/issue46014

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Dec 30, 2021

If we wanted to add more special casing for singledispatch, could also add some disallow-any-generic special casing (see https://bugs.python.org/issue46191 )

Edit: Oh I see Alex already linked that issue. But yeah, another potential mypy "feature"

@AlexWaygood AlexWaygood added the topic-type-form TypeForm might fix this label Apr 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-type-form TypeForm might fix this
Projects
None yet
Development

No branches or pull requests

3 participants