Skip to content

Commit 115bf00

Browse files
cleanup
1 parent 6cbb237 commit 115bf00

File tree

6 files changed

+50
-58
lines changed

6 files changed

+50
-58
lines changed

mypy/argmap.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@
3434

3535
IterableType = NewType("IterableType", Instance)
3636
"""Represents an instance of `Iterable[T]`."""
37-
TupleInstance = NewType("TupleInstance", Instance)
38-
"""Represents an instance of `builtins.tuple[T, ...]`."""
3937

4038

4139
def map_actuals_to_formals(
@@ -284,16 +282,6 @@ def is_iterable(self, typ: Type) -> bool:
284282

285283
return is_subtype(typ, self.context.iterable_type)
286284

287-
def is_iterable_instance_subtype(self, typ: Type) -> TypeGuard[Instance]:
288-
from mypy.subtypes import is_subtype
289-
290-
p_t = get_proper_type(typ)
291-
return (
292-
isinstance(p_t, Instance)
293-
and bool(p_t.args)
294-
and is_subtype(p_t, self.context.iterable_type)
295-
)
296-
297285
def is_iterable_instance_type(self, typ: Type) -> TypeGuard[IterableType]:
298286
"""Check if the type is an Iterable[T] or a subtype of it."""
299287
p_t = get_proper_type(typ)
@@ -303,10 +291,6 @@ def _make_iterable_instance_type(self, arg: Type) -> IterableType:
303291
value = Instance(self.context.iterable_type.type, [arg])
304292
return cast(IterableType, value)
305293

306-
def _make_tuple_instance_type(self, arg: Type) -> TupleInstance:
307-
value = Instance(self.context.tuple_type.type, [arg])
308-
return cast(TupleInstance, value)
309-
310294
def _solve_as_iterable(self, typ: Type) -> IterableType | AnyType:
311295
r"""Use the solver to cast a type as Iterable[T].
312296

mypy/checkexpr.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2288,7 +2288,6 @@ def argument_infer_context(self) -> ArgumentInferContext:
22882288
self.chk.named_type("typing.Mapping"),
22892289
self.chk.named_type("typing.Iterable"),
22902290
self.chk.named_type("builtins.function"),
2291-
self.chk.named_type("builtins.tuple"),
22922291
)
22932292
return self._arg_infer_context_cache
22942293

@@ -5252,12 +5251,10 @@ def visit_tuple_expr(self, e: TupleExpr) -> Type:
52525251
ctx = ctx_item.type
52535252
else:
52545253
ctx = None
5255-
5256-
arg_type_expander = ArgTypeExpander(self.argument_infer_context())
52575254
original_arg_type = self.accept(item.expr, ctx)
52585255
# convert arg type to one of TupleType, IterableType, AnyType or
5256+
arg_type_expander = ArgTypeExpander(self.argument_infer_context())
52595257
star_args_type = arg_type_expander.parse_star_args_type(original_arg_type)
5260-
52615258
if isinstance(star_args_type, TupleType):
52625259
if find_unpack_in_list(star_args_type.items) is not None:
52635260
if seen_unpack_in_items:

mypy/constraints.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,13 +1014,15 @@ def visit_instance(self, template: Instance) -> list[Constraint]:
10141014
if isinstance(item, UnpackType):
10151015
unpacked = get_proper_type(item.type)
10161016
if isinstance(unpacked, TypeVarTupleType):
1017-
# Cannot infer anything for T from [T, ...] <: *Ts
1018-
continue
1019-
assert (
1020-
isinstance(unpacked, Instance)
1021-
and unpacked.type.fullname == "builtins.tuple"
1022-
)
1023-
item = unpacked.args[0]
1017+
# Iterable[T] :> tuple[*Ts] => T :> Union[*Ts]
1018+
# Since Union[*Ts] is currently not available, use Any instead.
1019+
item = AnyType(TypeOfAny.from_omitted_generics)
1020+
else:
1021+
assert (
1022+
isinstance(unpacked, Instance)
1023+
and unpacked.type.fullname == "builtins.tuple"
1024+
)
1025+
item = unpacked.args[0]
10241026
cb = infer_constraints(template.args[0], item, SUPERTYPE_OF)
10251027
res.extend(cb)
10261028
return res

mypy/infer.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ class ArgumentInferContext(NamedTuple):
2929
mapping_type: Instance
3030
iterable_type: Instance
3131
function_type: Instance
32-
tuple_type: Instance
3332

3433

3534
def infer_function_type_arguments(

test-data/unit/check-kwargs.test

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,20 @@ f(**m)
498498
g(**m) # E: Argument 1 to "g" has incompatible type "**Mapping[str, object]"; expected "int"
499499
[builtins fixtures/dict.pyi]
500500

501+
[case testStarArgsTupleExpressionVersusTypeVarTuple]
502+
# flags: --enable-incomplete-feature=PreciseTupleTypes
503+
# https://github.com/python/mypy/issues/19665
504+
from typing import Unpack, TypeVarTuple
505+
506+
Ts = TypeVarTuple('Ts')
507+
508+
def as_tuple(*args: Unpack[Ts]) -> tuple[Unpack[Ts]]: return args
509+
510+
def test(one: int, many: tuple[int, ...]) -> None:
511+
reveal_type( (one, *many, one) ) # N: Revealed type is "tuple[builtins.int, Unpack[builtins.tuple[builtins.int, ...]], builtins.int]"
512+
reveal_type( as_tuple(one, *many, one) ) # N: Revealed type is "tuple[builtins.int, Unpack[builtins.tuple[builtins.int, ...]], builtins.int]"
513+
[builtins fixtures/tuple.pyi]
514+
501515
[case testTupleConversionWithUnionStarArgs]
502516
from typing import Union, List, Set, Tuple, Unpack, TypeVarTuple
503517

@@ -805,47 +819,42 @@ def test_union_recursive(x: Union[list[Union[NESTED, None]], list[NESTED]]) -> N
805819
[builtins fixtures/primitives.pyi]
806820

807821

808-
[case testStarArgsWithPaddedTypeVarTuple]
809-
# https://github.com/python/mypy/issues/19659
810-
from typing import Union, TypeVarTuple
822+
[case testStarArgsWithPaddedTupleArgument]
823+
from typing import Union, TypeVarTuple, Unpack
811824

812825
class A: pass
813826
Ts = TypeVarTuple('Ts')
814827

815-
def test_union_typevartuple(
816-
padded_tuple: tuple[A, *Ts, A],
817-
padded_union: Union[tuple[A, *Ts, A], tuple[None, None, None]],
818-
) -> None:
828+
def test_padded_tuple(padded_tuple: tuple[A, Unpack[Ts], A]) -> None:
819829
# technically, this should be ``typ[A | None | Union[*Ts]]``
820830
reveal_type( (*padded_tuple,) ) # N: Revealed type is "tuple[__main__.A, Unpack[Ts`-1], __main__.A]"
821831
reveal_type( [*padded_tuple] ) # N: Revealed type is "builtins.list[builtins.object]"
822832
reveal_type( {*padded_tuple} ) # N: Revealed type is "builtins.set[builtins.object]"
833+
834+
def test_padded_union(padded_union: Union[tuple[A, Unpack[Ts], A], tuple[None, None, None]]) -> None:
823835
reveal_type( (*padded_union,) ) # N: Revealed type is "builtins.tuple[builtins.object, ...]"
824836
reveal_type( [*padded_union] ) # N: Revealed type is "builtins.list[builtins.object]"
825837
reveal_type( {*padded_union} ) # N: Revealed type is "builtins.set[builtins.object]"
826838
[builtins fixtures/primitives.pyi]
827839

828-
[case testStarArgsWithParamSpec]
829-
# https://github.com/python/mypy/issues/19663
830-
from typing import Union, ParamSpec, Callable
831840

832-
P = ParamSpec('P')
841+
[case testStarArgsWithPaddedTupleArgumentUpcast]
842+
# https://github.com/python/mypy/issues/19659
843+
from typing import Iterable, TypeVarTuple, Unpack, Union, TypeVar
833844

834-
def test_paramspec(
835-
a: A, # E: Name "A" is not defined
836-
dummy: Callable[P, None], # ensure P is bound
837-
/,
838-
*args: P.args,
839-
**kwargs: P.kwargs,
840-
) -> None:
841-
# technically, this should be ``typ[A | None | Union[*Ts]]``
842-
reveal_type( args ) # N: Revealed type is "P.args`-1"
843-
reveal_type( (*args,) ) # N: Revealed type is "builtins.tuple[P.args`-1, ...]"
844-
reveal_type( [*args] ) # N: Revealed type is "builtins.list[P.args`-1]"
845-
reveal_type( {*args} ) # N: Revealed type is "builtins.set[P.args`-1]"
845+
class A: pass
846+
Ts = TypeVarTuple('Ts')
847+
T = TypeVar('T')
848+
849+
def upcast(arg: Iterable[T]) -> Iterable[T]: return arg
846850

847-
# test padded tuple expression
848-
reveal_type( (a, *args, a) ) # N: Revealed type is "builtins.tuple[__main__.A, Unpack[builtins.tuple[P.args`-1, ...]], __main__.A]"
851+
def test_padded_tuple(padded_tuple: tuple[A, Unpack[Ts], A]) -> None:
852+
as_iterable = upcast(padded_tuple)
853+
reveal_type(as_iterable) # N: Revealed type is "typing.Iterable[Any]"
854+
855+
def test_padded_union(padded_union: Union[tuple[A, Unpack[Ts], A], tuple[None, None, None]]) -> None:
856+
as_iterable = upcast(padded_union)
857+
reveal_type(as_iterable) # N: Revealed type is "typing.Iterable[Any]"
849858
[builtins fixtures/primitives.pyi]
850859

851860
[case testStarArgsWithUnion]
@@ -973,7 +982,6 @@ def test_bad_case(
973982
# E: Argument 1 to "g" has incompatible type "*tuple[Union[A, B, C, None], ...]"; expected "Optional[D]"
974983
[builtins fixtures/tuple.pyi]
975984

976-
977985
[case testListExpressionWithListSubtypeStarArgs]
978986
# https://github.com/python/mypy/issues/19662
979987
class MyList(list[int]): ...
@@ -983,8 +991,6 @@ def test(x: MyList, y: list[int]) -> None:
983991
reveal_type( [*y] ) # N: Revealed type is "builtins.list[builtins.int]"
984992
[builtins fixtures/list.pyi]
985993

986-
987-
988994
[case testPassingEmptyDictWithStars]
989995
def f(): pass
990996
def g(x=1): pass

test-data/unit/lib-stub/builtins.pyi

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,17 @@ class function:
2323
__name__: str
2424
class ellipsis: pass
2525

26-
from typing import Generic, Iterator, Sequence, TypeVar
26+
from typing import Generic, Iterator, Sequence, TypeVar, Iterable
2727
_T = TypeVar('_T')
2828
class list(Generic[_T], Sequence[_T]):
2929
def __contains__(self, item: object) -> bool: pass
3030
def __getitem__(self, key: int) -> _T: pass
3131
def __iter__(self) -> Iterator[_T]: pass
3232

33-
class dict: pass
33+
# class tuple(Generic[_T], Sequence[_T]):
34+
# def __contains__(self, item: object) -> bool: pass
35+
# def __getitem__(self, key: int) -> _T: pass
36+
# def __iter__(self) -> Iterator[_T]: pass
3437

38+
class dict: pass
3539
# Definition of None is implicit

0 commit comments

Comments
 (0)