Skip to content

Commit a230dd9

Browse files
committed
CR fixes (partial)
1 parent ea2b1b4 commit a230dd9

File tree

8 files changed

+14
-19
lines changed

8 files changed

+14
-19
lines changed

mypy/expandtype.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
FunctionLike, TypeVarDef
88
)
99

10-
import mypy
11-
1210

1311
def expand_type(typ: Type, env: Mapping[TypeVarId, Type]) -> Type:
1412
"""Substitute any type variable references in a type given by a type

mypy/typeanal.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,8 @@ def visit_unbound_type(self, t: UnboundType) -> Type:
240240
return t
241241
info = sym.node # type: TypeInfo
242242
if info.fullname() == 'builtins.tuple':
243+
# tuple[int] or tuple[int, ...] are invalid
244+
# The naked tuple should use canonical representation with type argument Any
243245
assert not t.args
244246
return Instance(info, self.anal_array([AnyType()]), t.line, t.column)
245247
else:
@@ -633,11 +635,6 @@ def visit_callable_type(self, t: CallableType) -> None:
633635
def visit_tuple_type(self, t: TupleType) -> None:
634636
for item in t.items:
635637
item.accept(self)
636-
# if it's not builtins.tuple, then its bases should have tuple[Any]
637-
# TODO: put assert here if it's not too slow
638-
if isinstance(t.fallback, Instance) and t.fallback.type.fullname() == 'builtins.tuple':
639-
fallback_item = UnionType.make_simplified_union(t.items)
640-
t.fallback.args = [fallback_item]
641638

642639
def visit_typeddict_type(self, t: TypedDictType) -> None:
643640
for item_type in t.items.values():

mypy/types.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,7 +1496,7 @@ def visit_tuple_type(self, t: TupleType) -> str:
14961496
s = self.list_str(t.items)
14971497
if t.fallback and t.fallback.type:
14981498
fallback_name = t.fallback.type.fullname()
1499-
if fallback_name not in ('builtins.tuple', 'builtins.object'):
1499+
if fallback_name != 'builtins.tuple':
15001500
return 'Tuple[{}, fallback={}]'.format(s, t.fallback.accept(self))
15011501
return 'Tuple[{}]'.format(s)
15021502

@@ -1747,9 +1747,7 @@ def set_typ_args(tp: Type, new_args: List[Type], line: int = -1, column: int = -
17471747
if isinstance(tp, Instance):
17481748
return Instance(tp.type, new_args, line, column)
17491749
if isinstance(tp, TupleType):
1750-
fallback_args = [UnionType.make_simplified_union(new_args)]
1751-
fallback = tp.fallback.copy_modified(args=fallback_args)
1752-
return tp.copy_modified(items=new_args, fallback=fallback)
1750+
return tp.copy_modified(items=new_args)
17531751
if isinstance(tp, UnionType):
17541752
return UnionType(new_args, line, column)
17551753
if isinstance(tp, CallableType):

test-data/unit/check-tuples.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -604,10 +604,10 @@ b = s in t
604604

605605
[file builtins.py]
606606
from typing import TypeVar, Generic
607-
_T = TypeVar('_T')
607+
_T_co = TypeVar('_T_co', covariant=True)
608608
class object:
609609
def __init__(self) -> None: pass
610-
class tuple(Generic[_T]):
610+
class tuple(Generic[_T_co]):
611611
def __len__(self) -> int: pass
612612
def __str__(self) -> str: pass
613613
def __contains__(self, o: object) -> bool: pass

test-data/unit/fixtures/callable.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
from typing import Generic, Tuple, TypeVar, Union
22

3-
T = TypeVar('T')
3+
T_co = TypeVar('T_co', covariant=True)
44

55
class object:
66
def __init__(self) -> None: pass
77

88
class type:
99
def __init__(self, x) -> None: pass
1010

11-
class tuple(Generic[T]): pass
11+
class tuple(Generic[T_co]): pass
1212

1313
class function: pass
1414

test-data/unit/fixtures/f_string.pyi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ from typing import TypeVar, Generic, Iterable, Iterator, List, overload
55

66
T = TypeVar('T')
77

8+
T_co = TypeVar('T_co', covariant=True)
9+
810
class object:
911
def __init__(self): pass
1012

@@ -20,7 +22,7 @@ class list(Iterable[T], Generic[T]):
2022
def __init__(self, x: Iterable[T]) -> None: pass
2123
def append(self, x: T) -> None: pass
2224

23-
class tuple(Generic[T]): pass
25+
class tuple(Generic[T_co]): pass
2426

2527
class function: pass
2628
class int:

test-data/unit/fixtures/isinstance.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
from typing import Tuple, TypeVar, Generic, Union
22

3-
T = TypeVar('T')
3+
T_co = TypeVar('T_co', covariant=True)
44

55
class object:
66
def __init__(self) -> None: pass
77

88
class type:
99
def __init__(self, x) -> None: pass
1010

11-
class tuple(Generic[T]): pass
11+
class tuple(Generic[T_co]): pass
1212

1313
class function: pass
1414

test-data/unit/fixtures/list.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class list(Iterable[T], Generic[T]):
2323
def extend(self, x: Iterable[T]) -> None: pass
2424

2525
_T_co = TypeVar('_T_co', covariant=True)
26-
class tuple(Sequence[_T_co], Generic[_T_co]): pass
26+
class tuple(Generic[_T_co]): pass
2727
class function: pass
2828
class int: pass
2929
class float: pass

0 commit comments

Comments
 (0)