Skip to content

Commit

Permalink
Enable strict optional for more test files (6) (#15603)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p committed Jul 6, 2023
1 parent fa8853b commit e0b159e
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 185 deletions.
3 changes: 0 additions & 3 deletions mypy/test/testcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@

# TODO: Enable strict optional in test cases by default. Remove files here, once test cases are updated
no_strict_optional_files = {
"check-functions.test",
"check-generic-subtyping.test",
"check-generics.test",
"check-inference-context.test",
"check-inference.test",
"check-isinstance.test",
Expand Down
109 changes: 58 additions & 51 deletions test-data/unit/check-functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@

[case testCallingVariableWithFunctionType]
from typing import Callable
f = None # type: Callable[[A], B]
a, b = None, None # type: (A, B)
f: Callable[[A], B]
a: A
b: B
if int():
a = f(a) # E: Incompatible types in assignment (expression has type "B", variable has type "A")
if int():
Expand Down Expand Up @@ -82,9 +83,9 @@ from typing import Callable
class A: pass
class B(A): pass

f = None # type: Callable[[B], A]
g = None # type: Callable[[A], A] # subtype of f
h = None # type: Callable[[B], B] # subtype of f
f: Callable[[B], A]
g: Callable[[A], A] # subtype of f
h: Callable[[B], B] # subtype of f
if int():
g = h # E: Incompatible types in assignment (expression has type "Callable[[B], B]", variable has type "Callable[[A], A]")
if int():
Expand Down Expand Up @@ -132,7 +133,7 @@ ff = g
from typing import Callable

def f(a: int, b: str) -> None: pass
f_nonames = None # type: Callable[[int, str], None]
f_nonames: Callable[[int, str], None]
def g(a: int, b: str = "") -> None: pass
def h(aa: int, b: str = "") -> None: pass

Expand Down Expand Up @@ -160,7 +161,7 @@ if int():
from typing import Any, Callable

def everything(*args: Any, **kwargs: Any) -> None: pass
everywhere = None # type: Callable[..., None]
everywhere: Callable[..., None]

def specific_1(a: int, b: str) -> None: pass
def specific_2(a: int, *, b: str) -> None: pass
Expand Down Expand Up @@ -238,6 +239,7 @@ if int():
gg = f # E: Incompatible types in assignment (expression has type "Callable[[int, str], None]", variable has type "Callable[[Arg(int, 'a'), Arg(str, 'b')], None]")

[case testFunctionTypeCompatibilityWithOtherTypes]
# flags: --no-strict-optional
from typing import Callable
f = None # type: Callable[[], None]
a, o = None, None # type: (A, object)
Expand Down Expand Up @@ -272,8 +274,8 @@ def g(x: int) -> Tuple[()]:

[case testFunctionSubtypingWithVoid]
from typing import Callable
f = None # type: Callable[[], None]
g = None # type: Callable[[], object]
f: Callable[[], None]
g: Callable[[], object]
if int():
f = g # E: Incompatible types in assignment (expression has type "Callable[[], object]", variable has type "Callable[[], None]")
if int():
Expand All @@ -286,9 +288,9 @@ if int():

[case testFunctionSubtypingWithMultipleArgs]
from typing import Callable
f = None # type: Callable[[A, A], None]
g = None # type: Callable[[A, B], None]
h = None # type: Callable[[B, B], None]
f: Callable[[A, A], None]
g: Callable[[A, B], None]
h: Callable[[B, B], None]
if int():
f = g # E: Incompatible types in assignment (expression has type "Callable[[A, B], None]", variable has type "Callable[[A, A], None]")
if int():
Expand All @@ -313,9 +315,9 @@ class B(A): pass

[case testFunctionTypesWithDifferentArgumentCounts]
from typing import Callable
f = None # type: Callable[[], None]
g = None # type: Callable[[A], None]
h = None # type: Callable[[A, A], None]
f: Callable[[], None]
g: Callable[[A], None]
h: Callable[[A, A], None]

if int():
f = g # E: Incompatible types in assignment (expression has type "Callable[[A], None]", variable has type "Callable[[], None]")
Expand All @@ -342,8 +344,8 @@ class A:

def f() -> None: pass

t = None # type: type
a = None # type: A
t: type
a: A

if int():
a = A # E: Incompatible types in assignment (expression has type "Type[A]", variable has type "A")
Expand All @@ -356,9 +358,9 @@ if int():
from foo import *
[file foo.pyi]
from typing import Callable, overload
f = None # type: Callable[[AA], A]
g = None # type: Callable[[B], B]
h = None # type: Callable[[A], AA]
f: Callable[[AA], A]
g: Callable[[B], B]
h: Callable[[A], AA]

if int():
h = i # E: Incompatible types in assignment (expression has type overloaded function, variable has type "Callable[[A], AA]")
Expand Down Expand Up @@ -395,11 +397,13 @@ def j(x: A) -> AA:
from foo import *
[file foo.pyi]
from typing import Callable, overload
g1 = None # type: Callable[[A], A]
g2 = None # type: Callable[[B], B]
g3 = None # type: Callable[[C], C]
g4 = None # type: Callable[[A], B]
a, b, c = None, None, None # type: (A, B, C)
g1: Callable[[A], A]
g2: Callable[[B], B]
g3: Callable[[C], C]
g4: Callable[[A], B]
a: A
b: B
c: C

if int():
b = f(a) # E: Incompatible types in assignment (expression has type "A", variable has type "B")
Expand Down Expand Up @@ -448,15 +452,15 @@ f([D]) # E: List item 0 has incompatible type "Type[D]"; expected "Callable[[An
[case testSubtypingTypeTypeAsCallable]
from typing import Callable, Type
class A: pass
x = None # type: Callable[..., A]
y = None # type: Type[A]
x: Callable[..., A]
y: Type[A]
x = y

[case testSubtypingCallableAsTypeType]
from typing import Callable, Type
class A: pass
x = None # type: Callable[..., A]
y = None # type: Type[A]
x: Callable[..., A]
y: Type[A]
if int():
y = x # E: Incompatible types in assignment (expression has type "Callable[..., A]", variable has type "Type[A]")

Expand Down Expand Up @@ -573,11 +577,11 @@ A().f('') # E: Argument 1 to "f" of "A" has incompatible type "str"; expected "i
[case testMethodAsDataAttribute]
from typing import Any, Callable, ClassVar
class B: pass
x = None # type: Any
x: Any
class A:
f = x # type: ClassVar[Callable[[A], None]]
g = x # type: ClassVar[Callable[[A, B], None]]
a = None # type: A
a: A
a.f()
a.g(B())
a.f(a) # E: Too many arguments
Expand All @@ -586,21 +590,21 @@ a.g() # E: Too few arguments
[case testMethodWithInvalidMethodAsDataAttribute]
from typing import Any, Callable, ClassVar
class B: pass
x = None # type: Any
x: Any
class A:
f = x # type: ClassVar[Callable[[], None]]
g = x # type: ClassVar[Callable[[B], None]]
a = None # type: A
a: A
a.f() # E: Attribute function "f" with type "Callable[[], None]" does not accept self argument
a.g() # E: Invalid self argument "A" to attribute function "g" with type "Callable[[B], None]"

[case testMethodWithDynamicallyTypedMethodAsDataAttribute]
from typing import Any, Callable, ClassVar
class B: pass
x = None # type: Any
x: Any
class A:
f = x # type: ClassVar[Callable[[Any], Any]]
a = None # type: A
a: A
a.f()
a.f(a) # E: Too many arguments

Expand All @@ -627,7 +631,7 @@ class A:
@overload
def f(self, b: B) -> None: pass
g = f
a = None # type: A
a: A
a.g()
a.g(B())
a.g(a) # E: No overload variant matches argument type "A" \
Expand All @@ -640,7 +644,7 @@ a.g(a) # E: No overload variant matches argument type "A" \
class A:
def f(self, x): pass
g = f
a = None # type: A
a: A
a.g(object())
a.g(a, a) # E: Too many arguments
a.g() # E: Too few arguments
Expand All @@ -652,7 +656,7 @@ class B: pass
class A(Generic[t]):
def f(self, x: t) -> None: pass
g = f
a = None # type: A[B]
a: A[B]
a.g(B())
a.g(a) # E: Argument 1 has incompatible type "A[B]"; expected "B"

Expand All @@ -661,11 +665,11 @@ from typing import Any, TypeVar, Generic, Callable, ClassVar
t = TypeVar('t')
class B: pass
class C: pass
x = None # type: Any
x: Any
class A(Generic[t]):
f = x # type: ClassVar[Callable[[A[B]], None]]
ab = None # type: A[B]
ac = None # type: A[C]
ab: A[B]
ac: A[C]
ab.f()
ac.f() # E: Invalid self argument "A[C]" to attribute function "f" with type "Callable[[A[B]], None]"

Expand All @@ -674,21 +678,21 @@ from typing import Any, TypeVar, Generic, Callable, ClassVar
t = TypeVar('t')
class B: pass
class C: pass
x = None # type: Any
x: Any
class A(Generic[t]):
f = x # type: ClassVar[Callable[[A], None]]
ab = None # type: A[B]
ac = None # type: A[C]
ab: A[B]
ac: A[C]
ab.f()
ac.f()

[case testCallableDataAttribute]
from typing import Callable, ClassVar
class A:
g = None # type: ClassVar[Callable[[A], None]]
g: ClassVar[Callable[[A], None]]
def __init__(self, f: Callable[[], None]) -> None:
self.f = f
a = A(None)
a = A(lambda: None)
a.f()
a.g()
a.f(a) # E: Too many arguments
Expand Down Expand Up @@ -895,7 +899,7 @@ def dec(x) -> Callable[[Any], None]: pass
class A:
@dec
def f(self, a, b, c): pass
a = None # type: A
a: A
a.f()
a.f(None) # E: Too many arguments for "f" of "A"

Expand Down Expand Up @@ -1945,9 +1949,9 @@ def a(f: Callable[[VarArg(int)], int]):
from typing import Callable
from mypy_extensions import Arg, DefaultArg

int_str_fun = None # type: Callable[[int, str], str]
int_opt_str_fun = None # type: Callable[[int, DefaultArg(str, None)], str]
int_named_str_fun = None # type: Callable[[int, Arg(str, 's')], str]
int_str_fun: Callable[[int, str], str]
int_opt_str_fun: Callable[[int, DefaultArg(str, None)], str]
int_named_str_fun: Callable[[int, Arg(str, 's')], str]

def isf(ii: int, ss: str) -> str:
return ss
Expand Down Expand Up @@ -2140,6 +2144,7 @@ main:8: error: Cannot use a covariant type variable as a parameter
from typing import TypeVar, Generic, Callable

[case testRejectContravariantReturnType]
# flags: --no-strict-optional
from typing import TypeVar, Generic

t = TypeVar('t', contravariant=True)
Expand All @@ -2148,16 +2153,18 @@ class A(Generic[t]):
return None
[builtins fixtures/bool.pyi]
[out]
main:5: error: Cannot use a contravariant type variable as return type
main:6: error: Cannot use a contravariant type variable as return type

[case testAcceptCovariantReturnType]
# flags: --no-strict-optional
from typing import TypeVar, Generic

t = TypeVar('t', covariant=True)
class A(Generic[t]):
def foo(self) -> t:
return None
[builtins fixtures/bool.pyi]

[case testAcceptContravariantArgument]
from typing import TypeVar, Generic

Expand Down
Loading

0 comments on commit e0b159e

Please sign in to comment.