-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
generic subclasss and typevar with constraint #7362
Comments
I think this is a bug. Most likely this is a bug during generation of function expansion for |
I am not an expert here, but is it really a bug?..
You said:
I think my example fulfills both points On the other hand, running mypy with |
@gantsevdenis This is just wrong, in your example, e.g.,
Sometimes it helps to just read the docs https://mypy.readthedocs.io/en/latest/generics.html#defining-sub-classes-of-generic-classes |
Oh... well thank you for pointing that out, my bad |
@ilevkivskyi Was about to post almost exactly the example pointed out in this issue, apparently this bug's still present in the latest version. Are there any plans on resolving this, or is any help needed? |
We have no plan to fix this in the near future. Any help would be appreciated, though! If you want to fix this, feel free to ask questions here (or in a draft PR). |
I recently hit a similar problem, here's a shorter reproducer. Mypy Playground link from typing import TypeVar
T = TypeVar('T', str, int)
class BlaList(list[T]):
def pop_item(self, key: int) -> T:
return super().pop(key)
# main.py:8: error: Incompatible return value type (got "T", expected "str")
# main.py:8: error: Incompatible return value type (got "T", expected "int") (In case I am mistaken, let me know, I can open a separate issue) |
I found a work-around for my use case, using from typing import TypeVar
T = TypeVar('T', bound=str | int)
class BlaList(list[T]):
def pop_item(self, key: int) -> T:
return super().pop(key) |
@intgr that's a very different thing. We're about to add better documentation of the difference to CPython: https://github.com/python/cpython/pull/31712/files (I should probably merge that PR) |
Thanks, that documentation clears up the distinction for me. Your wording "very different thing" is too strong; in my use case it really doesn't matter, so it's a decent work-around for this bug. But I understand now why mypy needs to treat them differently. |
Not sure if this is a "bug" or I didn't understand generics, but
mypy 0.720 (on windows) says
but if I change the T definition to
T = typing.TypeVar('T')
the errors go awayWhat I want is to specify that
is this a bug? di I misinterpreted the TypeVar usage?
The text was updated successfully, but these errors were encountered: