Skip to content

Commit feb9413

Browse files
committed
Fix last bits
1 parent c106204 commit feb9413

File tree

4 files changed

+26
-17
lines changed

4 files changed

+26
-17
lines changed

docs/source/kinds_of_types.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -485,9 +485,9 @@ Following previous examples:
485485

486486
A type alias does not create a new type. It's just a shorthand notation for
487487
another type -- it's equivalent to the target type. For generic type aliases
488-
this means that variance or constraints of type variables used for alias
489-
definition don't apply to aliases. Parameterized generic alias is treated
490-
simply as an original type with corresponding type variables substituted.
488+
this means that variance of type variables used for alias definition does not
489+
apply to aliases. Parameterized generic alias is treated simply as an original
490+
type with corresponding type variables substituted.
491491

492492
.. _newtypes:
493493

mypy/typeanal.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,15 @@ def visit_unbound_type(self, t: UnboundType) -> Type:
154154
act_len = len(an_args)
155155
if exp_len > 0 and act_len == 0:
156156
# Interpret bare Alias same as normal generic, i.e., Alias[Any, Any, ...]
157-
return self.replace_alias_tvars(override, all_vars, [AnyType()] * exp_len)
157+
return self.replace_alias_tvars(override, all_vars, [AnyType()] * exp_len,
158+
t.line, t.column)
158159
if exp_len == 0 and act_len == 0:
159160
return override
160161
if act_len != exp_len:
161162
self.fail('Bad number of arguments for type alias, expected: %s, given: %s'
162163
% (exp_len, act_len), t)
163164
return t
164-
return self.replace_alias_tvars(override, all_vars, an_args)
165+
return self.replace_alias_tvars(override, all_vars, an_args, t.line, t.column)
165166
elif not isinstance(sym.node, TypeInfo):
166167
name = sym.fullname
167168
if name is None:
@@ -233,9 +234,10 @@ def get_tvar_name(self, t: Type) -> Optional[str]:
233234
return t.name
234235
return None
235236

236-
def replace_alias_tvars(self, tp: Type, vars: List[str], subs: List[Type]) -> Type:
237-
"""Replace type variables in a generic type alias tp with substitutions subs.
238-
Length of subs should be already checked.
237+
def replace_alias_tvars(self, tp: Type, vars: List[str], subs: List[Type],
238+
newline: int, newcolumn: int) -> Type:
239+
"""Replace type variables in a generic type alias tp with substitutions subs
240+
resetting context. Length of subs should be already checked.
239241
"""
240242
typ_args = get_typ_args(tp)
241243
new_args = typ_args[:]
@@ -246,8 +248,8 @@ def replace_alias_tvars(self, tp: Type, vars: List[str], subs: List[Type]) -> Ty
246248
new_args[i] = subs[vars.index(tvar)]
247249
else:
248250
# ...recursively, if needed.
249-
new_args[i] = self.replace_alias_tvars(arg, vars, subs)
250-
return set_typ_args(tp, new_args)
251+
new_args[i] = self.replace_alias_tvars(arg, vars, subs, newline, newcolumn)
252+
return set_typ_args(tp, new_args, newline, newcolumn)
251253

252254
def visit_any(self, t: AnyType) -> Type:
253255
return t

mypy/types.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -1536,14 +1536,17 @@ def get_typ_args(tp: Type) -> List[Type]:
15361536
return typ_args
15371537

15381538

1539-
def set_typ_args(tp: Type, new_args: List[Type]) -> Type:
1539+
def set_typ_args(tp: Type, new_args: List[Type], line: int = -1, column: int = -1) -> Type:
15401540
"""Return a copy of a parameterizable Type with arguments set to new_args."""
1541+
line = line if line > 0 else tp.line
1542+
column = column if column > 0 else tp.column
15411543
if isinstance(tp, Instance):
1542-
return Instance(tp.type, new_args, tp.line, tp.column)
1544+
return Instance(tp.type, new_args, line, column)
15431545
if isinstance(tp, TupleType):
15441546
return tp.copy_modified(items=new_args)
15451547
if isinstance(tp, UnionType):
1546-
return UnionType.make_simplified_union(new_args, tp.line, tp.column)
1548+
return UnionType.make_simplified_union(new_args, line, column)
15471549
if isinstance(tp, CallableType):
1548-
return tp.copy_modified(arg_types=new_args[:-1], ret_type=new_args[-1])
1550+
return tp.copy_modified(arg_types=new_args[:-1], ret_type=new_args[-1],
1551+
line=line, column=column)
15491552
return tp

test-data/unit/check-generics.test

+7-3
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ E = Node[Node[T, T], List[T]]
601601
F = Node[List[T, T], S] # E: "list" expects 1 type argument, but 2 given
602602
G = Callable[..., List[T, T]] # E: "list" expects 1 type argument, but 2 given
603603
H = Union[int, Tuple[T, Node[T]]] # E: "Node" expects 2 type arguments, but 1 given
604-
h = None # type: H
604+
h = None # type: H # E: "Node" expects 2 type arguments, but 1 given
605605
h1 = None # type: H[int, str] # E: Bad number of arguments for type alias, expected: 1, given: 2
606606

607607
x = None # type: D[int, str]
@@ -945,13 +945,17 @@ S = TypeVar('S', int, list)
945945
class A(Generic[T, S]):
946946
def __init__(self, x: T, y: S) -> None: ...
947947

948-
BadA = A[str, T] # E: Bad ...
948+
BadA = A[str, T] # This error is reported twice (but it actually looks useful)
949949
SameA = A[T, T]
950950

951951
x = None # type: SameA[int]
952-
y = None # type: SameA[str] # E: Bad ...
952+
y = None # type: SameA[str] # E: Invalid type argument value for "A"
953953

954954
[builtins fixtures/list.pyi]
955+
[out]
956+
main:8: error: Invalid type argument value for "A"
957+
main:8: error: Type argument 1 of "A" has incompatible value "str"
958+
955959

956960
-- Multiple assignment with lists
957961
-- ------------------------------

0 commit comments

Comments
 (0)