Skip to content
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

Enable strict optional for pythoneval tests #15474

Merged
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
2 changes: 0 additions & 2 deletions mypy/test/testpythoneval.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ def test_python_evaluation(testcase: DataDrivenTestCase, cache_dir: str) -> None
"""
assert testcase.old_cwd is not None, "test was not properly set up"
# We must enable site packages to get access to installed stubs.
# TODO: Enable strict optional for these tests
mypy_cmdline = [
"--show-traceback",
"--no-strict-optional",
"--no-silence-site-packages",
"--no-error-summary",
"--hide-error-codes",
Expand Down
106 changes: 57 additions & 49 deletions test-data/unit/pythoneval.test
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ print(abs(A()))
5.5

[case testAbs2]
n = None # type: int
f = None # type: float
n: int
f: float
n = abs(1)
abs(1) + 'x' # Error
f = abs(1.1)
Expand Down Expand Up @@ -95,7 +95,7 @@ print(list.__add__([1, 2], [3, 4]))
import typing
class A:
x = 1
def f(self) -> None: print('f')
def f(self: typing.Optional["A"]) -> None: print('f')
class B(A):
pass
B.f(None)
Expand Down Expand Up @@ -130,7 +130,7 @@ A docstring!
[case testFunctionAttributes]
import typing
ord.__class__
print(type(ord.__doc__ + ''))
print(type(ord.__doc__ or '' + ''))
print(ord.__name__)
print(ord.__module__)
[out]
Expand Down Expand Up @@ -333,25 +333,28 @@ _program.py:6: note: Revealed type is "typing.IO[Any]"
[case testGenericPatterns]
from typing import Pattern
import re
p = None # type: Pattern[str]
p: Pattern[str]
p = re.compile('foo*')
b = None # type: Pattern[bytes]
b: Pattern[bytes]
b = re.compile(b'foo*')
print(p.match('fooo').group(0))
m = p.match('fooo')
assert m
print(m.group(0))
[out]
fooo

[case testGenericMatch]
from typing import Match
from typing import Match, Optional
import re
def f(m: Match[bytes]) -> None:
def f(m: Optional[Match[bytes]]) -> None:
assert m
print(m.group(0))
f(re.match(b'x*', b'xxy'))
[out]
b'xx'

[case testIntFloatDucktyping]
x = None # type: float
x: float
x = 2.2
x = 2
def f(x: float) -> None: pass
Expand All @@ -374,18 +377,17 @@ math.sin(2)
math.sin(2.2)

[case testAbsReturnType]

f = None # type: float
n = None # type: int
f: float
n: int
n = abs(2)
f = abs(2.2)
abs(2.2) + 'x'
[out]
_program.py:6: error: Unsupported operand types for + ("float" and "str")
_program.py:5: error: Unsupported operand types for + ("float" and "str")

[case testROperatorMethods]
b = None # type: bytes
s = None # type: str
b: bytes
s: str
if int():
s = b'foo' * 5 # Error
if int():
Expand Down Expand Up @@ -434,7 +436,6 @@ True
False

[case testOverlappingOperatorMethods]

class X: pass
class A:
def __add__(self, x) -> int:
Expand All @@ -444,11 +445,11 @@ class A:
class B:
def __radd__(self, x: A) -> str: return 'x'
class C(X, B): pass
b = None # type: B
b: B
b = C()
print(A() + b)
[out]
_program.py:9: error: Signatures of "__radd__" of "B" and "__add__" of "A" are unsafely overlapping
_program.py:8: error: Signatures of "__radd__" of "B" and "__add__" of "A" are unsafely overlapping

[case testBytesAndBytearrayComparisons]
import typing
Expand Down Expand Up @@ -859,7 +860,7 @@ class MyDDict(t.DefaultDict[int,T], t.Generic[T]):
MyDDict(dict)['0']
MyDDict(dict)[0]
[out]
_program.py:7: error: Argument 1 to "defaultdict" has incompatible type "Type[List[Any]]"; expected "Callable[[], str]"
_program.py:7: error: Argument 1 to "defaultdict" has incompatible type "Type[List[Any]]"; expected "Optional[Callable[[], str]]"
_program.py:10: error: Invalid index type "str" for "defaultdict[int, str]"; expected type "int"
_program.py:10: error: Incompatible types in assignment (expression has type "int", target has type "str")
_program.py:20: error: Argument 1 to "tst" has incompatible type "defaultdict[str, List[<nothing>]]"; expected "defaultdict[int, List[<nothing>]]"
Expand Down Expand Up @@ -966,10 +967,10 @@ print(getattr(B(), 'x'))
7

[case testSortedNoError]
from typing import Iterable, Callable, TypeVar, List, Dict
from typing import Iterable, Callable, TypeVar, List, Dict, Optional
T = TypeVar('T')
def sorted(x: Iterable[T], *, key: Callable[[T], object] = None) -> None: ...
a = None # type: List[Dict[str, str]]
def sorted(x: Iterable[T], *, key: Optional[Callable[[T], object]] = None) -> None: ...
a = [] # type: List[Dict[str, str]]
sorted(a, key=lambda y: y[''])

[case testAbstractProperty]
Expand Down Expand Up @@ -1002,9 +1003,13 @@ import re
bre = b'a+'
bpat = re.compile(bre)
bpat = re.compile(bpat)
re.search(bre, b'').groups()
s1 = re.search(bre, b'')
assert s1
s1.groups()
re.search(bre, u'') # Error
re.search(bpat, b'').groups()
s2 = re.search(bpat, b'')
assert s2
s2.groups()
re.search(bpat, u'') # Error
# match(), split(), findall(), finditer() are much the same, so skip those.
# sub(), subn() have more overloads and we are checking these:
Expand All @@ -1017,21 +1022,25 @@ re.subn(bpat, b'', b'')[0] + b''
re.subn(bre, lambda m: b'', b'')[0] + b''
re.subn(bpat, lambda m: b'', b'')[0] + b''
[out]
_testReModuleBytes.py:7: error: No overload variant of "search" matches argument types "bytes", "str"
_testReModuleBytes.py:7: note: Possible overload variants:
_testReModuleBytes.py:7: note: def search(pattern: Union[str, Pattern[str]], string: str, flags: Union[int, RegexFlag] = ...) -> Optional[Match[str]]
_testReModuleBytes.py:7: note: def search(pattern: Union[bytes, Pattern[bytes]], string: Buffer, flags: Union[int, RegexFlag] = ...) -> Optional[Match[bytes]]
_testReModuleBytes.py:9: error: Argument 1 to "search" has incompatible type "Pattern[bytes]"; expected "Union[str, Pattern[str]]"
_testReModuleBytes.py:9: error: No overload variant of "search" matches argument types "bytes", "str"
_testReModuleBytes.py:9: note: Possible overload variants:
_testReModuleBytes.py:9: note: def search(pattern: Union[str, Pattern[str]], string: str, flags: Union[int, RegexFlag] = ...) -> Optional[Match[str]]
_testReModuleBytes.py:9: note: def search(pattern: Union[bytes, Pattern[bytes]], string: Buffer, flags: Union[int, RegexFlag] = ...) -> Optional[Match[bytes]]
_testReModuleBytes.py:13: error: Argument 1 to "search" has incompatible type "Pattern[bytes]"; expected "Union[str, Pattern[str]]"

[case testReModuleString]
# Regression tests for various overloads in the re module -- string version
import re
sre = 'a+'
spat = re.compile(sre)
spat = re.compile(spat)
re.search(sre, '').groups()
s1 = re.search(sre, '')
assert s1
s1.groups()
re.search(sre, b'') # Error
re.search(spat, '').groups()
s2 = re.search(spat, '')
assert s2
s2.groups()
re.search(spat, b'') # Error
# match(), split(), findall(), finditer() are much the same, so skip those.
# sus(), susn() have more overloads and we are checking these:
Expand All @@ -1044,11 +1053,11 @@ re.subn(spat, '', '')[0] + ''
re.subn(sre, lambda m: '', '')[0] + ''
re.subn(spat, lambda m: '', '')[0] + ''
[out]
_testReModuleString.py:7: error: No overload variant of "search" matches argument types "str", "bytes"
_testReModuleString.py:7: note: Possible overload variants:
_testReModuleString.py:7: note: def search(pattern: Union[str, Pattern[str]], string: str, flags: Union[int, RegexFlag] = ...) -> Optional[Match[str]]
_testReModuleString.py:7: note: def search(pattern: Union[bytes, Pattern[bytes]], string: Buffer, flags: Union[int, RegexFlag] = ...) -> Optional[Match[bytes]]
_testReModuleString.py:9: error: Argument 1 to "search" has incompatible type "Pattern[str]"; expected "Union[bytes, Pattern[bytes]]"
_testReModuleString.py:9: error: No overload variant of "search" matches argument types "str", "bytes"
_testReModuleString.py:9: note: Possible overload variants:
_testReModuleString.py:9: note: def search(pattern: Union[str, Pattern[str]], string: str, flags: Union[int, RegexFlag] = ...) -> Optional[Match[str]]
_testReModuleString.py:9: note: def search(pattern: Union[bytes, Pattern[bytes]], string: Buffer, flags: Union[int, RegexFlag] = ...) -> Optional[Match[bytes]]
_testReModuleString.py:13: error: Argument 1 to "search" has incompatible type "Pattern[str]"; expected "Union[bytes, Pattern[bytes]]"

[case testListSetitemTuple]
from typing import List, Tuple
Expand Down Expand Up @@ -1085,7 +1094,6 @@ _program.py:17: note: Revealed type is "builtins.str"

[case testTypedDictGet]
# Test that TypedDict get plugin works with typeshed stubs
# TODO: Make it possible to use strict optional here
from mypy_extensions import TypedDict
class A: pass
D = TypedDict('D', {'x': int, 'y': str})
Expand All @@ -1097,14 +1105,14 @@ d.get()
s = ''
reveal_type(d.get(s))
[out]
_testTypedDictGet.py:7: note: Revealed type is "builtins.int"
_testTypedDictGet.py:8: note: Revealed type is "builtins.str"
_testTypedDictGet.py:9: note: Revealed type is "builtins.object"
_testTypedDictGet.py:10: error: All overload variants of "get" of "Mapping" require at least one argument
_testTypedDictGet.py:10: note: Possible overload variants:
_testTypedDictGet.py:10: note: def get(self, str, /) -> object
_testTypedDictGet.py:10: note: def [_T] get(self, str, /, default: object) -> object
_testTypedDictGet.py:12: note: Revealed type is "builtins.object"
_testTypedDictGet.py:6: note: Revealed type is "Union[builtins.int, None]"
_testTypedDictGet.py:7: note: Revealed type is "Union[builtins.str, None]"
_testTypedDictGet.py:8: note: Revealed type is "builtins.object"
_testTypedDictGet.py:9: error: All overload variants of "get" of "Mapping" require at least one argument
_testTypedDictGet.py:9: note: Possible overload variants:
_testTypedDictGet.py:9: note: def get(self, str, /) -> object
_testTypedDictGet.py:9: note: def [_T] get(self, str, /, default: object) -> object
_testTypedDictGet.py:11: note: Revealed type is "builtins.object"

[case testTypedDictMappingMethods]
from mypy_extensions import TypedDict
Expand Down Expand Up @@ -1143,10 +1151,10 @@ _testTypedDictMappingMethods.py:16: error: Key "value" of TypedDict "Cell" canno
_testTypedDictMappingMethods.py:21: note: Revealed type is "builtins.int"

[case testCrashOnComplexCheckWithNamedTupleNext]
from typing import NamedTuple
from typing import NamedTuple, Optional

MyNamedTuple = NamedTuple('MyNamedTuple', [('parent', 'MyNamedTuple')]) # type: ignore
def foo(mymap) -> MyNamedTuple:
def foo(mymap) -> Optional[MyNamedTuple]:
return next((mymap[key] for key in mymap), None)
[out]

Expand Down Expand Up @@ -1374,7 +1382,7 @@ JsonBlob = Dict[str, Any]
Column = Union[List[str], List[int], List[bool], List[float], List[DateTime], List[JsonBlob]]

def print_custom_table() -> None:
a = None # type: Column
a: Column

for row in simple_map(format_row, a, a, a, a, a, a, a, a): # 8 columns
reveal_type(row)
Expand Down