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
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@
fix_instance,
has_any_from_unimported_type,
instantiate_type_alias,
make_optional_type,
set_any_tvars,
validate_instance,
)
Expand Down Expand Up @@ -5860,17 +5859,10 @@ def _super_arg_types(self, e: SuperExpr) -> Type | tuple[Type, Type]:
return type_type, instance_type

def visit_slice_expr(self, e: SliceExpr) -> Type:
try:
supports_index = self.chk.named_type("typing_extensions.SupportsIndex")
except KeyError:
supports_index = self.chk.named_type("builtins.int") # thanks, fixture life
expected = make_optional_type(supports_index)
type_args = []
for index in [e.begin_index, e.end_index, e.stride]:
if index:
t = self.accept(index)
self.chk.check_subtype(t, expected, index, message_registry.INVALID_SLICE_INDEX)
type_args.append(t)
type_args.append(self.accept(index))
else:
type_args.append(NoneType())
return self.chk.named_generic_type("builtins.slice", type_args)
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ class tuple(Sequence[_Tco], Generic[_Tco]):
def __contains__(self, item: object) -> bool: pass
@overload
def __getitem__(self, x: int) -> _Tco: pass
# Real stubs use SupportsIndex, but we use int to speed up tests, as this is a common fixture.
@overload
def __getitem__(self, x: slice) -> Tuple[_Tco, ...]: ...
def __getitem__(self, x: slice[int | None]) -> Tuple[_Tco, ...]: ...
def __mul__(self, n: int) -> Tuple[_Tco, ...]: pass
def __rmul__(self, n: int) -> Tuple[_Tco, ...]: pass
def __add__(self, x: Tuple[_Tco, ...]) -> Tuple[_Tco, ...]: pass
Expand All @@ -37,7 +38,7 @@ class int:
def __neg__(self) -> 'int': pass
def __pos__(self) -> 'int': pass
class float: pass
class slice: pass
class slice(Generic[_Tco]): pass
class bool(int): pass
class str: pass # For convenience
class bytes: pass
Expand All @@ -47,7 +48,7 @@ class list(Sequence[_T], Generic[_T]):
@overload
def __getitem__(self, i: int) -> _T: ...
@overload
def __getitem__(self, s: slice) -> list[_T]: ...
def __getitem__(self, s: slice[int | None]) -> list[_T]: ...
def __contains__(self, item: object) -> bool: ...
def __iter__(self) -> Iterator[_T]: ...

Expand Down
Loading