diff --git a/mypy/main.py b/mypy/main.py index dd9a9c42c568..40984fe6e542 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -1184,7 +1184,7 @@ def add_invertible_flag( parser.add_argument("--test-env", action="store_true", help=argparse.SUPPRESS) # --local-partial-types disallows partial types spanning module top level and a function # (implicitly defined in fine-grained incremental mode) - parser.add_argument("--local-partial-types", action="store_true", help=argparse.SUPPRESS) + add_invertible_flag("--local-partial-types", default=False, help=argparse.SUPPRESS) # --logical-deps adds some more dependencies that are not semantically needed, but # may be helpful to determine relative importance of classes and functions for overall # type precision in a code base. It also _removes_ some deps, so this flag should be never diff --git a/test-data/unit/check-bound.test b/test-data/unit/check-bound.test index 1c713fd77c38..1f9eba612020 100644 --- a/test-data/unit/check-bound.test +++ b/test-data/unit/check-bound.test @@ -46,7 +46,7 @@ z = G(B()) [case testBoundVoid] -# flags: --no-strict-optional +# flags: --no-strict-optional --no-local-partial-types from typing import TypeVar, Generic T = TypeVar('T', bound=int) class C(Generic[T]): @@ -75,7 +75,7 @@ z: C [case testBoundHigherOrderWithVoid] -# flags: --no-strict-optional +# flags: --no-strict-optional --no-local-partial-types from typing import TypeVar, Callable class A: pass T = TypeVar('T', bound=A) diff --git a/test-data/unit/check-classes.test b/test-data/unit/check-classes.test index 5ccb9fa06c34..618b2c7a40c9 100644 --- a/test-data/unit/check-classes.test +++ b/test-data/unit/check-classes.test @@ -113,7 +113,7 @@ A().f = None # E: Cannot assign to a method \ from typing import Protocol class Base: - __hash__ = None + __hash__: None = None class Derived(Base): def __hash__(self) -> int: # E: Signature of "__hash__" incompatible with supertype "Base" \ diff --git a/test-data/unit/check-columns.test b/test-data/unit/check-columns.test index 8bb768cfe13b..940e0846c959 100644 --- a/test-data/unit/check-columns.test +++ b/test-data/unit/check-columns.test @@ -210,6 +210,7 @@ y: Dict[int, int] = { [builtins fixtures/dict.pyi] [case testColumnCannotDetermineType] +# flags: --no-local-partial-types (x) # E:2: Cannot determine type of "x" # E:2: Name "x" is used before definition x = None diff --git a/test-data/unit/check-custom-plugin.test b/test-data/unit/check-custom-plugin.test index 1e06f300570e..01facb63c6a6 100644 --- a/test-data/unit/check-custom-plugin.test +++ b/test-data/unit/check-custom-plugin.test @@ -1007,7 +1007,7 @@ reveal_type(Cls.attr) # N: Revealed type is "builtins.int" plugins=/test-data/unit/plugins/class_attr_hook.py [case testClassAttrPluginPartialType] -# flags: --config-file tmp/mypy.ini +# flags: --config-file tmp/mypy.ini --no-local-partial-types class Cls: attr = None diff --git a/test-data/unit/check-errorcodes.test b/test-data/unit/check-errorcodes.test index a5a22cb6cabd..294038664415 100644 --- a/test-data/unit/check-errorcodes.test +++ b/test-data/unit/check-errorcodes.test @@ -837,7 +837,7 @@ Foo = TypedDict("Bar", {}) # E: First argument "Bar" to TypedDict() does not ma [builtins fixtures/dict.pyi] [case testTruthyBool] -# flags: --enable-error-code truthy-bool +# flags: --enable-error-code truthy-bool --no-local-partial-types from typing import List, Union, Any class Foo: diff --git a/test-data/unit/check-incremental.test b/test-data/unit/check-incremental.test index 55360f15f5c5..77170280ecae 100644 --- a/test-data/unit/check-incremental.test +++ b/test-data/unit/check-incremental.test @@ -6317,6 +6317,7 @@ class C: ... [out3] [case testNoCrashOnPartialLambdaInference] +# flags: --no-local-partial-types import m [file m.py] from typing import TypeVar, Callable diff --git a/test-data/unit/check-inference.test b/test-data/unit/check-inference.test index 560092ed1a43..0da1c092efe8 100644 --- a/test-data/unit/check-inference.test +++ b/test-data/unit/check-inference.test @@ -1728,12 +1728,14 @@ b[{}] = 1 [builtins fixtures/dict.pyi] [case testInferDictInitializedToEmptyAndUpdatedFromMethod] +# flags: --no-local-partial-types map = {} def add() -> None: map[1] = 2 [builtins fixtures/dict.pyi] [case testInferDictInitializedToEmptyAndUpdatedFromMethodUnannotated] +# flags: --no-local-partial-types map = {} def add(): map[1] = 2 @@ -1921,6 +1923,7 @@ reveal_type(C().a) # N: Revealed type is "builtins.dict[Any, Any]" [builtins fixtures/dict.pyi] [case testInferAttributeInitializedToNoneAndAssignedClassBody] +# flags: --no-local-partial-types class C: a = None def __init__(self) -> None: @@ -2069,6 +2072,7 @@ x = 1 [out] [case testPartiallyInitializedVariableDoesNotEscapeScope2] +# flags: --no-local-partial-types x = None def f() -> None: x = None @@ -2114,36 +2118,32 @@ class C: -- ------------------------ [case testPartialTypeErrorSpecialCase1] +# flags: --no-local-partial-types # This used to crash. class A: x = None def f(self) -> None: - for a in self.x: + for a in self.x: # E: "None" has no attribute "__iter__" (not iterable) pass [builtins fixtures/for.pyi] -[out] -main:5: error: "None" has no attribute "__iter__" (not iterable) [case testPartialTypeErrorSpecialCase2] # This used to crash. class A: - x = [] + x = [] # E: Need type annotation for "x" (hint: "x: List[] = ...") def f(self) -> None: for a in self.x: pass [builtins fixtures/for.pyi] -[out] -main:3: error: Need type annotation for "x" (hint: "x: List[] = ...") [case testPartialTypeErrorSpecialCase3] +# flags: --no-local-partial-types class A: x = None def f(self) -> None: - for a in A.x: + for a in A.x: # E: "None" has no attribute "__iter__" (not iterable) pass [builtins fixtures/for.pyi] -[out] -main:4: error: "None" has no attribute "__iter__" (not iterable) [case testPartialTypeErrorSpecialCase4] # This used to crash. @@ -2492,6 +2492,7 @@ main:4: error: Unsupported target for indexed assignment ("Type[C[T]]") main:4: error: Invalid type: try using Literal[0] instead? [case testNoCrashOnPartialMember] +# flags: --no-local-partial-types class C: x = None def __init__(self) -> None: @@ -2512,6 +2513,7 @@ reveal_type(x) # N: Revealed type is "builtins.str" [out] [case testNoCrashOnPartialVariable2] +# flags: --no-local-partial-types from typing import Tuple, TypeVar T = TypeVar('T', bound=str) diff --git a/test-data/unit/check-narrowing.test b/test-data/unit/check-narrowing.test index ad59af01010c..ac6c6436ba8d 100644 --- a/test-data/unit/check-narrowing.test +++ b/test-data/unit/check-narrowing.test @@ -2354,6 +2354,7 @@ def fn_while(arg: T) -> None: [builtins fixtures/primitives.pyi] [case testRefinePartialTypeWithinLoop] +# flags: --no-local-partial-types x = None for _ in range(2): diff --git a/test-data/unit/check-optional.test b/test-data/unit/check-optional.test index 683ce0446915..c14b6ae376ae 100644 --- a/test-data/unit/check-optional.test +++ b/test-data/unit/check-optional.test @@ -321,10 +321,13 @@ def f() -> Generator[None, None, None]: [out] [case testNoneAndStringIsNone] -a = None +a: None = None b = "foo" reveal_type(a and b) # N: Revealed type is "None" +c = None +reveal_type(c and b) # N: Revealed type is "None" + [case testNoneMatchesObjectInOverload] import a a.f(None) @@ -581,7 +584,7 @@ x is not None and x + '42' # E: Unsupported operand types for + ("int" and "str [case testInvalidBooleanBranchIgnored] from typing import Optional -x = None +x: None = None x is not None and x + 42 [builtins fixtures/isinstance.pyi] diff --git a/test-data/unit/check-protocols.test b/test-data/unit/check-protocols.test index 0571c1729302..ed8edea5f0d5 100644 --- a/test-data/unit/check-protocols.test +++ b/test-data/unit/check-protocols.test @@ -2906,6 +2906,7 @@ hs(None) [case testPartialTypeProtocol] +# flags: --no-local-partial-types from typing import Protocol class Flapper(Protocol): @@ -2944,7 +2945,7 @@ class DataArray(ObjectHashable): [case testPartialAttributeNoneType] -# flags: --no-strict-optional +# flags: --no-strict-optional --no-local-partial-types from typing import Optional, Protocol, runtime_checkable @runtime_checkable @@ -2962,6 +2963,7 @@ class MyClass: [case testPartialAttributeNoneTypeStrictOptional] +# flags: --no-local-partial-types from typing import Optional, Protocol, runtime_checkable @runtime_checkable diff --git a/test-data/unit/deps.test b/test-data/unit/deps.test index 757bd9541fc9..1aa025579535 100644 --- a/test-data/unit/deps.test +++ b/test-data/unit/deps.test @@ -597,6 +597,7 @@ class C: -> m.C.__init__ [case testPartialNoneTypeAttributeCrash1] +# flags: --no-local-partial-types class C: pass class A: @@ -612,6 +613,7 @@ class A: -> , m.A.f, m.C [case testPartialNoneTypeAttributeCrash2] +# flags: --no-local-partial-types class C: pass class A: