Skip to content

Enable strict optional for more test files (2) #15596

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions mypy/test/testcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,33 +59,20 @@
"check-dynamic-typing.test",
"check-enum.test",
"check-expressions.test",
"check-formatting.test",
"check-functions.test",
"check-generic-subtyping.test",
"check-generics.test",
"check-incremental.test",
"check-inference-context.test",
"check-inference.test",
"check-inline-config.test",
"check-isinstance.test",
"check-kwargs.test",
"check-lists.test",
"check-literal.test",
"check-modules.test",
"check-namedtuple.test",
"check-newsemanal.test",
"check-overloading.test",
"check-plugin-attrs.test",
"check-protocols.test",
"check-selftype.test",
"check-serialize.test",
"check-statements.test",
"check-super.test",
"check-tuples.test",
"check-type-aliases.test",
"check-type-checks.test",
"check-typeddict.test",
"check-typevar-values.test",
"check-unions.test",
"check-varargs.test",
}
Expand Down
15 changes: 11 additions & 4 deletions test-data/unit/check-formatting.test
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

[case testStringInterpolationType]
from typing import Tuple
i, f, s, t = None, None, None, None # type: (int, float, str, Tuple[int])
i: int
f: float
s: str
t: Tuple[int]
'%d' % i
'%f' % f
'%s' % s
Expand All @@ -21,7 +24,9 @@ i, f, s, t = None, None, None, None # type: (int, float, str, Tuple[int])

[case testStringInterpolationSAcceptsAnyType]
from typing import Any
i, o, s = None, None, None # type: (int, object, str)
i: int
o: object
s: str
'%s %s %s' % (i, o, s)
[builtins fixtures/primitives.pyi]

Expand Down Expand Up @@ -139,8 +144,10 @@ class BytesThing:
def __getitem__(self, __key: bytes) -> str:
...

a = None # type: Any
ds, do, di = None, None, None # type: Dict[str, int], Dict[object, int], Dict[int, int]
a: Any
ds: Dict[str, int]
do: Dict[object, int]
di: Dict[int, int]
'%(a)' % 1 # E: Format requires a mapping (expression has type "int", expected type for mapping is "SupportsKeysAndGetItem[str, Any]")
'%()d' % a
'%()d' % ds
Expand Down
8 changes: 4 additions & 4 deletions test-data/unit/check-incremental.test
Original file line number Diff line number Diff line change
Expand Up @@ -1734,12 +1734,12 @@ class R: pass
[file r/s.py]
from . import m
R = m.R
a = None # type: R
a: R

[file r/s.py.2]
from . import m
R = m.R
a = None # type: R
a: R

[case testIncrementalBaseClassAttributeConflict]
class A: pass
Expand Down Expand Up @@ -2603,7 +2603,7 @@ def output() -> IntNode[str]:
return Node(1, 'x')
x = output() # type: IntNode

y = None # type: IntNode
y: IntNode
y.x = 1
y.y = 1
y.y = 'x'
Expand All @@ -2625,7 +2625,7 @@ def output() -> IntNode[str]:
return Node(1, 'x')
x = output() # type: IntNode

y = None # type: IntNode
y: IntNode
y.x = 1
y.y = 1
y.y = 'x'
Expand Down
31 changes: 16 additions & 15 deletions test-data/unit/check-inline-config.test
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

# mypy: disallow-any-generics, no-warn-no-return

from typing import List
def foo() -> List: # E: Missing type parameters for generic type "List"
from typing import List, Optional
def foo() -> Optional[List]: # E: Missing type parameters for generic type "List"
20

[builtins fixtures/list.pyi]
Expand All @@ -15,8 +15,8 @@ def foo() -> List: # E: Missing type parameters for generic type "List"
# mypy: disallow-any-generics
# mypy: no-warn-no-return

from typing import List
def foo() -> List: # E: Missing type parameters for generic type "List"
from typing import List, Optional
def foo() -> Optional[List]: # E: Missing type parameters for generic type "List"
20

[builtins fixtures/list.pyi]
Expand All @@ -25,8 +25,8 @@ def foo() -> List: # E: Missing type parameters for generic type "List"

# mypy: disallow-any-generics=true, warn-no-return=0

from typing import List
def foo() -> List: # E: Missing type parameters for generic type "List"
from typing import List, Optional
def foo() -> Optional[List]: # E: Missing type parameters for generic type "List"
20

[builtins fixtures/list.pyi]
Expand All @@ -36,8 +36,8 @@ def foo() -> List: # E: Missing type parameters for generic type "List"

# mypy: disallow-any-generics = true, warn-no-return = 0

from typing import List
def foo() -> List: # E: Missing type parameters for generic type "List"
from typing import List, Optional
def foo() -> Optional[List]: # E: Missing type parameters for generic type "List"
20

[builtins fixtures/list.pyi]
Expand Down Expand Up @@ -84,20 +84,20 @@ import a
[file a.py]
# mypy: disallow-any-generics, no-warn-no-return

from typing import List
def foo() -> List:
from typing import List, Optional
def foo() -> Optional[List]:
20

[file a.py.2]
# mypy: no-warn-no-return

from typing import List
def foo() -> List:
from typing import List, Optional
def foo() -> Optional[List]:
20

[file a.py.3]
from typing import List
def foo() -> List:
from typing import List, Optional
def foo() -> Optional[List]:
20
[out]
tmp/a.py:4: error: Missing type parameters for generic type "List"
Expand Down Expand Up @@ -131,8 +131,9 @@ tmp/a.py:4: error: Missing type parameters for generic type "List"
import a, b
[file a.py]
# mypy: no-warn-no-return
from typing import Optional

def foo() -> int:
def foo() -> Optional[int]:
20

[file b.py]
Expand Down
20 changes: 15 additions & 5 deletions test-data/unit/check-lists.test
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@

[case testNestedListAssignment]
from typing import List
a1, b1, c1 = None, None, None # type: (A, B, C)
a2, b2, c2 = None, None, None # type: (A, B, C)
a1: A
a2: A
b1: B
b2: B
c1: C
c2: C

if int():
a1, [b1, c1] = a2, [b2, c2]
Expand All @@ -21,7 +25,9 @@ class C: pass

[case testNestedListAssignmentToTuple]
from typing import List
a, b, c = None, None, None # type: (A, B, C)
a: A
b: B
c: C

a, b = [a, b]
a, b = [a] # E: Need more than 1 value to unpack (2 expected)
Expand All @@ -35,7 +41,9 @@ class C: pass

[case testListAssignmentFromTuple]
from typing import List
a, b, c = None, None, None # type: (A, B, C)
a: A
b: B
c: C
t = a, b

if int():
Expand All @@ -55,7 +63,9 @@ class C: pass

[case testListAssignmentUnequalAmountToUnpack]
from typing import List
a, b, c = None, None, None # type: (A, B, C)
a: A
b: B
c: C

def f() -> None: # needed because test parser tries to parse [a, b] as section header
[a, b] = [a, b]
Expand Down
1 change: 1 addition & 0 deletions test-data/unit/check-newsemanal.test
Original file line number Diff line number Diff line change
Expand Up @@ -2149,6 +2149,7 @@ x: C
class C:
def frob(self, foos: Dict[Any, Foos]) -> None:
foo = foos.get(1)
assert foo
dict(foo)
[builtins fixtures/dict.pyi]

Expand Down
17 changes: 11 additions & 6 deletions test-data/unit/check-protocols.test
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,9 @@ class MyHashable(Protocol):
class C:
__my_hash__ = None

var: MyHashable = C() # E: Incompatible types in assignment (expression has type "C", variable has type "MyHashable")
var: MyHashable = C() # E: Incompatible types in assignment (expression has type "C", variable has type "MyHashable") \
# N: Following member(s) of "C" have conflicts: \
# N: __my_hash__: expected "Callable[[], int]", got "None"

[case testNoneDisablesProtocolSubclassingWithStrictOptional]
# flags: --strict-optional
Expand Down Expand Up @@ -1263,13 +1265,13 @@ if int():
[builtins fixtures/classmethod.pyi]

[case testOverloadedMethodsInProtocols]
from typing import overload, Protocol, Union
from typing import overload, Protocol, Union, Optional

class P(Protocol):
@overload
def f(self, x: int) -> int: pass
def f(self, x: int) -> Optional[int]: pass
@overload
def f(self, x: str) -> str: pass
def f(self, x: str) -> Optional[str]: pass

