extend Document.__getitem__
type annotation to reflect that the method also accepts slices
#3706
Labels
Document.__getitem__
type annotation to reflect that the method also accepts slices
#3706
Problem
The type annotation of
Document.__getitem__
is wrong:The type annotation of
i
requires it to be anint
, but the implementation is obviously meant to also accept a slice (and works fine with slices)Solution
extend the signature to a Union and add overloads to tell typecheckers which return values correspond to which argument types:
that way it reflects the reality of which argument types the methods actually accepts and which return value types they produce.
Note that the actual implementation remains unchanged besides the extension of the type annotation
Thanks to the overload for slices, typecheckers also correctly recognize, that the result of indexing with a slice is iterable
(Which is not the case if you just add the Unions to the signature, then the typechecker would complain that a
Page
is not iterable)Alternatives
Ignore the problem until it is solved by the complete rework of type hints as mentioned by julian-smith-artifex-com:
but since there is no ETA for the overhaul and since this fix is so quick and straightforward, i believe it would still be worth it to implement it now
compatibility concerns
typing.Union
is already used repeatedly in the moduletyping.overload
has not been used previously, but was introduced alongsidetyping.Union
in python 3.5therefore i don't see any compatibility concerns
The text was updated successfully, but these errors were encountered: