Skip to content

Commit 04fe184

Browse files
gvanrossumJelleZijlstra
authored andcommitted
Add flags to pass on --warn-unused-ignores and --no-implicit-optional to mypy (#1421)
* Add flags to pass on --warn-unused-ignores and --no-implicit-optional to mypy * Make implicit Optional explicit in arg types (2and3 part) * Convert {stdlib,third_party}/2 to explicit Optional
1 parent 3025609 commit 04fe184

23 files changed

Lines changed: 257 additions & 237 deletions

File tree

stdlib/2/__builtin__.pyi

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ class slice(object):
485485
@overload
486486
def __init__(self, stop: Optional[int]) -> None: ...
487487
@overload
488-
def __init__(self, start: Optional[int], stop: Optional[int], step: int = None) -> None: ...
488+
def __init__(self, start: Optional[int], stop: Optional[int], step: Optional[int] = None) -> None: ...
489489
def indices(self, len: int) -> Tuple[int, int, int]: ...
490490

491491
class tuple(Sequence[_T_co], Generic[_T_co]):
@@ -676,13 +676,13 @@ class xrange(Sized, Iterable[int], Reversible[int]):
676676
def __reversed__(self) -> Iterator[int]: ...
677677

678678
class property(object):
679-
def __init__(self, fget: Callable[[Any], Any] = None,
680-
fset: Callable[[Any, Any], None] = None,
681-
fdel: Callable[[Any], None] = None, doc: str = None) -> None: ...
679+
def __init__(self, fget: Optional[Callable[[Any], Any]] = None,
680+
fset: Optional[Callable[[Any, Any], None]] = None,
681+
fdel: Optional[Callable[[Any], None]] = None, doc: Optional[str] = None) -> None: ...
682682
def getter(self, fget: Callable[[Any], Any]) -> property: ...
683683
def setter(self, fset: Callable[[Any, Any], None]) -> property: ...
684684
def deleter(self, fdel: Callable[[Any], None]) -> property: ...
685-
def __get__(self, obj: Any, type: type=None) -> Any: ...
685+
def __get__(self, obj: Any, type: Optional[type] = None) -> Any: ...
686686
def __set__(self, obj: Any, value: Any) -> None: ...
687687
def __delete__(self, obj: Any) -> None: ...
688688
def fget(self) -> Any: ...
@@ -716,7 +716,7 @@ def filter(function: Callable[[_T], Any],
716716
def filter(function: None,
717717
iterable: Iterable[Optional[_T]]) -> List[_T]: ...
718718
def format(o: object, format_spec: str = '') -> str: ... # TODO unicode
719-
def getattr(o: Any, name: unicode, default: Any = None) -> Any: ...
719+
def getattr(o: Any, name: unicode, default: Optional[Any] = None) -> Any: ...
720720
def hasattr(o: Any, name: unicode) -> bool: ...
721721
def hash(o: object) -> int: ...
722722
def hex(i: int) -> str: ... # TODO __index__
@@ -941,12 +941,12 @@ class ResourceWarning(Warning): ...
941941

942942
def eval(s: str, globals: Dict[str, Any] = ..., locals: Dict[str, Any] = ...) -> Any: ...
943943
def exec(object: str,
944-
globals: Dict[str, Any] = None,
945-
locals: Dict[str, Any] = None) -> Any: ... # TODO code object as source
944+
globals: Optional[Dict[str, Any]] = None,
945+
locals: Optional[Dict[str, Any]] = None) -> Any: ... # TODO code object as source
946946

947947
def cmp(x: Any, y: Any) -> int: ...
948948

949-
def execfile(filename: str, globals: Dict[str, Any] = None, locals: Dict[str, Any] = None) -> None: ...
949+
def execfile(filename: str, globals: Optional[Dict[str, Any]] = None, locals: Optional[Dict[str, Any]] = None) -> None: ...
950950

951951
class file(BinaryIO):
952952
@overload
@@ -958,7 +958,7 @@ class file(BinaryIO):
958958
def __iter__(self) -> Iterator[str]: ...
959959
def read(self, n: int = ...) -> str: ...
960960
def __enter__(self) -> BinaryIO: ...
961-
def __exit__(self, t: type = None, exc: BaseException = None, tb: Any = None) -> bool: ...
961+
def __exit__(self, t: Optional[type] = None, exc: Optional[BaseException] = None, tb: Optional[Any] = None) -> bool: ...
962962
def flush(self) -> None: ...
963963
def fileno(self) -> int: ...
964964
def isatty(self) -> bool: ...
@@ -976,6 +976,6 @@ class file(BinaryIO):
976976
def truncate(self, pos: Optional[int] = ...) -> int: ...
977977

978978
# Very old builtins
979-
def apply(func: Callable[..., _T], args: Sequence[Any] = None, kwds: Mapping[str, Any] = None) -> _T: ...
979+
def apply(func: Callable[..., _T], args: Optional[Sequence[Any]] = None, kwds: Optional[Mapping[str, Any]] = None) -> _T: ...
980980
_N = TypeVar('_N', bool, int, float, complex)
981981
def coerce(x: _N, y: _N) -> Tuple[_N, _N]: ...

stdlib/2/urllib2.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class Request(object):
4141
def add_header(self, key: str, val: str) -> None: ...
4242
def add_unredirected_header(self, key: str, val: str) -> None: ...
4343
def has_header(self, header_name: str) -> bool: ...
44-
def get_header(self, header_name: str, default: str = None) -> str: ...
44+
def get_header(self, header_name: str, default: Optional[str] = None) -> str: ...
4545
def header_items(self): ...
4646

4747
class OpenerDirector(object):

stdlib/2and3/asynchat.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ from abc import abstractmethod
22
import asyncore
33
import socket
44
import sys
5-
from typing import Union, Tuple, Sequence
5+
from typing import Optional, Sequence, Tuple, Union
66

77

88
class simple_producer:
@@ -12,7 +12,7 @@ class simple_producer:
1212
class async_chat(asyncore.dispatcher):
1313
ac_in_buffer_size = ... # type: int
1414
ac_out_buffer_size = ... # type: int
15-
def __init__(self, sock: socket.socket = None, map: asyncore._maptype = None) -> None: ...
15+
def __init__(self, sock: Optional[socket.socket] = None, map: Optional[asyncore._maptype] = None) -> None: ...
1616

1717
@abstractmethod
1818
def collect_incoming_data(self, data: bytes) -> None: ...

stdlib/2and3/asyncore.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def poll2(timeout: float = ..., map: _maptype = ...) -> None: ...
2525

2626
poll3 = poll2
2727

28-
def loop(timeout: float = ..., use_poll: bool = ..., map: _maptype = ..., count: int = None) -> None: ...
28+
def loop(timeout: float = ..., use_poll: bool = ..., map: _maptype = ..., count: Optional[int] = None) -> None: ...
2929

3030

3131
# Not really subclass of socket.socket; it's only delegation.
@@ -39,7 +39,7 @@ class dispatcher:
3939
closing = ... # type: bool
4040
ignore_log_types = ... # type: frozenset[str]
4141

42-
def __init__(self, sock: socket.socket = None, map: _maptype = ...) -> None: ...
42+
def __init__(self, sock: Optional[socket.socket] = None, map: _maptype = ...) -> None: ...
4343
def add_channel(self, map: _maptype = ...) -> None: ...
4444
def del_channel(self, map: _maptype = ...) -> None: ...
4545
def create_socket(self, family: int, type: int) -> None: ...

stdlib/2and3/bz2.pyi

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
# Stubs for bz2
22

3-
from typing import Any, BinaryIO, TextIO, IO, Union
3+
from typing import Any, BinaryIO, TextIO, IO, Optional, Union
44

55
def compress(data: bytes, compresslevel: int = ...) -> bytes: ...
66
def decompress(data: bytes) -> bytes: ...
77

88
def open(filename: Union[str, bytes, IO[Any]],
99
mode: str = 'rb',
10-
encoding: str = None,
11-
errors: str = None,
12-
newline: str = None) -> IO[Any]: ...
10+
encoding: Optional[str] = None,
11+
errors: Optional[str] = None,
12+
newline: Optional[str] = None) -> IO[Any]: ...
1313

