Skip to content

Commit 058f4ca

Browse files
committed
Actually error on --no-local-parial-types in parallel mode
1 parent fe45691 commit 058f4ca

11 files changed

Lines changed: 92 additions & 83 deletions

‎mypy/build.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,7 @@ def __init__(
898898
self.errors,
899899
self.plugin,
900900
self.import_map,
901+
parallel_worker,
901902
)
902903
self.all_types: dict[Expression, Type] = {} # Enabled by export_types
903904
self.indirection_detector = TypeIndirectionVisitor()

‎mypy/semanal.py‎

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,7 @@ def __init__(
461461
errors: Errors,
462462
plugin: Plugin,
463463
import_map: dict[str, set[str]],
464+
parallel_worker: bool,
464465
) -> None:
465466
"""Construct semantic analyzer.
466467
@@ -494,6 +495,8 @@ def __init__(
494495
self.errors = errors
495496
self.modules = modules
496497
self.import_map = import_map
498+
# True if this analysis is run in a parallel worker process.
499+
self.parallel_worker = parallel_worker
497500
self.msg = MessageBuilder(errors, modules)
498501
self.missing_modules = missing_modules
499502
self.missing_names = [set()]
@@ -719,24 +722,26 @@ def refresh_partial(
719722
self.accept(node)
720723
del self.patches
721724

725+
def ad_hoc_error(self, msg: str) -> None:
726+
n = TempNode(AnyType(TypeOfAny.special_form))
727+
n.line = 1
728+
n.column = 0
729+
n.end_line = 1
730+
n.end_column = 0
731+
self.fail(msg, n)
732+
722733
def refresh_top_level(self, file_node: MypyFile) -> None:
723734
"""Reanalyze a stale module top-level in fine-grained incremental mode."""
724735
if self.options.allow_redefinition and not self.options.local_partial_types:
725-
n = TempNode(AnyType(TypeOfAny.special_form))
726-
n.line = 1
727-
n.column = 0
728-
n.end_line = 1
729-
n.end_column = 0
730-
self.fail("--local-partial-types must be enabled if using --allow-redefinition", n)
736+
self.ad_hoc_error(
737+
"--local-partial-types must be enabled if using --allow-redefinition"
738+
)
731739
if self.options.allow_redefinition and self.options.allow_redefinition_old:
732-
n = TempNode(AnyType(TypeOfAny.special_form))
733-
n.line = 1
734-
n.column = 0
735-
n.end_line = 1
736-
n.end_column = 0
737-
self.fail(
738-
"--allow-redefinition-old and --allow-redefinition should not be used together", n
740+
self.ad_hoc_error(
741+
"--allow-redefinition-old and --allow-redefinition should not be used together"
739742
)
743+
if not self.options.local_partial_types and self.parallel_worker:
744+
self.ad_hoc_error("--local-partial-types must be enabled in parallel mode")
740745
self.recurse_into_functions = False
741746
self.add_implicit_module_attrs(file_node)
742747
for d in file_node.defs:

‎test-data/unit/check-bound.test‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ z = G(B())
4646

4747

4848
[case testBoundVoid]
49-
# flags: --no-strict-optional --no-local-partial-types
49+
# flags: --no-strict-optional
5050
from typing import TypeVar, Generic
5151
T = TypeVar('T', bound=int)
5252
class C(Generic[T]):
@@ -75,7 +75,7 @@ z: C
7575

7676

7777
[case testBoundHigherOrderWithVoid]
78-
# flags: --no-strict-optional --no-local-partial-types
78+
# flags: --no-strict-optional
7979
from typing import TypeVar, Callable
8080
class A: pass
8181
T = TypeVar('T', bound=A)

‎test-data/unit/check-columns.test‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,6 @@ y: Dict[int, int] = {
209209
[builtins fixtures/dict.pyi]
210210

211211
[case testColumnCannotDetermineType]
212-
# flags: --no-local-partial-types
213212
(x) # E:2: Cannot determine type of "x" # E:2: Name "x" is used before definition
214213
x = None
215214

‎test-data/unit/check-custom-plugin.test‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,7 @@ reveal_type(Cls.attr) # N: Revealed type is "builtins.int"
10071007
plugins=<ROOT>/test-data/unit/plugins/class_attr_hook.py
10081008

10091009
[case testClassAttrPluginPartialType]
1010-
# flags: --config-file tmp/mypy.ini --no-local-partial-types
1010+
# flags: --config-file tmp/mypy.ini
10111011

10121012
class Cls:
10131013
attr = None

‎test-data/unit/check-errorcodes.test‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -981,7 +981,7 @@ Foo = TypedDict("Bar", {}) # E: First argument "Bar" to TypedDict() does not ma
981981
[typing fixtures/typing-typeddict.pyi]
982982

983983
[case testTruthyBool]
984-
# flags: --enable-error-code truthy-bool --no-local-partial-types
984+
# flags: --enable-error-code truthy-bool
985985
from typing import List, Union, Any
986986

987987
class Foo:

‎test-data/unit/check-functions.test‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2664,7 +2664,7 @@ reveal_type(bar(None)) # N: Revealed type is "None"
26642664
[out]
26652665

26662666
[case testNoComplainInferredNone]
2667-
# flags: --no-strict-optional --no-local-partial-types
2667+
# flags: --no-strict-optional
26682668
from typing import TypeVar, Optional
26692669
T = TypeVar('T')
26702670
def X(val: T) -> T: ...

‎test-data/unit/check-incremental.test‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6639,7 +6639,7 @@ class C: ...
66396639
[out2]
66406640
[out3]
66416641

6642-
[case testNoCrashOnPartialLambdaInference]
6642+
[case testNoCrashOnPartialLambdaInference_no_parallel]
66436643
# flags: --no-local-partial-types
66446644
import m
66456645
[file m.py]
@@ -6666,6 +6666,37 @@ reveal_type(xs)
66666666
[out2]
66676667
tmp/m.py:9: note: Revealed type is "builtins.list[builtins.int]"
66686668

6669+
[case testNoCrashOnPartialLambdaInference_parallel_only]
6670+
# flags: --local-partial-types
6671+
import m
6672+
[file m.py]
6673+
from typing import TypeVar, Callable
6674+
6675+
V = TypeVar("V")
6676+
def apply(val: V, func: Callable[[V], None]) -> None:
6677+
return func(val)
6678+
6679+
xs = []
6680+
apply(0, lambda a: xs.append(a))
6681+
[file m.py.2]
6682+
from typing import TypeVar, Callable
6683+
6684+
V = TypeVar("V")
6685+
def apply(val: V, func: Callable[[V], None]) -> None:
6686+
return func(val)
6687+
6688+
xs = []
6689+
apply(0, lambda a: xs.append(a))
6690+
reveal_type(xs)
6691+
[builtins fixtures/list.pyi]
6692+
[out]
6693+
tmp/m.py:7: error: Need type annotation for "xs" (hint: "xs: list[<type>] = ...")
6694+
tmp/m.py:8: error: Cannot determine type of "xs"
6695+
[out2]
6696+
tmp/m.py:7: error: Need type annotation for "xs" (hint: "xs: list[<type>] = ...")
6697+
tmp/m.py:8: error: Cannot determine type of "xs"
6698+
tmp/m.py:9: note: Revealed type is "builtins.list[Any]"
6699+
66696700
[case testTypingSelfCoarse]
66706701
import m
66716702
[file lib.py]

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

Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1925,7 +1925,6 @@ reveal_type(C().a) # N: Revealed type is "builtins.dict[Any, Any]"
19251925
[builtins fixtures/dict.pyi]
19261926

19271927
[case testInferAttributeInitializedToNoneAndAssignedClassBody]
1928-
# flags: --no-local-partial-types
19291928
class C:
19301929
a = None
19311930
def __init__(self) -> None:
@@ -2099,7 +2098,6 @@ x = 1
20992098
[out]
21002099

21012100
[case testPartiallyInitializedVariableDoesNotEscapeScope2]
2102-
# flags: --no-local-partial-types
21032101
x = None
21042102
def f() -> None:
21052103
x = None
@@ -2127,22 +2125,6 @@ main:6: error: Incompatible types in assignment (expression has type "int", vari
21272125
main:7: error: "None" not callable
21282126

21292127
[case testGlobalInitializedToNoneSetFromFunction]
2130-
# flags: --no-local-partial-types
2131-
a = None
2132-
def f() -> None:
2133-
global a
2134-
a = 42
2135-
reveal_type(a) # N: Revealed type is "builtins.int"
2136-
reveal_type(a) # N: Revealed type is "builtins.int | None"
2137-
2138-
b = None
2139-
def unchecked():
2140-
global b
2141-
b = 42
2142-
reveal_type(b) # N: Revealed type is "Any | None"
2143-
2144-
[case testGlobalInitializedToNoneSetFromFunctionLocalPartialTypes]
2145-
# flags: --local-partial-types
21462128
a = None
21472129
def f() -> None:
21482130
global a
@@ -2157,23 +2139,6 @@ def unchecked():
21572139
reveal_type(b) # N: Revealed type is "Any | None"
21582140

21592141
[case testGlobalInitializedToNoneSetFromMethod]
2160-
# flags: --no-local-partial-types
2161-
a = None
2162-
class C:
2163-
def m(self) -> None:
2164-
global a
2165-
a = 42
2166-
reveal_type(a) # N: Revealed type is "builtins.int | None"
2167-
2168-
b = None
2169-
class CC:
2170-
def unchecked(self):
2171-
global b
2172-
b = 42
2173-
reveal_type(b) # N: Revealed type is "Any | None"
2174-
2175-
[case testGlobalInitializedToNoneSetFromMethodLocalPartialTypes]
2176-
# flags: --local-partial-types
21772142
a = None
21782143
class C:
21792144
def m(self) -> None:
@@ -2189,7 +2154,6 @@ class CC:
21892154
reveal_type(b) # N: Revealed type is "Any | None"
21902155

21912156
[case testPartialTypeErrorSpecialCase1]
2192-
# flags: --no-local-partial-types
21932157
# This used to crash.
21942158
class A:
21952159
x = None
@@ -2208,7 +2172,6 @@ class A:
22082172
[builtins fixtures/for.pyi]
22092173

22102174
[case testPartialTypeErrorSpecialCase3]
2211-
# flags: --no-local-partial-types
22122175
class A:
22132176
x = None
22142177
def f(self) -> None:
@@ -2561,7 +2524,6 @@ main:4: error: Unsupported target for indexed assignment ("type[C[T]]")
25612524
main:4: error: Invalid type: try using Literal[0] instead?
25622525

25632526
[case testNoCrashOnPartialMember]
2564-
# flags: --no-local-partial-types
25652527
class C:
25662528
x = None
25672529
def __init__(self) -> None:
@@ -2580,7 +2542,6 @@ reveal_type(x) # N: Revealed type is "builtins.str"
25802542
[builtins fixtures/tuple.pyi]
25812543

25822544
[case testNoCrashOnPartialVariable2]
2583-
# flags: --no-local-partial-types
25842545
from typing import Tuple, TypeVar
25852546
T = TypeVar('T', bound=str)
25862547

@@ -4364,7 +4325,7 @@ def f() -> None:
43644325
global x
43654326
x
43664327

4367-
[case testPerFileNoLocalPartialTypesForcesSinglePhase]
4328+
[case testPerFileNoLocalPartialTypesForcesSinglePhase_no_parallel]
43684329
# mypy: no-local-partial-types
43694330

43704331
x = []
@@ -4374,3 +4335,15 @@ def func() -> None:
43744335

43754336
reveal_type(x) # N: Revealed type is "builtins.list[builtins.int]"
43764337
[builtins fixtures/list.pyi]
4338+
4339+
[case testPerFileNoLocalPartialTypesForcesSinglePhase_parallel_only]
4340+
# mypy: no-local-partial-types
4341+
4342+
x = []
4343+
4344+
def func() -> None:
4345+
x.append(1)
4346+
[builtins fixtures/list.pyi]
4347+
[out]
4348+
main:1: error: --local-partial-types must be enabled in parallel mode
4349+
main:3: error: Need type annotation for "x" (hint: "x: list[<type>] = ...")

‎test-data/unit/check-protocols.test‎

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2920,7 +2920,6 @@ hs(None)
29202920

29212921

29222922
[case testPartialTypeProtocol]
2923-
# flags: --no-local-partial-types
29242923
from typing import Protocol
29252924

29262925
class Flapper(Protocol):
@@ -2941,25 +2940,7 @@ class Gleemer:
29412940

29422941

29432942
[case testPartialTypeProtocolHashable]
2944-
# flags: --no-strict-optional --no-local-partial-types
2945-
from typing import Protocol
2946-
2947-
class Hashable(Protocol):
2948-
def __hash__(self) -> int: ...
2949-
2950-
class ObjectHashable:
2951-
def __hash__(self) -> int: ...
2952-
2953-
class DataArray(ObjectHashable):
2954-
__hash__ = None
2955-
2956-
def f(self, x: Hashable) -> None:
2957-
reveal_type([self, x]) # N: Revealed type is "builtins.list[builtins.object]"
2958-
[builtins fixtures/tuple.pyi]
2959-
2960-
2961-
[case testPartialTypeProtocolHashableLocalPartialTypes]
2962-
# flags: --no-strict-optional --local-partial-types
2943+
# flags: --no-strict-optional
29632944
from typing import Protocol
29642945

29652946
class Hashable(Protocol):
@@ -2977,7 +2958,7 @@ class DataArray(ObjectHashable):
29772958

29782959

29792960
[case testPartialAttributeNoneType]
2980-
# flags: --no-strict-optional --no-local-partial-types
2961+
# flags: --no-strict-optional
29812962
from typing import Optional, Protocol, runtime_checkable
29822963

29832964
@runtime_checkable
@@ -2995,7 +2976,6 @@ class MyClass:
29952976

29962977

29972978
[case testPartialAttributeNoneTypeStrictOptional]
2998-
# flags: --no-local-partial-types
29992979
from typing import Optional, Protocol, runtime_checkable
30002980

30012981
@runtime_checkable

0 commit comments

Comments
 (0)