You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
from __future__ importannotationsfromtypingimport*T=TypeVar('T')
classParent(Sequence[T]):
'''a sequence type which is implemented one of two ways, according to the input parameters '''def__new__(cls, model: Sequence[T]) ->Parent[T]:
ifclsisParent:
iflen(model) %2:
returnOdds(model)
returnEvens(model)
returnsuper().__new__(cls)
_data: Sequence[T]
@overloaddef__getitem__(self, index: int, /) ->T: ...
@overloaddef__getitem__(self, index: slice, /) ->Sequence[T]: ...
def__getitem__(self, index: Union[int, slice], /
) ->Union[Sequence[T], T]: # pragma: no coverreturnself._data[index]
def__len__(self) ->int:
returnlen(self._data)
classEvens(Parent[T]):
'''the implementation of 'Parent' used when the input has an even length '''def__new__(cls, model: Sequence[T]) ->Evens[T]:
ret=super().__new__(cls, model)
# mypy: error: Argument 1 to "__new__" of "Parent" has# incompatible type "Type[Evens[T]]";# expected "Type[Parent[T]]"# [arg-type]# error: Argument 2 to "__new__" of "Parent" has# incompatible type "Sequence[T]"; expected# "Sequence[T]"# [arg-type]assertisinstance(ret, Evens)
returnretdef__init__(self, model: Sequence[T]):
self._data=model[::2]
classOdds(Parent[T]):
'''the implementation of 'Parent' used when the input has an odd length '''def__new__(cls, model: Sequence[T]) ->Odds[T]:
# mypy: two (3) errors, analogous to those of# 'Evens.__new__()', aboveret=super().__new__(cls, model)
assertisinstance(ret, Odds)
returnretdef__init__(self, model: Sequence[T]):
self._data=model[1::2]
deftest_function() ->None:
# this test passes; the code does work!evens=Parent(list(range(10)))
assertisinstance(evens, Parent)
assertisinstance(evens, Evens)
assertlist(evens) == [0, 2, 4, 6, 8]
odds=Parent(list(range(11)))
assertisinstance(odds, Parent)
assertisinstance(odds, Odds)
assertlist(odds) == [1, 3, 5, 7, 9]
To Reproduce
See above.
Actual Behavior
main.py:40: error: Argument 1 to "__new__" of "Parent" has incompatible type "Type[Evens[T]]"; expected "Type[Parent[T]]" [arg-type]
main.py:40: error: Argument 2 to "__new__" of "Parent" has incompatible type "Sequence[T]"; expected "Sequence[T]" [arg-type]
main.py:61: error: Argument 1 to "__new__" of "Parent" has incompatible type "Type[Odds[T]]"; expected "Type[Parent[T]]" [arg-type]
main.py:61: error: Argument 2 to "__new__" of "Parent" has incompatible type "Sequence[T]"; expected "Sequence[T]" [arg-type]
Found 4 errors in 1 file (checked 1 source file)
Expected Behavior
There should be no errors. (In fact, this worked correctly until mypy 0.950.)
The first and third errors make little sense since Type[Evens[T]] is a special case of Type[Parent[T]].
The second and fourth errors make even less sense, since Sequence[T] is exactly the same as Sequence[T].
If there is actually some kind of problem here, the error messages need to be much more helpful than they currently are.
Note that this is specific to __new__, other (explicit) class methods work correctly. I didn't check, but it may be caused by #12590 cc @JukkaL FWIW PR looks correct, probably it just exposes some old missing special casing for __new__. Likely should be an easy fix.
commit 1dd8e7fe6
Author: Kouroche Bouchiat <[email protected]>
Date: Sun Dec 17 21:32:57 2023 +0100
Substitute type variables in return type of static methods (#16670)
`add_class_tvars` correctly instantiates type variables in the return
type for class methods but not for static methods. Check if the analyzed
member is a static method in `analyze_class_attribute_access` and
substitute the type variable in the return type in `add_class_tvars`
accordingly.
Fixes #16668.
Bug Report
Consider the following code [mypy-play.net]:
To Reproduce
See above.
Actual Behavior
Expected Behavior
There should be no errors. (In fact, this worked correctly until mypy 0.950.)
The first and third errors make little sense since
Type[Evens[T]]
is a special case ofType[Parent[T]]
.The second and fourth errors make even less sense, since
Sequence[T]
is exactly the same asSequence[T]
.If there is actually some kind of problem here, the error messages need to be much more helpful than they currently are.
Your Environment
mypy.ini
(and other config files): N/AThe text was updated successfully, but these errors were encountered: