Skip to content

Commit 93d4cb0

Browse files
authored
Enable new type inference by default (#16345)
Fixes #15906 I am adding `--old-type-inference` so people can disable the flag if they have issues (for few releases). IIRC there will be some fallback in `mypy_primer`, but last time I checked it was all correct. Also I don't remember if we need to update some tests, but we will see.
1 parent 9011ca8 commit 93d4cb0

8 files changed

+61
-14
lines changed

mypy/checker.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4043,11 +4043,11 @@ def is_valid_defaultdict_partial_value_type(self, t: ProperType) -> bool:
40434043
return True
40444044
if len(t.args) == 1:
40454045
arg = get_proper_type(t.args[0])
4046-
if self.options.new_type_inference:
4047-
allowed = isinstance(arg, (UninhabitedType, NoneType))
4048-
else:
4046+
if self.options.old_type_inference:
40494047
# Allow leaked TypeVars for legacy inference logic.
40504048
allowed = isinstance(arg, (UninhabitedType, NoneType, TypeVarType))
4049+
else:
4050+
allowed = isinstance(arg, (UninhabitedType, NoneType))
40514051
if allowed:
40524052
return True
40534053
return False

mypy/checkexpr.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ def __init__(
343343
# on whether current expression is a callee, to give better error messages
344344
# related to type context.
345345
self.is_callee = False
346-
type_state.infer_polymorphic = self.chk.options.new_type_inference
346+
type_state.infer_polymorphic = not self.chk.options.old_type_inference
347347

348348
def reset(self) -> None:
349349
self.resolved_type = {}
@@ -2082,7 +2082,7 @@ def infer_function_type_arguments(
20822082
elif not first_arg or not is_subtype(self.named_type("builtins.str"), first_arg):
20832083
self.chk.fail(message_registry.KEYWORD_ARGUMENT_REQUIRES_STR_KEY_TYPE, context)
20842084

2085-
if self.chk.options.new_type_inference and any(
2085+
if not self.chk.options.old_type_inference and any(
20862086
a is None
20872087
or isinstance(get_proper_type(a), UninhabitedType)
20882088
or set(get_type_vars(a)) & set(callee_type.variables)
@@ -2181,7 +2181,11 @@ def infer_function_type_arguments_pass2(
21812181
lambda a: self.accept(args[a]),
21822182
)
21832183

2184-
arg_types = self.infer_arg_types_in_context(callee_type, args, arg_kinds, formal_to_actual)
2184+
# Same as during first pass, disable type errors (we still have partial context).
2185+
with self.msg.filter_errors():
2186+
arg_types = self.infer_arg_types_in_context(
2187+
callee_type, args, arg_kinds, formal_to_actual
2188+
)
21852189

21862190
inferred_args, _ = infer_function_type_arguments(
21872191
callee_type,
@@ -5230,7 +5234,7 @@ def infer_lambda_type_using_context(
52305234
# they must be considered as indeterminate. We use ErasedType since it
52315235
# does not affect type inference results (it is for purposes like this
52325236
# only).
5233-
if self.chk.options.new_type_inference:
5237+
if not self.chk.options.old_type_inference:
52345238
# With new type inference we can preserve argument types even if they
52355239
# are generic, since new inference algorithm can handle constraints
52365240
# like S <: T (we still erase return type since it's ultimately unknown).

mypy/main.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -995,9 +995,13 @@ def add_invertible_flag(
995995
help="Use a custom typing module",
996996
)
997997
internals_group.add_argument(
998-
"--new-type-inference",
998+
"--old-type-inference",
999999
action="store_true",
1000-
help="Enable new experimental type inference algorithm",
1000+
help="Disable new experimental type inference algorithm",
1001+
)
1002+
# Deprecated reverse variant of the above.
1003+
internals_group.add_argument(
1004+
"--new-type-inference", action="store_true", help=argparse.SUPPRESS
10011005
)
10021006
parser.add_argument(
10031007
"--enable-incomplete-feature",
@@ -1383,6 +1387,12 @@ def set_strict_flags() -> None:
13831387
if options.logical_deps:
13841388
options.cache_fine_grained = True
13851389

1390+
if options.new_type_inference:
1391+
print(
1392+
"Warning: --new-type-inference flag is deprecated;"
1393+
" new type inference algorithm is already enabled by default"
1394+
)
1395+
13861396
if options.strict_concatenate and not strict_option_set:
13871397
print("Warning: --strict-concatenate is deprecated; use --extra-checks instead")
13881398

mypy/options.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class BuildType:
6262
| {
6363
"platform",
6464
"bazel",
65-
"new_type_inference",
65+
"old_type_inference",
6666
"plugins",
6767
"disable_bytearray_promotion",
6868
"disable_memoryview_promotion",
@@ -360,7 +360,9 @@ def __init__(self) -> None:
360360
# skip most errors after this many messages have been reported.
361361
# -1 means unlimited.
362362
self.many_errors_threshold = defaults.MANY_ERRORS_THRESHOLD
363-
# Enable new experimental type inference algorithm.
363+
# Disable new experimental type inference algorithm.
364+
self.old_type_inference = False
365+
# Deprecated reverse version of the above, do not use.
364366
self.new_type_inference = False
365367
# Export line-level, limited, fine-grained dependency information in cache data
366368
# (undocumented feature).

mypy_self_check.ini

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ always_false = MYPYC
88
plugins = mypy.plugins.proper_plugin
99
python_version = 3.8
1010
exclude = mypy/typeshed/|mypyc/test-data/|mypyc/lib-rt/
11-
new_type_inference = True
1211
enable_error_code = ignore-without-code,redundant-expr
1312
enable_incomplete_feature = PreciseTupleTypes
1413
show_error_code_links = True

test-data/unit/check-generics.test

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2724,7 +2724,7 @@ def f(x: Callable[[G[T]], int]) -> T: ...
27242724
class G(Generic[T]):
27252725
def g(self, x: S) -> Union[S, T]: ...
27262726

2727-
f(lambda x: x.g(0)) # E: Incompatible return value type (got "Union[int, T]", expected "int")
2727+
reveal_type(f(lambda x: x.g(0))) # N: Revealed type is "builtins.int"
27282728

27292729
[case testDictStarInference]
27302730
class B: ...
@@ -3059,14 +3059,19 @@ def dec5(f: Callable[[int], T]) -> Callable[[int], List[T]]:
30593059
return [f(x)] * x
30603060
return g
30613061

3062+
I = TypeVar("I", bound=int)
3063+
def dec4_bound(f: Callable[[I], List[T]]) -> Callable[[I], T]:
3064+
...
3065+
30623066
reveal_type(dec1(lambda x: x)) # N: Revealed type is "def [T] (T`3) -> builtins.list[T`3]"
30633067
reveal_type(dec2(lambda x: x)) # N: Revealed type is "def [S] (S`4) -> builtins.list[S`4]"
30643068
reveal_type(dec3(lambda x: x[0])) # N: Revealed type is "def [S] (S`6) -> S`6"
30653069
reveal_type(dec4(lambda x: [x])) # N: Revealed type is "def [S] (S`9) -> S`9"
30663070
reveal_type(dec1(lambda x: 1)) # N: Revealed type is "def (builtins.int) -> builtins.list[builtins.int]"
30673071
reveal_type(dec5(lambda x: x)) # N: Revealed type is "def (builtins.int) -> builtins.list[builtins.int]"
30683072
reveal_type(dec3(lambda x: x)) # N: Revealed type is "def [S] (S`16) -> builtins.list[S`16]"
3069-
dec4(lambda x: x) # E: Incompatible return value type (got "S", expected "List[object]")
3073+
reveal_type(dec4(lambda x: x)) # N: Revealed type is "def [T] (builtins.list[T`19]) -> T`19"
3074+
dec4_bound(lambda x: x) # E: Value of type variable "I" of "dec4_bound" cannot be "List[T]"
30703075
[builtins fixtures/list.pyi]
30713076

30723077
[case testInferenceAgainstGenericParamSpecBasicInList]

test-data/unit/check-inference-context.test

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,6 +1305,25 @@ def g(l: List[C], x: str) -> Optional[C]:
13051305
return f(l, lambda c: reveal_type(c).x) # N: Revealed type is "__main__.C"
13061306
[builtins fixtures/list.pyi]
13071307

1308+
[case testPartialTypeContextWithTwoLambdas]
1309+
from typing import Any, Generic, TypeVar, Callable
1310+
1311+
def int_to_any(x: int) -> Any: ...
1312+
def any_to_int(x: Any) -> int: ...
1313+
def any_to_str(x: Any) -> str: ...
1314+
1315+
T = TypeVar("T")
1316+
class W(Generic[T]):
1317+
def __init__(
1318+
self, serialize: Callable[[T], Any], deserialize: Callable[[Any], T]
1319+
) -> None:
1320+
...
1321+
reveal_type(W(lambda x: int_to_any(x), lambda x: any_to_int(x))) # N: Revealed type is "__main__.W[builtins.int]"
1322+
W(
1323+
lambda x: int_to_any(x), # E: Argument 1 to "int_to_any" has incompatible type "str"; expected "int"
1324+
lambda x: any_to_str(x)
1325+
)
1326+
13081327
[case testWideOuterContextEmpty]
13091328
from typing import List, TypeVar
13101329

test-data/unit/cmdline.test

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,6 +1472,14 @@ note: A user-defined top-level module with name "typing" is not supported
14721472
Failed to find builtin module mypy_extensions, perhaps typeshed is broken?
14731473
== Return code: 2
14741474

1475+
[case testNewTypeInferenceFlagDeprecated]
1476+
# cmd: mypy --new-type-inference a.py
1477+
[file a.py]
1478+
pass
1479+
[out]
1480+
Warning: --new-type-inference flag is deprecated; new type inference algorithm is already enabled by default
1481+
== Return code: 0
1482+
14751483
[case testNotesOnlyResultInExitSuccess]
14761484
# cmd: mypy a.py
14771485
[file a.py]

0 commit comments

Comments
 (0)