Skip to content

Commit 65aedb5

Browse files
ddfishergvanrossum
authored andcommitted
Make mypy --no-implicit-optional clean for strict Optional-checked files (#3699)
* Make mypy --no-implicit-optinal clean for strict Optional-checked files * fix lint errors * quote optionals
1 parent c106579 commit 65aedb5

21 files changed

+122
-87
lines changed

mypy/binder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def __init__(self) -> None:
8989
self.break_frames = [] # type: List[int]
9090
self.continue_frames = [] # type: List[int]
9191

92-
def _add_dependencies(self, key: Key, value: Key = None) -> None:
92+
def _add_dependencies(self, key: Key, value: Optional[Key] = None) -> None:
9393
if value is None:
9494
value = key
9595
else:

mypy/checkexpr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2204,7 +2204,7 @@ def visit_backquote_expr(self, e: BackquoteExpr) -> Type:
22042204

22052205
def accept(self,
22062206
node: Expression,
2207-
type_context: Type = None,
2207+
type_context: Optional[Type] = None,
22082208
allow_none_return: bool = False,
22092209
always_allow_any: bool = False,
22102210
) -> Type:

mypy/checkmember.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def analyze_member_access(name: str,
3838
msg: MessageBuilder, *,
3939
original_type: Type,
4040
chk: 'mypy.checker.TypeChecker',
41-
override_info: TypeInfo = None) -> Type:
41+
override_info: Optional[TypeInfo] = None) -> Type:
4242
"""Return the type of attribute `name` of typ.
4343
4444
This is a general operation that supports various different variations:
@@ -603,7 +603,7 @@ def map_type_from_supertype(typ: Type, sub_info: TypeInfo,
603603
F = TypeVar('F', bound=FunctionLike)
604604

605605

606-
def bind_self(method: F, original_type: Type = None, is_classmethod: bool = False) -> F:
606+
def bind_self(method: F, original_type: Optional[Type] = None, is_classmethod: bool = False) -> F:
607607
"""Return a copy of `method`, with the type of its first parameter (usually
608608
self or cls) bound to original_type.
609609

mypy/checkstrformat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,6 @@ def named_type(self, name: str) -> Instance:
334334
"""
335335
return self.chk.named_type(name)
336336

337-
def accept(self, expr: Expression, context: Type = None) -> Type:
337+
def accept(self, expr: Expression, context: Optional[Type] = None) -> Type:
338338
"""Type check a node. Alias for TypeChecker.accept."""
339339
return self.chk.expr_checker.accept(expr, context)

mypy/errors.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ def __init__(self,
6565
message: str,
6666
blocker: bool,
6767
only_once: bool,
68-
origin: Tuple[str, int] = None,
69-
target: str = None) -> None:
68+
origin: Optional[Tuple[str, int]] = None,
69+
target: Optional[str] = None) -> None:
7070
self.import_ctx = import_ctx
7171
self.file = file
7272
self.module = module
@@ -175,7 +175,9 @@ def simplify_path(self, file: str) -> str:
175175
file = os.path.normpath(file)
176176
return remove_path_prefix(file, self.ignore_prefix)
177177

178-
def set_file(self, file: str, module: Optional[str], ignored_lines: Set[int] = None) -> None:
178+
def set_file(self, file: str,
179+
module: Optional[str],
180+
ignored_lines: Optional[Set[int]] = None) -> None:
179181
"""Set the path and module id of the current file."""
180182
# The path will be simplified later, in render_messages. That way
181183
# * 'file' is always a key that uniquely identifies a source file
@@ -251,9 +253,15 @@ def set_import_context(self, ctx: List[Tuple[str, int]]) -> None:
251253
"""Replace the entire import context with a new value."""
252254
self.import_ctx = ctx[:]
253255

254-
def report(self, line: int, column: int, message: str, blocker: bool = False,
255-
severity: str = 'error', file: str = None, only_once: bool = False,
256-
origin_line: int = None) -> None:
256+
def report(self,
257+
line: int,
258+
column: int,
259+
message: str,
260+
blocker: bool = False,
261+
severity: str = 'error',
262+
file: Optional[str] = None,
263+
only_once: bool = False,
264+
origin_line: Optional[int] = None) -> None:
257265
"""Report message at the given line using the current error context.
258266
259267
Args:

mypy/fastparse.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@
6060
TYPE_COMMENT_AST_ERROR = 'invalid type comment or annotation'
6161

6262

