Skip to content

Commit 80292ba

Browse files
author
Guido van Rossum
committed
Only do the lightweight type inferencing thing outside functions. Roll back changes to check-typevar-values.test.
1 parent e7e06a8 commit 80292ba

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

mypy/semanal.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1083,10 +1083,14 @@ def visit_assignment_stmt(self, s: AssignmentStmt) -> None:
10831083

10841084
def analyze_simple_literal_type(self, rvalue: Node) -> Optional[Type]:
10851085
"""Return builtins.int if rvalue is an int literal, etc."""
1086-
if self.weak_opts or not self.lightweight_type_check:
1086+
if self.weak_opts or not self.lightweight_type_check or self.function_stack:
10871087
# Skip this if any weak options are set.
10881088
# Also skip if lightweight type check not requested.
10891089
# This is mostly to avoid breaking unit tests.
1090+
# Also skip inside a function; this is to avoid confusing
1091+
# the code that handles dead code due to isinstance()
1092+
# inside type variables with value restrictions (like
1093+
# AnyStr).
10901094
return None
10911095
if isinstance(rvalue, IntExpr):
10921096
return self.named_type_or_none('builtins.int')

test-data/unit/check-typevar-values.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,9 @@ from typing import TypeVar
145145
T = TypeVar('T', int, str)
146146
def f(x: T) -> T:
147147
if isinstance(x, int):
148-
y = 1+0
148+
y = 1
149149
else:
150-
y = ''+''
150+
y = ''
151151
return y
152152
[builtins fixtures/isinstance.py]
153153

@@ -156,7 +156,7 @@ from typing import TypeVar
156156
T = TypeVar('T', int, str)
157157
def f(x: T) -> T:
158158
if isinstance(x, int):
159-
y = 1+0
159+
y = 1
160160
else:
161161
y = object()
162162
return y # E: Incompatible return value type (got "object", expected "str")

0 commit comments

Comments
 (0)