8000 Last strict optional fixes by ilevkivskyi · Pull Request #4070 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Last strict optional fixes #4070

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

Merged
merged 22 commits into from
Oct 24, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
10000
Prev Previous commit
Next Next commit
Fix most signatures and other fixes
  • Loading branch information
ilevkivskyi committed Oct 7, 2017
commit b105456086c3e12bf73f144811e8ba471b1ef38c
3 changes: 1 addition & 2 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -719,8 +719,7 @@ def infer_arg_types_in_context2(

Returns the inferred types of *actual arguments*.
"""
dummy = None # type: Any
res = [dummy] * len(args) # type: List[Type]
res = [None] * len(args) # type: List[Optional[Type]]

for i, actuals in enumerate(formal_to_actual):
for ai in actuals:
Expand Down
53 changes: 26 additions & 27 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,7 @@ def analyze_typevar_declaration(self, t: Type) -> Optional[TypeVarList]:
return tvars
return None

def analyze_unbound_tvar(self, t: Type) -> Tuple[str, TypeVarExpr]:
def analyze_unbound_tvar(self, t: Type) -> Optional[Tuple[str, TypeVarExpr]]:
if not isinstance(t, UnboundType):
return None
unbound = t
Expand Down Expand Up @@ -1249,7 +1249,7 @@ def class_type(self, info: TypeInfo) -> Type:
else:
return leading_type

def named_type(self, qualified_name: str, args: List[Type] = None) -> Instance:
def named_type(self, qualified_name: str, args: Optional[List[Type]] = None) -> Instance:
sym = self.lookup_qualified(qualified_name, None)
assert sym, "Internal error: attempted to construct unknown type"
node = sym.node
Expand All @@ -1259,7 +1259,8 @@ def named_type(self, qualified_name: str, args: List[Type] = None) -> Instance:
return Instance(node, args)
return Instance(node, [AnyType(TypeOfAny.special_form)] * len(node.defn.type_vars))

def named_type_or_none(self, qualified_name: str, args: List[Type] = None) -> Instance:
def named_type_or_none(self, qualified_name: str,
args: Optional[List[Type]] = None) -> Optional[Instance]:
sym = self.lookup_fully_qualified_or_none(qualified_name)
if not sym:
return None
Expand Down Expand Up @@ -1337,9 +1338,9 @@ def analyze_typeddict_classdef(self, defn: ClassDef) -> bool:
return False

def check_typeddict_classdef(self, defn: ClassDef,
oldfields: List[str] = None) -> Tuple[List[str],
List[Type],
Set[str]]:
oldfields: Optional[List[str]] = None) -> Tuple[List[str],
List[Type],
Set[str]]:
TPDICT_CLASS_ERROR = ('Invalid statement in TypedDict definition; '
'expected "field_name: field_type"')
if self.options.python_version < (3, 6):
Expand Down Expand Up @@ -1622,7 +1623,7 @@ def visit_block(self, b: Block) -> None:
self.accept(s)
self.block_depth[-1] -= 1

def visit_block_maybe(self, b: Block) -> None:
def visit_block_maybe(self, b: Optional[Block]) -> None:
if b:
self.visit_block(b)

Expand Down Expand Up @@ -1654,16 +1655,11 @@ def anal_type(self, t: Type, *,
allow_tuple_literal: bool = False,
aliasing: bool = False,
third_pass: bool = False) -> Type:
if t:
a = self.type_analyzer(
tvar_scope=tvar_scope,
aliasing=aliasing,
allow_tuple_literal=allow_tuple_literal,
third_pass=third_pass)
return t.accept(a)

else:
return None
a = self.type_analyzer(tvar_scope=tvar_scope,
aliasing=aliasing,
allow_tuple_literal=allow_tuple_literal,
third_pass=third_pass)
return t.accept(a)

def visit_assignment_stmt(self, s: AssignmentStmt) -> None:
for lval in s.lvalues:
Expand Down Expand Up @@ -2260,7 +2256,8 @@ def process_namedtuple_definition(self, s: AssignmentStmt) -> None:
node.kind = GDEF # TODO locally defined namedtuple
node.node = named_tuple

def check_namedtuple(self, node: Expression, var_name: str = None) -> Optional[TypeInfo]:
def check_namedtuple(self, node: Expression,
var_name: Optional[str] = None) -> Optional[TypeInfo]:
"""Check if a call defines a namedtuple.

The optional var_name argument is the name of the variable to
Expand All @@ -2280,6 +2277,7 @@ def check_namedtuple(self, node: Expression, var_name: str = None) -> Optional[T
fullname = callee.fullname
if fullname not in ('collections.namedtuple', 'typing.NamedTuple'):
return None
assert fullname
items, types, ok = self.parse_namedtuple_args(call, fullname)
if not ok:
# Error. Construct dummy return value.
Expand Down Expand Up @@ -2440,7 +2438,7 @@ def add_field(var: Var, is_initialized_in_class: bool = False,
def add_method(funcname: str,
ret: Type,
args: List[Argument],
name: str = None,
name: Optional[str] = None,
is_classmethod: bool = False,
) -> None:
if is_classmethod:
Expand Down Expand Up @@ -2513,7 +2511,8 @@ def process_typeddict_definition(self, s: AssignmentStmt) -> None:
node.kind = GDEF # TODO locally defined TypedDict
node.node = typed_dict

def check_typeddict(self, node: Expression, var_name: str = None) -> Optional[TypeInfo]:
def check_typeddict(self, node: Expression,
var_name: Optional[str] = None) -> Optional[TypeInfo]:
"""Check if a call defines a TypedDict.

The optional var_name argument is the name of the variable to
Expand All @@ -2533,7 +2532,7 @@ def check_typeddict(self, node: Expression, var_name: str = None) -> Optional[Ty
fullname = callee.fullname
if fullname != 'mypy_extensions.TypedDict':
return None
items, types, total, ok = self.parse_typeddict_args(call, fullname)
items, types, total, ok = self.parse_typeddict_args(call)
if not ok:
# Error. Construct dummy return value.
info = self.build_typeddict_typeinfo('TypedDict', [], [], set())
Expand All @@ -2559,8 +2558,7 @@ def check_typeddict(self, node: Expression, var_name: str = None) -> Optional[Ty
call.analyzed.set_line(call.line, call.column)
return info

def parse_typeddict_args(self, call: CallExpr,
fullname: str) -> Tuple[List[str], List[Type], bool, bool]:
def parse_typeddict_args(self, call: CallExpr) -> Tuple[List[str], List[Type], bool, bool]:
# TODO: Share code with check_argument_count in checkexpr.py?
args = call.args
if len(args) < 2:
Expand Down Expand Up @@ -2663,7 +2661,7 @@ def check_classvar(self, s: AssignmentStmt) -> None:
# Other kinds of member assignments should be already reported
self.fail_invalid_classvar(lvalue)

def is_classvar(self, typ: Type) -> bool:
def is_classvar(self, typ: Optional[Type]) -> bool:
if not isinstance(typ, UnboundType):
return False
sym = self.lookup_qualified(typ.name, typ)
Expand Down Expand Up @@ -2749,7 +2747,8 @@ def process_enum_call(self, s: AssignmentStmt) -> None:
node.kind = GDEF # TODO locally defined Enum
node.node = enum_call

def check_enum_call(self, node: Expression, var_name: str = None) -> Optional[TypeInfo]:
def check_enum_call(self, node: Expression,
var_name: Optional[str] = None) -> Optional[TypeInfo]:
"""Check if a call defines an Enum.

Example:
Expand Down Expand Up @@ -4407,7 +4406,7 @@ def fail(self, msg: str, ctx: Context, *, blocker: bool = False) -> None:
def fail_blocker(self, msg: str, ctx: Context) -> None:
self.fail(msg, ctx, blocker=True)

def builtin_type(self, name: str, args: List[Type] = None) -> Instance:
def builtin_type(self, name: str, args: Optional[List[Type]] = None) -> Instance:
names = self.modules['builtins']
sym = names.names[name]
node = sym.node
Expand Down Expand Up @@ -4472,7 +4471,7 @@ def calculate_class_mro(defn: ClassDef, fail: Callable[[str, Context], None]) ->
defn.info.fallback_to_any = any(baseinfo.fallback_to_any for baseinfo in defn.info.mro)


def find_duplicate(list: List[T]) -> T:
def find_duplicate(list: List[T]) -> Optional[T]:
"""If the list has duplicates, return one of the duplicates.

Otherwise, return None.
Expand Down
0