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
{{ message }}
Repository navigation
Sequence type does neither match the ABC nor the implementation of tuple #8345
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).
Activity
JelleZijlstra commentedon Jan 30, 2020
I haven't thought too deeply about this, but it sounds like we should just mark the argument to
Sequence.countas positional-only.koehlma commentedon Jan 30, 2020
Yes, I think that this would be a proper fix.
It would also be great to make
Sequencea proper protocol such thatwould work. Currently this gives me
because the ABC has concrete methods.
JukkaL commentedon Feb 4, 2020
Many methods in the
typingABCs 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
Sequencea 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).