Skip to content

Commit b2b32e7

Browse files
authored
Allow inverting --local-partial-types (#18377)
Also add it to a bunch of test cases where it is needed
1 parent 55d4c17 commit b2b32e7

12 files changed

+30
-18
lines changed

mypy/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1185,7 +1185,7 @@ def add_invertible_flag(
11851185
parser.add_argument("--test-env", action="store_true", help=argparse.SUPPRESS)
11861186
# --local-partial-types disallows partial types spanning module top level and a function
11871187
# (implicitly defined in fine-grained incremental mode)
1188-
parser.add_argument("--local-partial-types", action="store_true", help=argparse.SUPPRESS)
1188+
add_invertible_flag("--local-partial-types", default=False, help=argparse.SUPPRESS)
11891189
# --logical-deps adds some more dependencies that are not semantically needed, but
11901190
# may be helpful to determine relative importance of classes and functions for overall
11911191
# type precision in a code base. It also _removes_ some deps, so this flag should be never

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
49+
# flags: --no-strict-optional --no-local-partial-types
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
78+
# flags: --no-strict-optional --no-local-partial-types
7979
from typing import TypeVar, Callable
8080
class A: pass
8181
T = TypeVar('T', bound=A)

test-data/unit/check-classes.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ A().f = None # E: Cannot assign to a method \
113113
from typing import Protocol
114114

115115
class Base:
116-
__hash__ = None
116+
__hash__: None = None
117117

118118
class Derived(Base):
119119
def __hash__(self) -> int: # E: Signature of "__hash__" incompatible with supertype "Base" \

test-data/unit/check-columns.test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ y: Dict[int, int] = {
210210
[builtins fixtures/dict.pyi]
211211

212212
[case testColumnCannotDetermineType]
213+
# flags: --no-local-partial-types
213214
(x) # E:2: Cannot determine type of "x" # E:2: Name "x" is used before definition
214215
x = None
215216

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
1010+
# flags: --config-file tmp/mypy.ini --no-local-partial-types
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
@@ -837,7 +837,7 @@ Foo = TypedDict("Bar", {}) # E: First argument "Bar" to TypedDict() does not ma
837837
[builtins fixtures/dict.pyi]
838838

839839
[case testTruthyBool]
840-
# flags: --enable-error-code truthy-bool
840+
# flags: --enable-error-code truthy-bool --no-local-partial-types
841841
from typing import List, Union, Any
842842

843843
class Foo:

test-data/unit/check-incremental.test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6317,6 +6317,7 @@ class C: ...
63176317
[out3]
63186318

63196319
[case testNoCrashOnPartialLambdaInference]
6320+
# flags: --no-local-partial-types
63206321
import m
63216322
[file m.py]
63226323
from typing import TypeVar, Callable

test-data/unit/check-inference.test

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1728,12 +1728,14 @@ b[{}] = 1
17281728
[builtins fixtures/dict.pyi]
17291729

17301730
[case testInferDictInitializedToEmptyAndUpdatedFromMethod]
1731+
# flags: --no-local-partial-types
17311732
map = {}
17321733
def add() -> None:
17331734
map[1] = 2
17341735
[builtins fixtures/dict.pyi]
17351736

17361737
[case testInferDictInitializedToEmptyAndUpdatedFromMethodUnannotated]
1738+
# flags: --no-local-partial-types
17371739
map = {}
17381740
def add():
17391741
map[1] = 2
@@ -1921,6 +1923,7 @@ reveal_type(C().a) # N: Revealed type is "builtins.dict[Any, Any]"
19211923
[builtins fixtures/dict.pyi]
19221924

19231925
[case testInferAttributeInitializedToNoneAndAssignedClassBody]
1926+
# flags: --no-local-partial-types
19241927
class C:
19251928
a = None
19261929
def __init__(self) -> None:
@@ -2069,6 +2072,7 @@ x = 1
20692072
[out]
20702073

20712074
[case testPartiallyInitializedVariableDoesNotEscapeScope2]
2075+
# flags: --no-local-partial-types
20722076
x = None
20732077
def f() -> None:
20742078
x = None
@@ -2114,36 +2118,32 @@ class C:
21142118
-- ------------------------
21152119

21162120
[case testPartialTypeErrorSpecialCase1]
2121+
# flags: --no-local-partial-types
21172122
# This used to crash.
21182123
class A:
21192124
x = None
21202125
def f(self) -> None:
2121-
for a in self.x:
2126+
for a in self.x: # E: "None" has no attribute "__iter__" (not iterable)
21222127
pass
21232128
[builtins fixtures/for.pyi]
2124-
[out]
2125-
main:5: error: "None" has no attribute "__iter__" (not iterable)
21262129

21272130
[case testPartialTypeErrorSpecialCase2]
21282131
# This used to crash.
21292132
class A:
2130-
x = []
2133+
x = [] # E: Need type annotation for "x" (hint: "x: List[<type>] = ...")
21312134
def f(self) -> None:
21322135
for a in self.x:
21332136
pass
21342137
[builtins fixtures/for.pyi]
2135-
[out]
2136-
main:3: error: Need type annotation for "x" (hint: "x: List[<type>] = ...")
21372138

21382139
[case testPartialTypeErrorSpecialCase3]
2140+
# flags: --no-local-partial-types
21392141
class A:
21402142
x = None
21412143
def f(self) -> None:
2142-
for a in A.x:
2144+
for a in A.x: # E: "None" has no attribute "__iter__" (not iterable)
21432145
pass
21442146
[builtins fixtures/for.pyi]
2145-
[out]
2146-
main:4: error: "None" has no attribute "__iter__" (not iterable)
21472147

21482148
[case testPartialTypeErrorSpecialCase4]
21492149
# This used to crash.
@@ -2492,6 +2492,7 @@ main:4: error: Unsupported target for indexed assignment ("Type[C[T]]")
24922492
main:4: error: Invalid type: try using Literal[0] instead?
24932493

24942494
[case testNoCrashOnPartialMember]
2495+
# flags: --no-local-partial-types
24952496
class C:
24962497
x = None
24972498
def __init__(self) -> None:
@@ -2512,6 +2513,7 @@ reveal_type(x) # N: Revealed type is "builtins.str"
25122513
[out]
25132514

25142515
[case testNoCrashOnPartialVariable2]
2516+
# flags: --no-local-partial-types
25152517
from typing import Tuple, TypeVar
25162518
T = TypeVar('T', bound=str)
25172519

test-data/unit/check-narrowing.test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2354,6 +2354,7 @@ def fn_while(arg: T) -> None:
23542354
[builtins fixtures/primitives.pyi]
23552355

23562356
[case testRefinePartialTypeWithinLoop]
2357+
# flags: --no-local-partial-types
23572358

23582359
x = None
23592360
for _ in range(2):

test-data/unit/check-optional.test

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,10 +321,13 @@ def f() -> Generator[None, None, None]:
321321
[out]
322322

323323
[case testNoneAndStringIsNone]
324-
a = None
324+
a: None = None
325325
b = "foo"
326326
reveal_type(a and b) # N: Revealed type is "None"
327327

328+
c = None
329+
reveal_type(c and b) # N: Revealed type is "None"
330+
328331
[case testNoneMatchesObjectInOverload]
329332
import a
330333
a.f(None)
@@ -581,7 +584,7 @@ x is not None and x + '42' # E: Unsupported operand types for + ("int" and "str
581584
[case testInvalidBooleanBranchIgnored]
582585
from typing import Optional
583586

584-
x = None
587+
x: None = None
585588
x is not None and x + 42
586589
[builtins fixtures/isinstance.pyi]
587590

test-data/unit/check-protocols.test

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2906,6 +2906,7 @@ hs(None)
29062906

29072907

29082908
[case testPartialTypeProtocol]
2909+
# flags: --no-local-partial-types
29092910
from typing import Protocol
29102911

29112912
class Flapper(Protocol):
@@ -2944,7 +2945,7 @@ class DataArray(ObjectHashable):
29442945

29452946

29462947
[case testPartialAttributeNoneType]
2947-
# flags: --no-strict-optional
2948+
# flags: --no-strict-optional --no-local-partial-types
29482949
from typing import Optional, Protocol, runtime_checkable
29492950

29502951
@runtime_checkable
@@ -2962,6 +2963,7 @@ class MyClass:
29622963

29632964

29642965
[case testPartialAttributeNoneTypeStrictOptional]
2966+
# flags: --no-local-partial-types
29652967
from typing import Optional, Protocol, runtime_checkable
29662968

29672969
@runtime_checkable

test-data/unit/deps.test

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,7 @@ class C:
597597
<m.f> -> m.C.__init__
598598

599599
[case testPartialNoneTypeAttributeCrash1]
600+
# flags: --no-local-partial-types
600601
class C: pass
601602

602603
class A:
@@ -612,6 +613,7 @@ class A:
612613
<m.C> -> <m.A.x>, m.A.f, m.C
613614

614615
[case testPartialNoneTypeAttributeCrash2]
616+
# flags: --no-local-partial-types
615617
class C: pass
616618

617619
class A:

0 commit comments

Comments
 (0)