63-
def parse(source: Union[str, bytes], fnam: str = None, errors: Errors = None,
63+
def parse(source: Union[str, bytes],
64+
fnam: Optional[str] = None,
65+
errors: Optional[Errors] = None,
6466
options: Options = Options()) -> MypyFile:
6567

6668
"""Parse a source file, without doing any semantic analysis.

mypy/fastparse2.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@
7373
TYPE_COMMENT_AST_ERROR = 'invalid type comment'
7474

7575

76-
def parse(source: Union[str, bytes], fnam: str = None, errors: Errors = None,
76+
def parse(source: Union[str, bytes],
77+
fnam: Optional[str] = None,
78+
errors: Optional[Errors] = None,
7779
options: Options = Options()) -> MypyFile:
7880
"""Parse a source file, without doing any semantic analysis.
7981

mypy/messages.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,26 +158,26 @@ def is_errors(self) -> bool:
158158
return self.errors.is_errors()
159159

160160
def report(self, msg: str, context: Context, severity: str,
161-
file: str = None, origin: Context = None) -> None:
161+
file: Optional[str] = None, origin: Optional[Context] = None) -> None:
162162
"""Report an error or note (unless disabled)."""
163163
if self.disable_count <= 0:
164164
self.errors.report(context.get_line() if context else -1,
165165
context.get_column() if context else -1,
166166
msg.strip(), severity=severity, file=file,
167167
origin_line=origin.get_line() if origin else None)
168168

169-
def fail(self, msg: str, context: Context, file: str = None,
170-
origin: Context = None) -> None:
169+
def fail(self, msg: str, context: Context, file: Optional[str] = None,
170+
origin: Optional[Context] = None) -> None:
171171
"""Report an error message (unless disabled)."""
172172
self.report(msg, context, 'error', file=file, origin=origin)
173173

174-
def note(self, msg: str, context: Context, file: str = None,
175-
origin: Context = None) -> None:
174+
def note(self, msg: str, context: Context, file: Optional[str] = None,
175+
origin: Optional[Context] = None) -> None:
176176
"""Report a note (unless disabled)."""
177177
self.report(msg, context, 'note', file=file, origin=origin)
178178

179-
def warn(self, msg: str, context: Context, file: str = None,
180-
origin: Context = None) -> None:
179+
def warn(self, msg: str, context: Context, file: Optional[str] = None,
180+
origin: Optional[Context] = None) -> None:
181181
"""Report a warning message (unless disabled)."""
182182
self.report(msg, context, 'warning', file=file, origin=origin)
183183

mypy/nodes.py

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def __init__(self, line: int = -1, column: int = -1) -> None:
2323
self.line = line
2424
self.column = column
2525

