Skip to content

Commit

Permalink
Enable strict optional for more test files (4) (#15601)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p authored Jul 5, 2023
1 parent d4865b2 commit fae7d90
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 95 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-basic.test",
"check-bound.test",
"check-dynamic-typing.test",
"check-expressions.test",
"check-functions.test",
"check-generic-subtyping.test",
Expand Down
26 changes: 13 additions & 13 deletions test-data/unit/check-basic.test
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
[out]

[case testAssignmentAndVarDef]
a = None # type: A
b = None # type: B
a: A
b: B
if int():
a = a
if int():
Expand All @@ -17,23 +17,23 @@ class A:
class B:
def __init__(self): pass

x = None # type: A
x: A
x = A()
if int():
x = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
[case testInheritInitFromObject]
class A(object): pass
class B(object): pass
x = None # type: A
x: A
if int():
x = A()
if int():
x = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
[case testImplicitInheritInitFromObject]
class A: pass
class B: pass
x = None # type: A
o = None # type: object
x: A
o: object
if int():
x = o # E: Incompatible types in assignment (expression has type "object", variable has type "A")
if int():
Expand All @@ -59,7 +59,7 @@ x = B() # type: A
y = A() # type: B # E: Incompatible types in assignment (expression has type "A", variable has type "B")
[case testDeclaredVariableInParentheses]

(x) = None # type: int
(x) = 2 # type: int
if int():
x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
if int():
Expand Down Expand Up @@ -135,8 +135,8 @@ main:6: error: Missing positional arguments "baz", "bas" in call to "foo"

[case testLocalVariables]
def f() -> None:
x = None # type: A
y = None # type: B
x: A
y: B
if int():
x = x
x = y # E: Incompatible types in assignment (expression has type "B", variable has type "A")
Expand Down Expand Up @@ -229,21 +229,21 @@ reveal_type(__annotations__) # N: Revealed type is "builtins.dict[builtins.str,
[case testLocalVariableShadowing]
class A: pass
class B: pass
a = None # type: A
a: A
if int():
a = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
a = A()
def f() -> None:
a = None # type: B
a: B
if int():
a = A() # E: Incompatible types in assignment (expression has type "A", variable has type "B")
a = B()
a = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
a = A()
[case testGlobalDefinedInBlockWithType]
class A: pass
while A:
a = None # type: A
while 1:
a: A
if int():
a = A()
a = object() # E: Incompatible types in assignment (expression has type "object", variable has type "A")
Expand Down
10 changes: 6 additions & 4 deletions test-data/unit/check-bound.test
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,16 @@ T = TypeVar('T', bound=A)
class G(Generic[T]):
def __init__(self, x: T) -> None: pass

v = None # type: G[A]
w = None # type: G[B]
x = None # type: G[str] # E: Type argument "str" of "G" must be a subtype of "A"
v: G[A]
w: G[B]
x: G[str] # E: Type argument "str" of "G" must be a subtype of "A"
y = G('a') # E: Value of type variable "T" of "G" cannot be "str"
z = G(A())
z = G(B())


[case testBoundVoid]
# flags: --no-strict-optional
from typing import TypeVar, Generic
T = TypeVar('T', bound=int)
class C(Generic[T]):
Expand All @@ -70,10 +71,11 @@ def g(): pass

f(g())
C(g())
z = None # type: C
z: C


[case testBoundHigherOrderWithVoid]
# flags: --no-strict-optional
from typing import TypeVar, Callable
class A: pass
T = TypeVar('T', bound=A)
Expand Down
Loading

0 comments on commit fae7d90

Please sign in to comment.