-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Generic class with constrained type vars #5416
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
Comments
Apart from the fact that using a generic class type variable in class methods is typically a bad idea (although not formally wrong), I think the first and last examples are bugs. Both are results of how mypy checks functions with constrained type variables. Also I think @elazarg might help with the first one if he has time. |
Thanks for responding so quickly! I'll
In my actual code, the class method |
This is probably fine. However I would anyway try refactoring the code (if possible) to make |
My intention with |
It looks like you are too worried :-) I said typically and if possible. In your situation it is perfectly OK. |
I just tested the examples in the original post using mypy 0.630. The first example no longer generates errors. The fourth and last examples continue to generate errors. Maybe related to fixing #5309? |
Hi there ! Still facing the last issue (7 years later lmao) with mypy 1.10.1 and python 3.11.9. Here's my code and mypy output : Content of from collections.abc import Iterable
from typing import TypeVar
from src.domain.models.key_word import KeyWord
from src.domain.models.regex import Regex
# KeyWord and Regex are 2 classes implementing interfaces
T = TypeVar("T", KeyWord, Regex)
class SearchableStringItemList(list[T]):
def __init__(self, iterable: Iterable[T]) -> None:
super().__init__(iterable)
# other instance methods not pasted here
# ... Content of from collections.abc import Iterable
from src.domain.models.key_word import KeyWord
from src.domain.models.searchable_string_item_list import SearchableStringItemList
class KeyWordList(SearchableStringItemList[KeyWord]):
def __init__(self, iterable: Iterable[KeyWord]) -> None:
if not all(isinstance(item, KeyWord) for item in iterable):
raise TypeError(
"Cannot create a KeyWordList from an iterable if it contains other types "
f"than KeyWord. Received types : { {type(item) for item in iterable} }"
)
super().__init__(iterable)
# other instance methods not pasted here
# ... Content of from collections.abc import Iterable
from src.domain.models.key_word import KeyWord
from src.domain.models.searchable_string_item_list import SearchableStringItemList
class KeyWordList(SearchableStringItemList[KeyWord]):
def __init__(self, iterable: Iterable[KeyWord]) -> None:
if not all(isinstance(item, KeyWord) for item in iterable):
raise TypeError(
"Cannot create a KeyWordList from an iterable if it contains other types "
f"than KeyWord. Received types : { {type(item) for item in iterable} }"
)
super().__init__(iterable) mypy output : src\domain\models\searchable_string_item_list.py: note: In member "__init__" of class "SearchableStringItemList":
src\domain\models\searchable_string_item_list.py:21:26: error: Argument 1 to "__init__" of "list" has incompatible type "Iterable[KeyWord]"; expected "Iterable[T]" [arg-type]
super().__init__(iterable)
^~~~~~~~
src\domain\models\searchable_string_item_list.py:21:26: error: Argument 1 to "__init__" of "list" has incompatible type "Iterable[Regex]"; expected "Iterable[T]" [arg-type]
super().__init__(iterable)
^~~~~~~~
Found 2 errors in 1 file As mentioned above, adding a type ignore comment removes this mypy error and my code works perfectly fine : Modified content of class SearchableStringItemList(list[T]):
def __init__(self, iterable: Iterable[T]) -> None:
super().__init__(iterable) # type:ignore [arg-type]
# other instance methods not pasted here
# ... If you have any idea of a better solution than adding a type ignore, I'd love to hear it ! Thanks 🙏 |
Uh oh!
There was an error while loading. Please reload this page.
On Python 3.7.0 and mypy 0.620, I'm getting some error messages I don't understand. The Mypy documentation and PEP 484 didn't clear it up for me. Maybe it's a bug in Mypy. Maybe I don't understand type erasure. In case it's the former, here's a minimal example. In case it's the latter, please help.
In the following examples, the fact that
m
does not return is not critical. In the real code that gave me these errors,m
is an abstract method, which also doesn't change anything important.Put the following examples in
mypy-test.py
to get the corresponding output from runningmypyt mypy-test.py
. The first and the last examples are the ones that are baffling me.classmethod
with constrained type variableMypy output:
classmethod
with generic selfproduces no error.
classmethod
with unconstrained type variableproduces no error.
instance method with constrained type
produces
instance method with unconstrained type
produces no error.
instance method with constrained type and inheritance
produces
The text was updated successfully, but these errors were encountered: