Skip to content

Commit 9a9a201

Browse files
committed
Various changes
1 parent 3baa4e9 commit 9a9a201

File tree

12 files changed

+173
-77
lines changed

12 files changed

+173
-77
lines changed

mypy/applytype.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,14 @@ def apply_generic_arguments(
104104
tvar, type, callable, report_incompatible_typevar_value, context, skip_unsatisfied
105105
)
106106
if isinstance(target_type, UninhabitedType) and tvar.has_default():
107-
if isinstance(tvar.default, TypeVarLikeType):
108-
id_to_type[tvar.id] = id_to_type[tvar.default.id]
109-
else:
110-
id_to_type[tvar.id] = tvar.default
111-
elif isinstance(target_type, TypeVarLikeType) and isinstance(
112-
target_type.default, TypeVarLikeType
113-
):
114-
id_to_type[target_type.id] = id_to_type[target_type.default.id]
107+
# if isinstance(tvar.default, TypeVarLikeType):
108+
# id_to_type[tvar.id] = id_to_type[tvar.default.id]
109+
# else:
110+
id_to_type[tvar.id] = tvar.default
111+
# elif isinstance(target_type, TypeVarLikeType) and isinstance(
112+
# target_type.default, TypeVarLikeType
113+
# ):
114+
# id_to_type[target_type.id] = id_to_type[target_type.default.id]
115115
elif target_type is not None:
116116
id_to_type[tvar.id] = target_type
117117

mypy/checkexpr.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5255,6 +5255,16 @@ def visit_callable_type(self, t: CallableType) -> bool:
52555255
return False
52565256
return super().visit_callable_type(t)
52575257

5258+
def visit_type_var(self, t: TypeVarType) -> bool:
5259+
# Exclude t.default - the default value for t.default is Any
5260+
return self.query_types([t.upper_bound] + t.values)
5261+
5262+
def visit_param_spec(self, t: ParamSpecType) -> bool:
5263+
return self.default
5264+
5265+
def visit_type_var_tuple(self, t: TypeVarTupleType) -> bool:
5266+
return self.default
5267+
52585268

52595269
def has_coroutine_decorator(t: Type) -> bool:
52605270
"""Whether t came from a function decorated with `@coroutine`."""

mypy/expandtype.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
flatten_nested_unions,
3939
get_proper_type,
4040
has_param_specs,
41-
has_type_vars,
41+
has_type_vars_default,
4242
remove_trivial,
4343
)
4444
from mypy.typevartuples import (
@@ -228,11 +228,13 @@ def visit_type_var(self, t: TypeVarType) -> Type:
228228
t = t.copy_modified(upper_bound=t.upper_bound.accept(self))
229229
repl = self.variables.get(t.id, t)
230230

231-
if has_type_vars(repl) and not isinstance(repl, Instance):
232-
if repl in self.recursive_guard: # or isinstance(repl, CallableType):
231+
if has_type_vars_default(repl):
232+
if repl in self.recursive_guard:
233233
return repl
234234
self.recursive_guard.add(repl)
235235
repl = repl.accept(self)
236+
if isinstance(repl, TypeVarType):
237+
repl.default = repl.default.accept(self)
236238

237239
if isinstance(repl, Instance):
238240
# TODO: do we really need to do this?

mypy/exprtotype.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
Type,
3434
TypeList,
3535
TypeOfAny,
36+
TypeOfTypeList,
3637
UnboundType,
3738
UnionType,
3839
)
@@ -164,6 +165,9 @@ def expr_to_unanalyzed_type(
164165
elif isinstance(expr, (ListExpr, TupleExpr)):
165166
return TypeList(
166167
[expr_to_unanalyzed_type(t, options, allow_new_syntax, expr) for t in expr.items],
168+
TypeOfTypeList.callable_args
169+
if isinstance(expr, ListExpr)
170+
else TypeOfTypeList.param_spec_default,
167171
line=expr.line,
168172
column=expr.column,
169173
)

mypy/messages.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2771,12 +2771,15 @@ def for_function(callee: CallableType) -> str:
27712771
return ""
27722772

27732773

2774-
def wrong_type_arg_count(n: int, act: str, name: str) -> str:
2775-
s = f"{n} type arguments"
2776-
if n == 0:
2777-
s = "no type arguments"
2778-
elif n == 1:
2779-
s = "1 type argument"
2774+
def wrong_type_arg_count(low: int, high: int, act: str, name: str) -> str:
2775+
if low == high:
2776+
s = f"{low} type arguments"
2777+
if low == 0:
2778+
s = "no type arguments"
2779+
elif low == 1:
2780+
s = "1 type argument"
2781+
else:
2782+
s = f"between {low} and {high} type arguments"
27802783
if act == "0":
27812784
act = "none"
27822785
return f'"{name}" expects {s}, but {act} given'

mypy/nodes.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2826,6 +2826,7 @@ class is generic then it will be a type constructor of higher kind.
28262826
"fallback_to_any",
28272827
"meta_fallback_to_any",
28282828
"type_vars",
2829+
"has_type_var_default",
28292830
"has_param_spec_type",
28302831
"bases",
28312832
"_promote",
@@ -2928,6 +2929,9 @@ class is generic then it will be a type constructor of higher kind.
29282929
# Generic type variable names (full names)
29292930
type_vars: list[str]
29302931

2932+
# Whether this class has a TypeVar with a default value
2933+
has_type_var_default: bool
2934+
29312935
# Whether this class has a ParamSpec type variable
29322936
has_param_spec_type: bool
29332937

@@ -3009,6 +3013,7 @@ def __init__(self, names: SymbolTable, defn: ClassDef, module_name: str) -> None
30093013
self.defn = defn
30103014
self.module_name = module_name
30113015
self.type_vars = []
3016+
self.has_type_var_default = False
30123017
self.has_param_spec_type = False
30133018
self.has_type_var_tuple_type = False
30143019
self.bases = []
@@ -3048,6 +3053,8 @@ def add_type_vars(self) -> None:
30483053
self.has_type_var_tuple_type = False
30493054
if self.defn.type_vars:
30503055
for i, vd in enumerate(self.defn.type_vars):
3056+
if vd.has_default():
3057+
self.has_type_var_default = True
30513058
if isinstance(vd, mypy.types.ParamSpecType):
30523059
self.has_param_spec_type = True
30533060
if isinstance(vd, mypy.types.TypeVarTupleType):
@@ -3242,6 +3249,7 @@ def serialize(self) -> JsonDict:
32423249
"defn": self.defn.serialize(),
32433250
"abstract_attributes": self.abstract_attributes,
32443251
"type_vars": self.type_vars,
3252+
"has_type_var_default": self.has_type_var_default,
32453253
"has_param_spec_type": self.has_param_spec_type,
32463254
"bases": [b.serialize() for b in self.bases],
32473255
"mro": [c.fullname for c in self.mro],
@@ -3274,6 +3282,7 @@ def deserialize(cls, data: JsonDict) -> TypeInfo:
32743282
# TODO: Is there a reason to reconstruct ti.subtypes?
32753283
ti.abstract_attributes = [(attr[0], attr[1]) for attr in data["abstract_attributes"]]
32763284
ti.type_vars = data["type_vars"]
3285+
ti.has_type_var_default = data["has_type_var_default"]
32773286
ti.has_param_spec_type = data["has_param_spec_type"]
32783287
ti.bases = [mypy.types.Instance.deserialize(b) for b in data["bases"]]
32793288
_promote = []

mypy/semanal.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@
178178
type_aliases_source_versions,
179179
typing_extensions_aliases,
180180
)
181-
from mypy.options import Options
181+
from mypy.options import TYPE_VAR_TUPLE, Options
182182
from mypy.patterns import (
183183
AsPattern,
184184
ClassPattern,
@@ -3996,7 +3996,7 @@ def process_typevar_declaration(self, s: AssignmentStmt) -> bool:
39963996
type_var.line = call.line
39973997
call.analyzed = type_var
39983998
updated = True
3999-
# self.tvar_scope.bind_new(name, type_var)
3999+
# self.tvar_scope.bind_new(name, type_var) # TODO!
40004000
else:
40014001
assert isinstance(call.analyzed, TypeVarExpr)
40024002
updated = (
@@ -4196,6 +4196,9 @@ def process_paramspec_declaration(self, s: AssignmentStmt) -> bool:
41964196
return False
41974197

41984198
n_values = call.arg_kinds[1:].count(ARG_POS)
4199+
if n_values != 0:
4200+
self.fail("Only the first positional argument to ParamSpec has defined semantics", s)
4201+
41994202
default: Type = AnyType(TypeOfAny.from_omitted_generics)
42004203
for param_value, param_name, param_kind in zip(
42014204
call.args[1 + n_values :],
@@ -4226,7 +4229,7 @@ def process_paramspec_declaration(self, s: AssignmentStmt) -> bool:
42264229
)
42274230
paramspec_var.line = call.line
42284231
call.analyzed = paramspec_var
4229-
self.tvar_scope.bind_new(name, paramspec_var)
4232+
# self.tvar_scope.bind_new(name, paramspec_var) # TODO!
42304233
else:
42314234
assert isinstance(call.analyzed, ParamSpecExpr)
42324235
self.add_symbol(name, call.analyzed, s)
@@ -4244,6 +4247,11 @@ def process_typevartuple_declaration(self, s: AssignmentStmt) -> bool:
42444247
return False
42454248

42464249
n_values = call.arg_kinds[1:].count(ARG_POS)
4250+
if n_values != 0:
4251+
self.fail(
4252+
"Only the first positional argument to TypeVarTuple has defined semantics", s
4253+
)
4254+
42474255
default: Type = AnyType(TypeOfAny.from_omitted_generics)
42484256
for param_value, param_name, param_kind in zip(
42494257
call.args[1 + n_values :],
@@ -4266,8 +4274,8 @@ def process_typevartuple_declaration(self, s: AssignmentStmt) -> bool:
42664274
s,
42674275
)
42684276

4269-
# if not self.incomplete_feature_enabled(TYPE_VAR_TUPLE, s):
4270-
# return False
4277+
if not self.incomplete_feature_enabled(TYPE_VAR_TUPLE, s):
4278+
return False
42714279

42724280
name = self.extract_typevarlike_name(s, call)
42734281
if name is None:
@@ -4286,6 +4294,7 @@ def process_typevartuple_declaration(self, s: AssignmentStmt) -> bool:
42864294
)
42874295
typevartuple_var.line = call.line
42884296
call.analyzed = typevartuple_var
4297+
# self.tvar_scope.bind_new(name, typevartuple_var) # TODO!
42894298
else:
42904299
assert isinstance(call.analyzed, TypeVarTupleExpr)
42914300
self.add_symbol(name, call.analyzed, s)

mypy/type_visitor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,10 +490,10 @@ def visit_type_var(self, t: TypeVarType) -> bool:
490490
return self.query_types([t.upper_bound, t.default] + t.values)
491491

492492
def visit_param_spec(self, t: ParamSpecType) -> bool:
493-
return self.default
493+
return t.default.accept(self)
494494

495495
def visit_type_var_tuple(self, t: TypeVarTupleType) -> bool:
496-
return self.default
496+
return t.default.accept(self)
497497

498498
def visit_unpack_type(self, t: UnpackType) -> bool:
499499
return self.query_types([t.type])

0 commit comments

Comments
 (0)