Skip to content

Commit 3505632

Browse files
gvanrossummatthiaskramm
authored andcommitted
Add Optional[] for all remaining cases of x: <type> = None (#1424)
* Final round of adding Optional[] to type of arguments with default = None * Update Travis to use --no-implicit-optionals and clarify CONTRIBUTING.md
1 parent 81f77b1 commit 3505632

18 files changed

Lines changed: 152 additions & 133 deletions

File tree

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ matrix:
88
- python: "3.6"
99
env: TEST_CMD="./tests/mypy_selftest.py"
1010
- python: "3.5"
11-
env: TEST_CMD="./tests/mypy_test.py"
11+
env: TEST_CMD="./tests/mypy_test.py --no-implicit-optional"
1212
- python: "2.7"
1313
env: TEST_CMD="./tests/pytype_test.py --num-parallel=4"
1414

CONTRIBUTING.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ rule is that they should be as concise as possible. Specifically:
135135
names, or methods and fields within a single class;
136136
* use a single blank line between top-level class definitions, or none
137137
if the classes are very small;
138-
* do not use docstrings.
138+
* do not use docstrings;
139+
* for arguments with a type and a default, use spaces around the `=`.
139140

140141
Imports in stubs are considered private (not part of the exported API)
141142
unless:
@@ -144,6 +145,14 @@ unless:
144145
* they use the form ``from library import *`` which means all names
145146
from that library are exported.
146147

148+
For arguments with type and a default value of `None`, PEP 484
149+
prescribes that the type automatically becomes `Optional`. However we
150+
prefer explicit over implicit in this case, and require the explicit
151+
`Optional[]` around the type. The mypy tests enforce this (through
152+
the use of --no-implicit-optional) and the error looks like
153+
`Incompatible types in assignment (expression has type None, variable
154+
has type "Blah") `.
155+
147156
Stub files support forward references natively. In other words, the
148157
order of class declarations and type aliases does not matter in
149158
a stub file. You can also use the name of the class within its own

stdlib/3.4/asyncio/locks.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Event:
3737
def wait(self) -> Generator[Any, None, bool]: ...
3838

3939
class Condition(_ContextManagerMixin):
40-
def __init__(self, lock: Lock = None, *, loop: Optional[AbstractEventLoop] = ...) -> None: ...
40+
def __init__(self, lock: Optional[Lock] = None, *, loop: Optional[AbstractEventLoop] = ...) -> None: ...
4141
def locked(self) -> bool: ...
4242
@coroutine
4343
def acquire(self) -> Generator[Any, None, bool]: ...

stdlib/3/builtins.pyi

Lines changed: 61 additions & 59 deletions
Large diffs are not rendered by default.

stdlib/3/configparser.pyi

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ class RawConfigParser(_parser):
8080
def has_option(self, section: str, option: str) -> bool: ...
8181

8282
def read(self, filenames: Union[str, Sequence[str]],
83-
encoding: str = None) -> List[str]: ...
83+
encoding: Optional[str] = None) -> List[str]: ...
8484

85-
def read_file(self, f: Iterable[str], source: str = None) -> None: ...
85+
def read_file(self, f: Iterable[str], source: Optional[str] = None) -> None: ...
8686

8787
def read_string(self, string: str, source: str = ...) -> None: ...
8888

@@ -117,16 +117,16 @@ class RawConfigParser(_parser):
117117

118118
class ConfigParser(RawConfigParser):
119119
def __init__(self,
120-
defaults: _section = None,
120+
defaults: Optional[_section] = None,
121121
dict_type: Mapping[str, str] = ...,
122122
allow_no_value: bool = ...,
123123
delimiters: Sequence[str] = ...,
124124
comment_prefixes: Sequence[str] = ...,
125-
inline_comment_prefixes: Sequence[str] = None,
125+
inline_comment_prefixes: Optional[Sequence[str]] = None,
126126
strict: bool = ...,
127127
empty_lines_in_values: bool = ...,
128128
default_section: str = ...,
129-
interpolation: Interpolation = None,
129+
interpolation: Optional[Interpolation] = None,
130130
converters: _converters = ...) -> None: ...
131131

132132
class SafeConfigParser(ConfigParser): ...

stdlib/3/getpass.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Stubs for getpass
22

3-
from typing import TextIO
3+
from typing import Optional, TextIO
44

55

6-
def getpass(prompt: str = ..., stream: TextIO = None) -> str: ...
6+
def getpass(prompt: str = ..., stream: Optional[TextIO] = None) -> str: ...
77

88

99
def getuser() -> str: ...

stdlib/3/importlib/__init__.pyi

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import sys
44
import types
55
from typing import Any, Mapping, Optional, Sequence
66