class C:
def f(self, x: Union[int, str]) -> None:
Expand All @@ -1286,9 +1288,9 @@ main:18: error: Incompatible types in assignment (expression has type "D", varia
main:18: note: Following member(s) of "D" have conflicts:
main:18: note: Expected:
main:18: note: @overload
main:18: note: def f(self, x: int) -> int
main:18: note: def f(self, x: int) -> Optional[int]
main:18: note: @overload
main:18: note: def f(self, x: str) -> str
main:18: note: def f(self, x: str) -> Optional[str]
main:18: note: Got:
main:18: note: def f(self, x: int) -> None

Expand Down Expand Up @@ -1441,6 +1443,7 @@ def g(x: P, y: P2) -> None: pass
reveal_type(f(g)) # N: Revealed type is "__main__.P2"

[case testMeetOfIncompatibleProtocols]
# flags: --no-strict-optional
from typing import Protocol, Callable, TypeVar

class P(Protocol):
Expand Down Expand Up @@ -1634,6 +1637,7 @@ f(Alias) # E: Only concrete class can be given where "Type[P]" is expected
f(GoodAlias)

[case testInstantiationProtocolInTypeForVariables]
# flags: --no-strict-optional
from typing import Type, Protocol

class P(Protocol):
Expand Down Expand Up @@ -2397,6 +2401,7 @@ x: P = None
[out]

[case testNoneSubtypeOfAllProtocolsWithoutStrictOptional]
# flags: --no-strict-optional
from typing import Protocol
class P(Protocol):
attr: int
Expand Down
9 changes: 5 additions & 4 deletions test-data/unit/check-selftype.test
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,8 @@ class Base(Protocol):

class TweakFunc:
def func(self: Base) -> int:
return reveal_type(super().func()) # N: Revealed type is "builtins.int"
return reveal_type(super().func()) # E: Call to abstract method "func" of "Base" with trivial body via super() is unsafe \
# N: Revealed type is "builtins.int"

class Good:
def func(self) -> int: ...
Expand Down Expand Up @@ -1217,14 +1218,14 @@ class AClass:
...

def foo(x: Type[AClass]) -> None:
reveal_type(x.delete) # N: Revealed type is "Overload(def (id: builtins.int, id2: builtins.int) -> builtins.int, def (id: __main__.AClass, id2: None =) -> builtins.int)"
reveal_type(x.delete) # N: Revealed type is "Overload(def (id: builtins.int, id2: builtins.int) -> Union[builtins.int, None], def (id: __main__.AClass, id2: None =) -> Union[builtins.int, None])"
y = x()
reveal_type(y.delete) # N: Revealed type is "Overload(def (id: builtins.int, id2: builtins.int) -> builtins.int, def (id: __main__.AClass, id2: None =) -> builtins.int)"
reveal_type(y.delete) # N: Revealed type is "Overload(def (id: builtins.int, id2: builtins.int) -> Union[builtins.int, None], def (id: __main__.AClass, id2: None =) -> Union[builtins.int, None])"
y.delete(10, 20)
y.delete(y)

def bar(x: AClass) -> None:
reveal_type(x.delete) # N: Revealed type is "Overload(def (id: builtins.int, id2: builtins.int) -> builtins.int, def (id: __main__.AClass, id2: None =) -> builtins.int)"
reveal_type(x.delete) # N: Revealed type is "Overload(def (id: builtins.int, id2: builtins.int) -> Union[builtins.int, None], def (id: __main__.AClass, id2: None =) -> Union[builtins.int, None])"
x.delete(10, 20)
[builtins fixtures/classmethod.pyi]

Expand Down
1 change: 1 addition & 0 deletions test-data/unit/check-serialize.test
Original file line number Diff line number Diff line change
Expand Up @@ -1275,6 +1275,7 @@ main:2: error: Too many arguments for "f"
main:2: error: Too many arguments for "f"

[case testSerializeDummyType]
# flags: --no-strict-optional
import a
[file a.py]
import b
Expand Down
9 changes: 6 additions & 3 deletions test-data/unit/check-super.test
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class B:
def f(self) -> 'B': pass
class A(B):
def f(self) -> 'A':
a, b = None, None # type: (A, B)
a: A
b: B
if int():
a = super().f() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
a = super().g() # E: "g" undefined in superclass
Expand All @@ -26,7 +27,8 @@ class B:
def f(self, y: 'A') -> None: pass
class A(B):
def f(self, y: Any) -> None:
a, b = None, None # type: (A, B)
a: A
b: B
super().f(b) # E: Argument 1 to "f" of "B" has incompatible type "B"; expected "A"
super().f(a)
self.f(b)
Expand All @@ -35,6 +37,7 @@ class A(B):
[out]

[case testAccessingSuperInit]
# flags: --no-strict-optional
import typing
class B:
def __init__(self, x: A) -> None: pass
Expand Down Expand Up @@ -90,7 +93,7 @@ class B(A):
def __new__(cls, x: int, y: str = '') -> 'B':
super().__new__(cls, 1)
super().__new__(cls, 1, '') # E: Too many arguments for "__new__" of "A"
return None
return cls(1)
B('') # E: Argument 1 to "B" has incompatible type "str"; expected "int"
B(1)
B(1, 'x')
Expand Down
Loading