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
importtypingastseq1: t.Sequence[str] = ("Hello", "World!")
try:
seq1.count(x="Hello")
exceptTypeErroraserror:
print("Raises a `TypeError` anyway:", error)
VALUES: t.List[int] = [3, 4, 3, 5]
classMySequence(t.Sequence[int]):
@t.overloaddef__getitem__(self, index: int) ->int:
pass@t.overloaddef__getitem__(self, index: slice) ->t.List[int]:
passdef__getitem__(self, index: t.Union[int, slice]) ->t.Union[int, t.List[int]]:
returnVALUES[index]
def__len__(self) ->int:
returnlen(VALUES)
seq2: t.Sequence[int] =MySequence()
try:
seq2.count(x=3)
exceptTypeErroraserror:
print("Raises a `TypeError` anyway:", error)
# does not raise a `TypeError` but does not type checkprint(seq2.count(value=3))
The first two calls to count do type check but raise an error at runtime. The errors, however, are quite different: in the first case, count does not accept any keyword arguments as in case of a tuple the arguments are positional only. In the second case, count does accept keyword arguments but not x because the name of the argument is value in the ABC Sequence (which is in itself inconsistent, strictly speaking a tuple does not implement the Sequence ABC as it does not allow keyword arguments). Finally, the last call to count succeeds because it is valid according to the ABC but it does not type check.
The behavior I expect is as follows: with regard to the Sequence type, count should accept no keyword arguments. If no arguments are given using keywords, then tuple and the ABC are consistent.
I stumbled across this while trying to implement a protocol for hashable sequences. This turned out impossible because if I declare precisely the interface of the ABC then I cannot assign tuples to a variable having that type.
If you point me into a direction of where I should look to fix this, I might be able to send a pull request.
Many methods in the typing ABCs and protocols should perhaps have positional-only args. This would have to changed in typeshed (do you want to make a typeshed PR?), so I'm closing this issue here.
Making Sequence a protocol is a mostly unrelated matter, and would require CPython changes (and I'm not convinced that it's desirable, but again, this is not the best place to discuss it).
Have a look at the following code:
The first two calls to
count
do type check but raise an error at runtime. The errors, however, are quite different: in the first case,count
does not accept any keyword arguments as in case of atuple
the arguments are positional only. In the second case,count
does accept keyword arguments but notx
because the name of the argument isvalue
in the ABCSequence
(which is in itself inconsistent, strictly speaking a tuple does not implement theSequence
ABC as it does not allow keyword arguments). Finally, the last call tocount
succeeds because it is valid according to the ABC but it does not type check.The behavior I expect is as follows: with regard to the Sequence type,
count
should accept no keyword arguments. If no arguments are given using keywords, thentuple
and the ABC are consistent.I stumbled across this while trying to implement a protocol for hashable sequences. This turned out impossible because if I declare precisely the interface of the ABC then I cannot assign tuples to a variable having that type.
If you point me into a direction of where I should look to fix this, I might be able to send a pull request.
Python Version: 3.8.0
Mypy Version: 0.761
I use the following mypy configuration:
The text was updated successfully, but these errors were encountered: