-
Notifications
You must be signed in to change notification settings - Fork 3
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
Types that should match the alias definition don't #2
Comments
Edit: My mistake, I thought this was the mypy repo while following links. I think it's a mypy bug. Original comment: I am also having as similar issue, where if I try to assign or return a type e.g. Jsonable = Union[bool, float, int, str, list['Jsonable'], dict[str, 'Jsonable'], None] And I get e.g. this error:
Using mypy 1.3.0 on Python 3.11.3 |
@endrift This is due to type variance on the type arguments to For this to work like you want, the type definitions need to use something with covariant type arguments. If the definition was changed to from typing import Sequence, Union, Mapping
Jsonable = Union[int, str, float, bool, Sequence["Jsonable"], Mapping[str, "Jsonable"]] then this will work x: list[str] = ["hi"]
y: Jsonable = x because |
While I appreciate your help with my obviously off-topic issue, I'm pretty sure at some point I tried that and it only partially alleviated the issue. I don't quite remember because I haven't touched that code in a few months. I do remember stumbling upon a documented mypy limitation at some point though, but that might have been something more complicated. |
True, it could be a limitation of mypy. It will work with pyright for certain. |
The issue from python/mypy#13786 applies to the type alias from this package as well: For something like
Mypy will complain
despite
dict[str, str]
clearly being expected to match the definition of the type alias.In the comments on the aforementioned issue, there is a suggestion to use
typing.Sequence
andtyping.Mapping
instead oflist
anddict
like the error message suggests, which does solve this issue, but comes with issues of its own because one often genuinely only wantsdict
s andlist
s to count as JSON-like, not generic mappings and sequences.No idea what to do (or if anything should be done at all at this point, other than waiting for new features in Mypy or Python's typing), just pointing out that there is an issue here.
The text was updated successfully, but these errors were encountered: