Skip to content

Commit a6b9d37

Browse files
Merge branch 'master' into for-loop-len-cache
2 parents 5ad712a + c6b40df commit a6b9d37

File tree

4 files changed

+38
-29
lines changed

4 files changed

+38
-29
lines changed

docs/source/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,9 @@
278278
intersphinx_mapping = {
279279
"python": ("https://docs.python.org/3", None),
280280
"attrs": ("https://www.attrs.org/en/stable/", None),
281-
"cython": ("https://docs.cython.org/en/latest", None),
281+
"cython": ("https://cython.readthedocs.io/en/stable", None),
282282
"monkeytype": ("https://monkeytype.readthedocs.io/en/latest", None),
283-
"setuptools": ("https://setuptools.readthedocs.io/en/latest", None),
283+
"setuptools": ("https://setuptools.pypa.io/en/latest", None),
284284
}
285285

286286

mypy/checker.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5577,6 +5577,8 @@ def infer_variable_types_from_type_maps(
55775577
previous_type, _, _ = self.check_lvalue(expr)
55785578
if previous_type is not None:
55795579
already_exists = True
5580+
if isinstance(expr.node, Var) and expr.node.is_final:
5581+
self.msg.cant_assign_to_final(expr.name, False, expr)
55805582
if self.check_subtype(
55815583
typ,
55825584
previous_type,

mypy/checkexpr.py

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1755,14 +1755,6 @@ def check_callable_call(
17551755
return AnyType(TypeOfAny.from_error), callee
17561756
seen_unpack = True
17571757

1758-
formal_to_actual = map_actuals_to_formals(
1759-
arg_kinds,
1760-
arg_names,
1761-
callee.arg_kinds,
1762-
callee.arg_names,
1763-
lambda i: self.accept(args[i]),
1764-
)
1765-
17661758
# This is tricky: return type may contain its own type variables, like in
17671759
# def [S] (S) -> def [T] (T) -> tuple[S, T], so we need to update their ids
17681760
# to avoid possible id clashes if this call itself appears in a generic
@@ -1773,27 +1765,29 @@ def check_callable_call(
17731765
freeze_all_type_vars(fresh_ret_type)
17741766
callee = callee.copy_modified(ret_type=fresh_ret_type)
17751767

1768+
if callee.is_generic():
1769+
callee = freshen_function_type_vars(callee)
1770+
callee = self.infer_function_type_arguments_using_context(callee, context)
1771+
1772+
formal_to_actual = map_actuals_to_formals(
1773+
arg_kinds,
1774+
arg_names,
1775+
callee.arg_kinds,
1776+
callee.arg_names,
1777+
lambda i: self.accept(args[i]),
1778+
)
1779+
17761780
if callee.is_generic():
17771781
need_refresh = any(
17781782
isinstance(v, (ParamSpecType, TypeVarTupleType)) for v in callee.variables
17791783
)
1780-
callee = freshen_function_type_vars(callee)
1781-
callee = self.infer_function_type_arguments_using_context(callee, context)
1782-
if need_refresh:
1783-
# Argument kinds etc. may have changed due to
1784-
# ParamSpec or TypeVarTuple variables being replaced with an arbitrary
1785-
# number of arguments; recalculate actual-to-formal map
1786-
formal_to_actual = map_actuals_to_formals(
1787-
arg_kinds,
1788-
arg_names,
1789-
callee.arg_kinds,
1790-
callee.arg_names,
1791-
lambda i: self.accept(args[i]),
1792-
)
17931784
callee = self.infer_function_type_arguments(
17941785
callee, args, arg_kinds, arg_names, formal_to_actual, need_refresh, context
17951786
)
17961787
if need_refresh:
1788+
# Argument kinds etc. may have changed due to
1789+
# ParamSpec or TypeVarTuple variables being replaced with an arbitrary
1790+
# number of arguments; recalculate actual-to-formal map
17971791
formal_to_actual = map_actuals_to_formals(
17981792
arg_kinds,
17991793
arg_names,
@@ -2258,6 +2252,11 @@ def infer_function_type_arguments_pass2(
22582252
if isinstance(arg, (NoneType, UninhabitedType)) or has_erased_component(arg):
22592253
inferred_args[i] = None
22602254
callee_type = self.apply_generic_arguments(callee_type, inferred_args, context)
2255+
2256+
if not callee_type.is_generic():
2257+
# Fast path, second pass can't give new information.
2258+
return callee_type, []
2259+
22612260
if need_refresh:
22622261
formal_to_actual = map_actuals_to_formals(
22632262
arg_kinds,
@@ -3469,7 +3468,7 @@ def visit_op_expr(self, e: OpExpr) -> Type:
34693468
# It's actually a type expression X | Y.
34703469
return self.accept(e.analyzed)
34713470
if e.op == "and" or e.op == "or":
3472-
return self.check_boolean_op(e, e)
3471+
return self.check_boolean_op(e)
34733472
if e.op == "*" and isinstance(e.left, ListExpr):
34743473
# Expressions of form [...] * e get special type inference.
34753474
return self.check_list_multiply(e)
@@ -4256,20 +4255,18 @@ def check_op(
42564255
context=context,
42574256
)
42584257

4259-
def check_boolean_op(self, e: OpExpr, context: Context) -> Type:
4258+
def check_boolean_op(self, e: OpExpr) -> Type:
42604259
"""Type check a boolean operation ('and' or 'or')."""
42614260

42624261
# A boolean operation can evaluate to either of the operands.
42634262

4264-
# We use the current type context to guide the type inference of of
4263+
# We use the current type context to guide the type inference of
42654264
# the left operand. We also use the left operand type to guide the type
42664265
# inference of the right operand so that expressions such as
42674266
# '[1] or []' are inferred correctly.
42684267
ctx = self.type_context[-1]
42694268
left_type = self.accept(e.left, ctx)
4270-
expanded_left_type = try_expanding_sum_type_to_union(
4271-
self.accept(e.left, ctx), "builtins.bool"
4272-
)
4269+
expanded_left_type = try_expanding_sum_type_to_union(left_type, "builtins.bool")
42734270

42744271
assert e.op in ("and", "or") # Checked by visit_op_expr
42754272

test-data/unit/check-python310.test

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2839,3 +2839,13 @@ match value_type:
28392839
case _:
28402840
assert_never(value_type)
28412841
[builtins fixtures/tuple.pyi]
2842+
2843+
[case testAssignmentToFinalInMatchCaseNotAllowed]
2844+
from typing import Final
2845+
2846+
FOO: Final[int] = 10
2847+
2848+
val: int = 8
2849+
match val:
2850+
case FOO: # E: Cannot assign to final name "FOO"
2851+
pass

0 commit comments

Comments
 (0)