1414
class BZ2File(BinaryIO):
1515
def __init__(self,
1616
filename: Union[str, bytes, IO[Any]],
1717
mode: str = "r",
18-
buffering: Any = None,
18+
buffering: Optional[Any] = None,
1919
compresslevel: int = 9) -> None: ...
2020

2121
class BZ2Compressor(object):

stdlib/2and3/pdb.pyi

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# NOTE: This stub is incomplete - only contains some global functions
22

3-
from typing import Any, Dict
3+
from typing import Any, Dict, Optional
44

55
def run(statement: str,
6-
globals: Dict[str, Any] = None,
7-
locals: Dict[str, Any] = None) -> None:
6+
globals: Optional[Dict[str, Any]] = None,
7+
locals: Optional[Dict[str, Any]] = None) -> None:
88
...
99

1010
def runeval(expression: str,
11-
globals: Dict[str, Any] = None,
12-
locals: Dict[str, Any] = None) -> Any:
11+
globals: Optional[Dict[str, Any]] = None,
12+
locals: Optional[Dict[str, Any]] = None) -> Any:
1313
...
1414

1515
def runctx(statement: str,
@@ -23,7 +23,7 @@ def runcall(*args: Any, **kwds: Any) -> Any:
2323
def set_trace() -> None:
2424
...
2525

26-
def post_mortem(t: Any = None) -> None:
26+
def post_mortem(t: Optional[Any] = None) -> None:
2727
...
2828

2929
def pm() -> None:

tests/mypy_test.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@
2424
parser.add_argument('-x', '--exclude', type=str, nargs='*', help="Exclude pattern")
2525
parser.add_argument('-p', '--python-version', type=str, nargs='*',
2626
help="These versions only (major[.minor])")
27+
parser.add_argument('--no-implicit-optional', action='store_true',
28+
help="Run mypy with --no-implicit-optional (causes lots of errors)")
29+
parser.add_argument('--warn-unused-ignores', action='store_true',
30+
help="Run mypy with --warn-unused-ignores "
31+
"(hint: only git rid of warnings that are "
32+
"unused for all platforms and Python versions)")
33+
2734
parser.add_argument('filter', type=str, nargs='*', help="Include pattern (default all)")
2835

2936

@@ -124,7 +131,10 @@ def main():
124131
runs += 1
125132
flags = ['--python-version', '%d.%d' % (major, minor)]
126133
flags.append('--strict-optional')
127-
# flags.append('--warn-unused-ignores') # Fast parser and regular parser disagree.
134+
if args.no_implicit_optional:
135+
flags.append('--no-implicit-optional')
136+
if args.warn_unused_ignores:
137+
flags.append('--warn-unused-ignores')
128138
sys.argv = ['mypy'] + flags + files
129139
if args.verbose:
130140
print("running", ' '.join(sys.argv))

third_party/2/dateutil/parser.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ class parserinfo(object):
2727
def validate(self, res: datetime) -> bool: ...
2828

2929
class parser(object):
30-
def __init__(self, info: parserinfo = None) -> None: ...
30+
def __init__(self, info: Optional[parserinfo] = None) -> None: ...
3131
def parse(self, timestr: Union[str, unicode, IO[unicode]],
32-
default: datetime = None,
33-
ignoretz: bool = ..., tzinfos: Dict[Union[str, unicode], tzinfo] = None,
32+
default: Optional[datetime] = None,
33+
ignoretz: bool = ..., tzinfos: Optional[Dict[Union[str, unicode], tzinfo]] = None,
3434
**kwargs: Any) -> datetime: ...
3535

3636
DEFAULTPARSER = ... # type: parser
3737
def parse(timestr: Union[str, unicode, IO[unicode]],
38-
parserinfo: parserinfo = None,
38+
parserinfo: Optional[parserinfo] = None,
3939
**kwargs: Any) -> datetime: ...

third_party/2/google/protobuf/message_factory.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ from .descriptor_pool import DescriptorPool
66

77
class MessageFactory:
88
pool = ... # type: Any
9-
def __init__(self, pool: DescriptorPool=None) -> None: ...
9+
def __init__(self, pool: Optional[DescriptorPool] = None) -> None: ...
1010
def GetPrototype(self, descriptor: Descriptor) -> Type[Message]: ...
1111
def GetMessages(self, files: Iterable[str]) -> Dict[str, Type[Message]]: ...
1212

third_party/2/werkzeug/wrappers.pyi

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,15 @@ class BaseResponse:
7676
status = ... # type: str
7777
direct_passthrough = ... # type: bool
7878
response = ... # type: Iterable[str]
79-
def __init__(self, response: Union[Iterable[str], str]=None, status=Union[basestring, int], headers: Union[Headers, Mapping[basestring, basestring], Sequence[Tuple[basestring, basestring]]]=None, mimetype: basestring=None, content_type: basestring=None, direct_passthrough: bool=False) -> None: ...
79+
def __init__(self,
80+
response: Optional[Union[Iterable[str], str]] = None,
81+
status: Optional[Union[basestring, int]] = None,
82+
headers: Optional[Union[Headers,
83+
Mapping[basestring, basestring],
84+
Sequence[Tuple[basestring, basestring]]]] = None,
85+
mimetype: Optional[basestring] = None,
86+
content_type: Optional[basestring] = None,
87+
direct_passthrough: Optional[bool] = False) -> None: ...
8088
def call_on_close(self, func): ...
8189
@classmethod
8290
def force_type(cls, response, environ=None): ...

0 commit comments

Comments
 (0)