Rely on typeshed/stubs for slice typing by ilevkivskyi · Pull Request #21401 · python/mypy · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions mypy/checkexpr.py
1 change: 0 additions & 1 deletion mypy/message_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ def with_additional_msg(self, info: str) -> ErrorMessage:
TOO_MANY_TARGETS_FOR_VARIADIC_UNPACK: Final = ErrorMessage(
"Too many assignment targets for variadic unpack"
)
INVALID_SLICE_INDEX: Final = ErrorMessage("Slice index must be an integer, SupportsIndex or None")
CANNOT_INFER_LAMBDA_TYPE: Final = ErrorMessage("Cannot infer type of lambda")
CANNOT_ACCESS_INIT: Final = (
'Accessing "__init__" on an instance is unsound, since instance.__init__ could be from'
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-class-namedtuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ class Base(NamedTuple):
# E: No overload variant of "__getitem__" of "tuple" matches argument type "TypeVar" \
# N: Possible overload variants: \
# N: def __getitem__(self, int, /) -> int \
# N: def __getitem__(self, slice, /) -> tuple[int, ...]
# N: def __getitem__(self, slice[int | None], /) -> tuple[int, ...]
return self.x
def bad_override(self) -> int:
return self.x
Expand Down
8 changes: 4 additions & 4 deletions test-data/unit/check-expressions.test
Original file line number Diff line number Diff line change
Expand Up @@ -1226,10 +1226,10 @@ o[:] # E: Value of type "object" is not indexable
from typing import Any
a: Any
o: object
a[o:1] # E: Slice index must be an integer, SupportsIndex or None
a[1:o] # E: Slice index must be an integer, SupportsIndex or None
a[o:] # E: Slice index must be an integer, SupportsIndex or None
a[:o] # E: Slice index must be an integer, SupportsIndex or None
a[o:1]
a[1:o]
a[o:]
a[:o]
[builtins fixtures/slice.pyi]

[case testSliceSupportsIndex]
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-narrowing.test
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ weird_mixture: Union[KeyedTypedDict, KeyedNamedTuple]
if weird_mixture["key"] is Key.B: # E: No overload variant of "__getitem__" of "tuple" matches argument type "str" \
# N: Possible overload variants: \
# N: def __getitem__(self, int, /) -> Literal[Key.C] \
# N: def __getitem__(self, slice, /) -> tuple[Literal[Key.C], ...]
# N: def __getitem__(self, slice[int | None], /) -> tuple[Literal[Key.C], ...]
reveal_type(weird_mixture) # N: Revealed type is "TypedDict('__main__.KeyedTypedDict', {'key': Literal[__main__.Key.B]}) | tuple[Literal[__main__.Key.C], fallback=__main__.KeyedNamedTuple]"
else:
reveal_type(weird_mixture) # N: Revealed type is "TypedDict('__main__.KeyedTypedDict', {'key': Literal[__main__.Key.B]}) | tuple[Literal[__main__.Key.C], fallback=__main__.KeyedNamedTuple]"
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-tuples.test
Original file line number Diff line number Diff line change
Expand Up @@ -1435,7 +1435,7 @@ reveal_type(t[x]) # N: Revealed type is "builtins.int | builtins.str"
t[y] # E: No overload variant of "__getitem__" of "tuple" matches argument type "str" \
# N: Possible overload variants: \
# N: def __getitem__(self, int, /) -> int | str \
# N: def __getitem__(self, slice, /) -> tuple[int | str, ...]
# N: def __getitem__(self, slice[int | None], /) -> tuple[int | str, ...]

[builtins fixtures/tuple.pyi]

Expand All @@ -1444,7 +1444,7 @@ t = (0, "")
x = 0
y = ""
reveal_type(t[x:]) # N: Revealed type is "builtins.tuple[builtins.int | builtins.str, ...]"
t[y:] # E: Slice index must be an integer, SupportsIndex or None
t[y:] # E: Invalid index type "slice[str, None, None]" for "tuple[int, str]"; expected type "slice[int | None]"
[builtins fixtures/tuple.pyi]

[case testTupleSliceStepZeroNoCrash]
Expand Down
10 changes: 7 additions & 3 deletions test-data/unit/fixtures/slice.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Builtins stub used in slicing test cases.
from typing import Generic, TypeVar
from typing import Generic, TypeVar, Protocol
T = TypeVar('T')
_Tco = TypeVar('_Tco', covariant=True)

class SupportsIndex(Protocol):
def __index__(self) -> int: ...

class object:
def __init__(self): pass
Expand All @@ -12,8 +16,8 @@ class function: pass
class int: pass
class str: pass

class slice: pass
class slice(Generic[_Tco]): pass
class ellipsis: pass
class dict: pass
class list(Generic[T]):
def __getitem__(self, x: slice) -> list[T]: pass
def __getitem__(self, x: slice[SupportsIndex | None]) -> list[T]: pass
7 changes: 4 additions & 3 deletions test-data/unit/fixtures/tuple.pyi
Loading