26-
def set_line(self, target: Union['Context', int], column: int = None) -> None:
26+
def set_line(self, target: Union['Context', int], column: Optional[int] = None) -> None:
2727
"""If target is a node, pull line (and column) information
2828
into this node. If column is specified, this will override any column
2929
information coming from a node.
@@ -254,7 +254,7 @@ def __init__(self,
254254
defs: List[Statement],
255255
imports: List['ImportBase'],
256256
is_bom: bool = False,
257-
ignored_lines: Set[int] = None) -> None:
257+
ignored_lines: Optional[Set[int]] = None) -> None:
258258
self.defs = defs
259259
self.line = 1 # Dummy line number
260260
self.imports = imports
@@ -445,7 +445,7 @@ class Argument(Node):
445445

446446
def __init__(self, variable: 'Var', type_annotation: 'Optional[mypy.types.Type]',
447447
initializer: Optional[Expression], kind: int,
448-
initialization_statement: Optional['AssignmentStmt'] = None) -> None:
448+
initialization_statement: 'Optional[AssignmentStmt]' = None) -> None:
449449
self.variable = variable
450450

451451
self.type_annotation = type_annotation
@@ -457,7 +457,7 @@ def __init__(self, variable: 'Var', type_annotation: 'Optional[mypy.types.Type]'
457457

458458
self.kind = kind
459459

460-
def _initialization_statement(self) -> Optional['AssignmentStmt']:
460+
def _initialization_statement(self) -> 'Optional[AssignmentStmt]':
461461
"""Convert the initializer into an assignment statement.
462462
"""
463463
if not self.initializer:
@@ -468,7 +468,7 @@ def _initialization_statement(self) -> Optional['AssignmentStmt']:
468468
assign = AssignmentStmt([lvalue], rvalue)
469469
return assign
470470

471-
def set_line(self, target: Union[Context, int], column: int = None) -> None:
471+
def set_line(self, target: Union[Context, int], column: Optional[int] = None) -> None:
472472
super().set_line(target, column)
473473

474474
if self.initializer:
@@ -507,7 +507,7 @@ class FuncItem(FuncBase):
507507
]
508508

509509
def __init__(self, arguments: List[Argument], body: 'Block',
510-
typ: 'mypy.types.FunctionLike' = None) -> None:
510+
typ: 'Optional[mypy.types.FunctionLike]' = None) -> None:
511511
self.arguments = arguments
512512
self.arg_names = [arg.variable.name() for arg in self.arguments]
513513
self.arg_kinds = [arg.kind for arg in self.arguments]
@@ -525,7 +525,7 @@ def __init__(self, arguments: List[Argument], body: 'Block',
525525
def max_fixed_argc(self) -> int:
526526
return self.max_pos
527527

528-
def set_line(self, target: Union[Context, int], column: int = None) -> None:
528+
def set_line(self, target: Union[Context, int], column: Optional[int] = None) -> None:
529529
super().set_line(target, column)
530530
for arg in self.arguments:
531531
arg.set_line(self.line, self.column)
@@ -554,7 +554,7 @@ def __init__(self,
554554
name: str, # Function name
555555
arguments: List[Argument],
556556
body: 'Block',
557-
typ: 'mypy.types.FunctionLike' = None) -> None:
557+
typ: 'Optional[mypy.types.FunctionLike]' = None) -> None:
558558
super().__init__(arguments, body, typ)
559559
self._name = name
560560

@@ -679,7 +679,7 @@ class Var(SymbolNode):
679679
'is_classvar'
680680
]
681681

682-
def __init__(self, name: str, type: 'mypy.types.Type' = None) -> None:
682+
def __init__(self, name: str, type: 'Optional[mypy.types.Type]' = None) -> None:
683683
self._name = name
684684
self.type = type
685685
if self.type is None:
@@ -738,10 +738,10 @@ class ClassDef(Statement):
738738
def __init__(self,
739739
name: str,
740740
defs: 'Block',
741-
type_vars: List['mypy.types.TypeVarDef'] = None,
742-
base_type_exprs: List[Expression] = None,
743-
metaclass: str = None,
744-
keywords: List[Tuple[str, Expression]] = None) -> None:
741+
type_vars: Optional[List['mypy.types.TypeVarDef']] = None,
742+
base_type_exprs: Optional[List[Expression]] = None,
743+
metaclass: Optional[str] = None,
744+
keywords: Optional[List[Tuple[str, Expression]]] = None) -> None:
745745
self.name = name
746746
self.defs = defs
747747
self.type_vars = type_vars or []
@@ -848,7 +848,7 @@ class AssignmentStmt(Statement):
848848
new_syntax = False # type: bool
849849

850850
def __init__(self, lvalues: List[Lvalue], rvalue: Expression,
851-
type: 'mypy.types.Type' = None, new_syntax: bool = False) -> None:
851+
type: 'Optional[mypy.types.Type]' = None, new_syntax: bool = False) -> None:
852852
self.lvalues = lvalues
853853
self.rvalue = rvalue
854854
self.type = type
@@ -900,8 +900,12 @@ class ForStmt(Statement):
900900
else_body = None # type: Optional[Block]
901901
is_async = False # True if `async for ...` (PEP 492, Python 3.5)
902902

903-
def __init__(self, index: Lvalue, expr: Expression, body: Block,
904-
else_body: Optional[Block], index_type: 'mypy.types.Type' = None) -> None:
903+
def __init__(self,
904+
index: Lvalue,
905+
expr: Expression,
906+
body: Block,
907+
else_body: Optional[Block],
908+
index_type: 'Optional[mypy.types.Type]' = None) -> None:
905909
self.index = index
906910
self.index_type = index_type
907911
self.expr = expr
@@ -926,7 +930,7 @@ class AssertStmt(Statement):
926930
expr = None # type: Expression
927931
msg = None # type: Optional[Expression]
928932

929-
def __init__(self, expr: Expression, msg: Expression = None) -> None:
933+
def __init__(self, expr: Expression, msg: Optional[Expression] = None) -> None:
930934
self.expr = expr
931935
self.msg = msg
932936

@@ -996,7 +1000,7 @@ class TryStmt(Statement):
9961000
else_body = None # type: Optional[Block]
9971001
finally_body = None # type: Optional[Block]
9981002

999-
def __init__(self, body: Block, vars: List[Optional['NameExpr']],
1003+
def __init__(self, body: Block, vars: List['Optional[NameExpr]'],
10001004
types: List[Optional[Expression]],
10011005
handlers: List[Block], else_body: Optional[Block],
10021006
finally_body: Optional[Block]) -> None:
@@ -1020,7 +1024,7 @@ class WithStmt(Statement):
10201024
is_async = False # True if `async with ...` (PEP 492, Python 3.5)
10211025

10221026
def __init__(self, expr: List[Expression], target: List[Optional[Lvalue]],
1023-
body: Block, target_type: 'mypy.types.Type' = None) -> None:
1027+
body: Block, target_type: 'Optional[mypy.types.Type]' = None) -> None:
10241028
self.expr = expr
10251029
self.target = target
10261030
self.target_type = target_type
@@ -1038,7 +1042,10 @@ class PrintStmt(Statement):
10381042
# The file-like target object (given using >>).
10391043
target = None # type: Optional[Expression]
10401044

1041-
def __init__(self, args: List[Expression], newline: bool, target: Expression = None) -> None:
1045+
def __init__(self,
1046+
args: List[Expression],
1047+
newline: bool,
1048+
target: Optional[Expression] = None) -> None:
10421049
self.args = args
10431050
self.newline = newline
10441051
self.target = target
@@ -1292,8 +1299,12 @@ class CallExpr(Expression):
12921299
# cast(...) this is a CastExpr.
12931300
analyzed = None # type: Optional[Expression]
12941301

1295-
def __init__(self, callee: Expression, args: List[Expression], arg_kinds: List[int],
1296-
arg_names: List[Optional[str]] = None, analyzed: Expression = None) -> None:
1302+
def __init__(self,
1303+
callee: Expression,
1304+
args: List[Expression],
1305+
arg_kinds: List[int],
1306+
arg_names: Optional[List[Optional[str]]] = None,
1307+
analyzed: Optional[Expression] = None) -> None:
12971308
if not arg_names:
12981309
arg_names = [None] * len(args)
12991310

@@ -1813,7 +1824,7 @@ class TypeAliasExpr(Expression):
18131824
in_runtime = False # type: bool
18141825

18151826
def __init__(self, type: 'mypy.types.Type', tvars: List[str],
1816-
fallback: 'mypy.types.Type' = None, in_runtime: bool = False) -> None:
1827+
fallback: 'Optional[mypy.types.Type]' = None, in_runtime: bool = False) -> None:
18171828
self.type = type
18181829
self.fallback = fallback
18191830
self.in_runtime = in_runtime
@@ -2038,14 +2049,14 @@ def is_generic(self) -> bool:
20382049
"""Is the type generic (i.e. does it have type variables)?"""
20392050
return len(self.type_vars) > 0
20402051

2041-
def get(self, name: str) -> Optional['SymbolTableNode']:
2052+
def get(self, name: str) -> 'Optional[SymbolTableNode]':
20422053
for cls in self.mro:
20432054
n = cls.names.get(name)
20442055
if n:
20452056
return n
20462057
return None
20472058

2048-
def get_containing_type_info(self, name: str) -> Optional['TypeInfo']:
2059+
def get_containing_type_info(self, name: str) -> 'Optional[TypeInfo]':
20492060
for cls in self.mro:
20502061
if name in cls.names:
20512062
return cls
@@ -2150,8 +2161,8 @@ def __str__(self) -> str:
21502161
return self.dump()
21512162

21522163
def dump(self,
2153-
str_conv: 'mypy.strconv.StrConv' = None,
2154-
type_str_conv: 'mypy.types.TypeStrVisitor' = None) -> str:
2164+
str_conv: 'Optional[mypy.strconv.StrConv]' = None,
2165+
type_str_conv: 'Optional[mypy.types.TypeStrVisitor]' = None) -> str:
21552166
"""Return a string dump of the contents of the TypeInfo."""
21562167
if not str_conv:
21572168
str_conv = mypy.strconv.StrConv()
@@ -2282,9 +2293,13 @@ class SymbolTableNode:
22822293
# Was this defined by assignment to self attribute?
22832294
implicit = False # type: bool
22842295

2285-
def __init__(self, kind: int, node: Optional[SymbolNode], mod_id: str = None,
2286-
typ: 'mypy.types.Type' = None,
2287-
module_public: bool = True, normalized: bool = False,
2296+
def __init__(self,
2297+
kind: int,
2298+
node: Optional[SymbolNode],
2299+
mod_id: Optional[str] = None,
2300+
typ: 'Optional[mypy.types.Type]' = None,
2301+
module_public: bool = True,
2302+
normalized: bool = False,
22882303
alias_tvars: Optional[List[str]] = None,
22892304
implicit: bool = False) -> None:
22902305
self.kind = kind

mypy/server/deps.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Generate fine-grained dependencies for AST nodes."""
22

3-
from typing import Dict, List, Set
3+
from typing import Dict, List, Set, Optional
44

55
from mypy.checkmember import bind_self
66
from mypy.nodes import (
@@ -151,7 +151,7 @@ def visit_call_expr(self, e: CallExpr) -> None:
151151

152152
# Helpers
153153

154-
def add_dependency(self, trigger: str, target: str = None) -> None:
154+
def add_dependency(self, trigger: str, target: Optional[str] = None) -> None:
155155
if target is None:
156156
target = self.current()
157157
self.map.setdefault(trigger, set()).add(target)

0 commit comments

Comments
 (0)