Skip to content

Commit

Permalink
Enable strict optional for more test files (8) (#15606)
Browse files Browse the repository at this point in the history
Followup to #15586
  • Loading branch information
cdce8p authored Jul 7, 2023
1 parent 91e4ce4 commit ef0b763
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 163 deletions.
5 changes: 0 additions & 5 deletions mypy/test/testcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@
# TODO: Enable strict optional in test cases by default. Remove files here, once test cases are updated
no_strict_optional_files = {
"check-modules.test",
"check-namedtuple.test",
"check-overloading.test",
"check-plugin-attrs.test",
"check-statements.test",
"check-tuples.test",
"check-unions.test",
"check-varargs.test",
}
Expand Down
36 changes: 18 additions & 18 deletions test-data/unit/check-namedtuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from collections import namedtuple

X = namedtuple('X', 'x y')
x = None # type: X
x: X
a, b = x
b = x[0]
a = x[1]
Expand All @@ -14,7 +14,7 @@ x[2] # E: Tuple index out of range
from collections import namedtuple

X = namedtuple('X', ('x', 'y'))
x = None # type: X
x: X
a, b = x
b = x[0]
a = x[1]
Expand All @@ -32,7 +32,7 @@ X = namedtuple('X', 'x, _y, _z') # E: "namedtuple()" field names cannot start w
from collections import namedtuple

X = namedtuple('X', 'x y')
x = None # type: X
x: X
x.x
x.y
x.z # E: "X" has no attribute "z"
Expand Down Expand Up @@ -63,13 +63,13 @@ class A(NamedTuple):
from collections import namedtuple

X = namedtuple('X', 'x y')
x = None # type: X
x: X
x.x = 5 # E: Property "x" defined in "X" is read-only
x.y = 5 # E: Property "y" defined in "X" is read-only
x.z = 5 # E: "X" has no attribute "z"

class A(X): pass
a = None # type: A
a: A
a.x = 5 # E: Property "x" defined in "X" is read-only
a.y = 5 # E: Property "y" defined in "X" is read-only
-- a.z = 5 # not supported yet
Expand Down Expand Up @@ -292,7 +292,7 @@ A = NamedTuple('A', [('a', int), ('b', str)])
class B(A): pass
a = A(1, '')
b = B(1, '')
t = None # type: Tuple[int, str]
t: Tuple[int, str]
if int():
b = a # E: Incompatible types in assignment (expression has type "A", variable has type "B")
if int():
Expand Down Expand Up @@ -357,7 +357,7 @@ C(2).b
from collections import namedtuple

X = namedtuple('X', ['x', 'y'])
x = None # type: X
x: X
reveal_type(x._asdict()) # N: Revealed type is "builtins.dict[builtins.str, Any]"

[builtins fixtures/dict.pyi]
Expand All @@ -366,7 +366,7 @@ reveal_type(x._asdict()) # N: Revealed type is "builtins.dict[builtins.str, Any
from collections import namedtuple

X = namedtuple('X', ['x', 'y'])
x = None # type: X
x: X
reveal_type(x._replace()) # N: Revealed type is "Tuple[Any, Any, fallback=__main__.X]"
x._replace(y=5)
x._replace(x=3)
Expand All @@ -391,7 +391,7 @@ X._replace(x=1, y=2) # E: Missing positional argument "_self" in call to "_repl
from typing import NamedTuple

X = NamedTuple('X', [('x', int), ('y', str)])
x = None # type: X
x: X
reveal_type(x._replace()) # N: Revealed type is "Tuple[builtins.int, builtins.str, fallback=__main__.X]"
x._replace(x=5)
x._replace(y=5) # E: Argument "y" to "_replace" of "X" has incompatible type "int"; expected "str"
Expand All @@ -405,7 +405,7 @@ reveal_type(X._make([5, 'a'])) # N: Revealed type is "Tuple[builtins.int, built
X._make('a b') # E: Argument 1 to "_make" of "X" has incompatible type "str"; expected "Iterable[Any]"

-- # FIX: not a proper class method
-- x = None # type: X
-- x: X
-- reveal_type(x._make([5, 'a'])) # N: Revealed type is "Tuple[builtins.int, builtins.str, fallback=__main__.X]"
-- x._make('a b') # E: Argument 1 to "_make" of "X" has incompatible type "str"; expected Iterable[Any]

Expand All @@ -423,7 +423,7 @@ from typing import NamedTuple

X = NamedTuple('X', [('x', int), ('y', str)])
reveal_type(X._source) # N: Revealed type is "builtins.str"
x = None # type: X
x: X
reveal_type(x._source) # N: Revealed type is "builtins.str"
[builtins fixtures/tuple.pyi]

Expand Down Expand Up @@ -459,7 +459,7 @@ from typing import NamedTuple

X = NamedTuple('X', [('x', int), ('y', str)])
reveal_type(X._field_types) # N: Revealed type is "builtins.dict[builtins.str, Any]"
x = None # type: X
x: X
reveal_type(x._field_types) # N: Revealed type is "builtins.dict[builtins.str, Any]"

[builtins fixtures/dict.pyi]
Expand All @@ -472,7 +472,7 @@ def f(x: A) -> None: pass

class B(NamedTuple('B', []), A): pass
f(B())
x = None # type: A
x: A
if int():
x = B()

Expand All @@ -482,7 +482,7 @@ def g(x: C) -> None: pass
class D(NamedTuple('D', []), A): pass

g(D()) # E: Argument 1 to "g" has incompatible type "D"; expected "C"
y = None # type: C
y: C
if int():
y = D() # E: Incompatible types in assignment (expression has type "D", variable has type "C")
[builtins fixtures/tuple.pyi]
Expand All @@ -499,9 +499,9 @@ class A(NamedTuple('A', [('x', str)])):
class B(A):
pass

a = None # type: A
a: A
a = A('').member()
b = None # type: B
b: B
b = B('').member()
a = B('')
a = B('').member()
Expand All @@ -511,14 +511,14 @@ a = B('').member()
from typing import NamedTuple, TypeVar
A = NamedTuple('A', [('x', str)])
reveal_type(A('hello')._replace(x='')) # N: Revealed type is "Tuple[builtins.str, fallback=__main__.A]"
a = None # type: A
a: A
a = A('hello')._replace(x='')

class B(A):
pass

reveal_type(B('hello')._replace(x='')) # N: Revealed type is "Tuple[builtins.str, fallback=__main__.B]"
b = None # type: B
b: B
b = B('hello')._replace(x='')
[builtins fixtures/tuple.pyi]

Expand Down
35 changes: 24 additions & 11 deletions test-data/unit/check-overloading.test
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,8 @@ class C: pass
from foo import *
[file foo.pyi]
from typing import overload
a, b = None, None # type: (A, B)
a: A
b: B
if int():
b = f(a) # E: Incompatible types in assignment (expression has type "A", variable has type "B")
if int():
Expand Down Expand Up @@ -492,7 +493,8 @@ class C: pass
from foo import *
[file foo.pyi]
from typing import overload
a, b = None, None # type: (A, B)
a: A
b: B
if int():
b = a.f(a) # E: Incompatible types in assignment (expression has type "A", variable has type "B")
if int():
Expand All @@ -514,7 +516,8 @@ class B: pass
from foo import *
[file foo.pyi]
from typing import overload
a, b = None, None # type: (A, B)
a: A
b: B
if int():
a = f(a)
if int():
Expand Down Expand Up @@ -549,7 +552,10 @@ from foo import *
[file foo.pyi]
from typing import overload, TypeVar, Generic
t = TypeVar('t')
ab, ac, b, c = None, None, None, None # type: (A[B], A[C], B, C)
ab: A[B]
ac: A[C]
b: B
c: C
if int():
b = f(ab)
c = f(ac)
Expand All @@ -569,7 +575,8 @@ class C: pass
from foo import *
[file foo.pyi]
from typing import overload
a, b = None, None # type: (A, B)
a: A
b: B
a = A(a)
a = A(b)
a = A(object()) # E: No overload variant of "A" matches argument type "object" \
Expand All @@ -589,8 +596,8 @@ class B: pass
from foo import *
[file foo.pyi]
from typing import overload, Callable
o = None # type: object
a = None # type: A
o: object
a: A

if int():
a = f # E: Incompatible types in assignment (expression has type overloaded function, variable has type "A")
Expand All @@ -607,7 +614,8 @@ class A: pass
from foo import *
[file foo.pyi]
from typing import overload
t, a = None, None # type: (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 @@ -625,7 +633,8 @@ class B: pass
from foo import *
[file foo.pyi]
from typing import overload
a, b = None, None # type: int, str
a: int
b: str
if int():
a = A()[a]
if int():
Expand All @@ -647,7 +656,9 @@ from foo import *
[file foo.pyi]
from typing import TypeVar, Generic, overload
t = TypeVar('t')
a, b, c = None, None, None # type: (A, B, C[A])
a: A
b: B
c: C[A]
if int():
a = c[a]
b = c[a] # E: Incompatible types in assignment (expression has type "A", variable has type "B")
Expand Down Expand Up @@ -761,7 +772,8 @@ from typing import overload
def f(t: type) -> 'A': pass
@overload
def f(t: 'A') -> 'B': pass
a, b = None, None # type: (A, B)
a: A
b: B
if int():
a = f(A)
if int():
Expand Down Expand Up @@ -1215,6 +1227,7 @@ f(*(1, '', 1))() # E: No overload variant of "f" matches argument type "Tuple[in
[builtins fixtures/tuple.pyi]

[case testPreferExactSignatureMatchInOverload]
# flags: --no-strict-optional
from foo import *
[file foo.pyi]
from typing import overload, List
Expand Down
3 changes: 3 additions & 0 deletions test-data/unit/check-plugin-attrs.test
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,7 @@ class A(Generic[T]):
[builtins fixtures/classmethod.pyi]

[case testAttrsForwardReference]
# flags: --no-strict-optional
import attr
@attr.s(auto_attribs=True)
class A:
Expand All @@ -687,6 +688,7 @@ A(B(None))
[builtins fixtures/list.pyi]

[case testAttrsForwardReferenceInClass]
# flags: --no-strict-optional
import attr
@attr.s(auto_attribs=True)
class A:
Expand Down Expand Up @@ -1593,6 +1595,7 @@ def f(t: TA) -> None:
[builtins fixtures/plugin_attrs.pyi]

[case testNonattrsFields]
# flags: --no-strict-optional
from typing import Any, cast, Type
from attrs import fields

Expand Down
Loading

0 comments on commit ef0b763

Please sign in to comment.