7-
def __import__(name: str, globals: Mapping[str, Any] = None,
8-
locals: Mapping[str, Any] = None, fromlist: Sequence[str] = ...,
7+
def __import__(name: str, globals: Optional[Mapping[str, Any]] = None,
8+
locals: Optional[Mapping[str, Any]] = None,
9+
fromlist: Sequence[str] = ...,
910
level: int = ...) -> types.ModuleType: ...
1011

11-
def import_module(name: str, package: str = None) -> types.ModuleType: ...
12+
def import_module(name: str, package: Optional[str] = None) -> types.ModuleType: ...
1213

1314
if sys.version_info >= (3, 3):
14-
def find_loader(name: str, path: str = None) -> Optional[Loader]: ...
15+
def find_loader(name: str, path: Optional[str] = None) -> Optional[Loader]: ...
1516

1617
def invalidate_caches() -> None: ...
1718

stdlib/3/importlib/abc.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Finder(metaclass=ABCMeta):
1919
# easier to simply ignore that this method exists.
2020
# @abstractmethod
2121
# def find_module(self, fullname: str,
22-
# path: Sequence[_Path] = None) -> Optional[Loader]: ...
22+
# path: Optional[Sequence[_Path]] = None) -> Optional[Loader]: ...
2323

2424
class ResourceLoader(Loader):
2525
@abstractmethod
@@ -64,7 +64,7 @@ if sys.version_info >= (3, 3):
6464
# Not defined on the actual class, but expected to exist.
6565
def find_spec(
6666
self, fullname: str, path: Optional[Sequence[_Path]],
67-
target: types.ModuleType = None
67+
target: Optional[types.ModuleType] = None
6868
) -> Optional[ModuleSpec]:
6969
...
7070

@@ -78,7 +78,7 @@ if sys.version_info >= (3, 3):
7878
# Not defined on the actual class, but expected to exist.
7979
def find_spec(
8080
self, fullname: str,
81-
target: types.ModuleType = None
81+
target: Optional[types.ModuleType] = None
8282
) -> Optional[ModuleSpec]: ...
8383

8484
class FileLoader(ResourceLoader, ExecutionLoader):

stdlib/3/importlib/machinery.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ if sys.version_info >= (3, 3):
2222
@classmethod
2323
def find_spec(cls, fullname: str,
2424
path: Optional[Sequence[importlib.abc._Path]],
25-
target: types.ModuleType = None) -> Optional[ModuleSpec]:
25+
target: Optional[types.ModuleType] = None) -> Optional[ModuleSpec]:
2626
...
2727
# InspectLoader
2828
@classmethod
@@ -79,7 +79,7 @@ if sys.version_info >= (3, 3):
7979
@classmethod
8080
def find_spec(cls, fullname: str,
8181
path: Optional[Sequence[importlib.abc._Path]],
82-
target: types.ModuleType = None) -> Optional[ModuleSpec]:
82+
target: Optional[types.ModuleType] = None) -> Optional[ModuleSpec]:
8383
...
8484
# InspectLoader
8585
@classmethod
@@ -136,7 +136,7 @@ if sys.version_info >= (3, 3):
136136
@classmethod
137137
def find_spec(cls, fullname: str,
138138
path: Optional[Sequence[importlib.abc._Path]],
139-
target: types.ModuleType = None) -> Optional[ModuleSpec]:
139+
target: Optional[types.ModuleType] = None) -> Optional[ModuleSpec]:
140140
...
141141
else:
142142
class WindowsRegisteryFinder:

stdlib/3/importlib/util.pyi

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,22 @@ if sys.version_info >= (3, 3):
2020
if sys.version_info >= (3, 4):
2121
MAGIC_NUMBER = ... # type: bytes
2222

23-
def cache_from_source(path: str, debug_override: bool = None, *,
24-
optimization: Any = None) -> str: ...
23+
def cache_from_source(path: str, debug_override: Optional[bool] = None, *,
24+
optimization: Optional[Any] = None) -> str: ...
2525
def source_from_cache(path: str) -> str: ...
2626
def decode_source(source_bytes: bytes) -> str: ...
2727
def find_spec(
28-
name: str, package: str = None
28+
name: str, package: Optional[str] = None
2929
) -> importlib.machinery.ModuleSpec: ...
3030
def spec_from_loader(
3131
name: str, loader: Optional[importlib.abc.Loader], *,
32-
origin: str = None, loader_state: Any = None,
33-
is_package: bool = None
32+
origin: Optional[str] = None, loader_state: Optional[Any] = None,
33+
is_package: Optional[bool] = None
3434
) -> importlib.machinery.ModuleSpec: ...
3535
def spec_from_file_location(
3636
name: str, location: str, *,
37-
loader: importlib.abc.Loader = None,
38-
submodule_search_locations: List[str]=None
37+
loader: Optional[importlib.abc.Loader] = None,
38+
submodule_search_locations: Optional[List[str]] = None
3939
) -> importlib.machinery.ModuleSpec: ...
4040

4141
if sys.version_info >= (3, 5):

0 commit comments

Comments
 (0)