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 13 commits
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
41 changes: 33 additions & 8 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
from mypy.erasetype import erase_typevars
from mypy.expandtype import expand_type, expand_type_by_instance
from mypy.visitor import NodeVisitor
from mypy.join import join_types
from mypy.join import join_types, join_type_list
from mypy.treetransform import TransformVisitor
from mypy.binder import ConditionalTypeBinder, get_declaration
from mypy.meet import is_overlapping_types
Expand Down Expand Up @@ -1747,9 +1747,9 @@ def check_multi_assignment_from_iterable(self, lvalues: List[Lvalue], rvalue_typ
def check_lvalue(self, lvalue: Lvalue) -> Tuple[Optional[Type],
Optional[IndexExpr],
Optional[Var]]:
lvalue_type = None # type: Optional[Type]
index_lvalue = None # type: Optional[IndexExpr]
inferred = None # type: Optional[Var]
lvalue_type = None
index_lvalue = None
inferred = None

if self.is_definition(lvalue):
if isinstance(lvalue, NameExpr):
Expand Down Expand Up @@ -2702,12 +2702,10 @@ def iterable_item_type(self, instance: Instance) -> Type:
def function_type(self, func: FuncBase) -> FunctionLike:
return function_type(func, self.named_type('builtins.function'))

# TODO: These next two functions should refer to TypeMap below
def find_isinstance_check(self, n: Expression) -> Tuple[Optional[Dict[Expression, Type]],
Optional[Dict[Expression, Type]]]:
def find_isinstance_check(self, n: Expression) -> 'Tuple[TypeMap, TypeMap]':
return find_isinstance_check(n, self.type_map)

def push_type_map(self, type_map: Optional[Dict[Expression, Type]]) -> None:
def push_type_map(self, type_map: 'TypeMap') -> None:
if type_map is None:
self.binder.unreachable()
else:
Expand Down Expand Up @@ -2874,6 +2872,24 @@ def remove_optional(typ: Type) -> Type:
return typ


def builtin_item_type(tp: Type) -> Optional[Type]:
# This is only OK for built-in containers, where we know the behavior of __contains__.
if isinstance(tp, Instance):
if tp.type.fullname() in ['builtins.list', 'builtins.tuple', 'builtins.dict',
'builtins.set', 'builtins.frozenfet']:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

frozenset, not frozenfet

if not tp.args:
# TODO: make lib-stub/builtins.pyi define generic tuple.
return None
if not isinstance(tp.args[0], AnyType):
return tp.args[0]
elif isinstance(tp, TupleType) and all(not isinstance(it, AnyType) for it in tp.items):
return join_type_list(tp.items)
elif isinstance(tp, TypedDictType) and tp.fallback.type.fullname() == 'typing.Mapping':
# TypedDict always has non-optional string keys.
return tp.fallback.args[0]
return None


def and_conditional_maps(m1: TypeMap, m2: TypeMap) -> TypeMap:
"""Calculate what information we can learn from the truth of (e1 and e2)
in terms of the information that we can learn from the truth of e1 and
Expand Down Expand Up @@ -2986,6 +3002,15 @@ def find_isinstance_check(node: Expression,
if literal(expr) == LITERAL_TYPE:
vartype = type_map[expr]
return conditional_callable_type_map(expr, vartype)
elif isinstance(node, ComparisonExpr) and node.operators in [['in'], ['not in']]:
expr = node.operands[0]
cont_type = type_map[node.operands[1]]
item_type = builtin_item_type(cont_type)
if item_type and literal(expr) == LITERAL_TYPE and not is_literal_none(expr):
if node.operators == ['in']:
return {expr: item_type}, {}
if node.operators == ['not in']:
return {}, {expr: item_type}
elif isinstance(node, ComparisonExpr) and experiments.STRICT_OPTIONAL:
# Check for `x is None` and `x is not None`.
is_not = node.operators == ['is not']
Expand Down
8 changes: 4 additions & 4 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 All @@ -731,7 +730,8 @@ def infer_arg_types_in_context2(
for i, t in enumerate(res):
if not t:
res[i] = self.accept(args[i])
return res
assert all(tp is not None for tp in res)
return cast(List[Type], res)

def infer_function_type_arguments_using_context(
self, callable: CallableType, error_context: Context) -> CallableType:
Expand Down Expand Up @@ -2646,7 +2646,7 @@ def is_async_def(t: Type) -> bool:
def map_actuals_to_formals(caller_kinds: List[int],
caller_names: Optional[Sequence[Optional[str]]],
callee_kinds: List[int],
callee_names: List[Optional[str]],
callee_names: Sequence[Optional[str]],
caller_arg_type: Callable[[int],
Type]) -> List[List[int]]:
"""Calculate mapping between actual (caller) args and formals.
Expand Down
8 changes: 4 additions & 4 deletions mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from abc import abstractmethod
from collections import OrderedDict
from typing import (
Any, TypeVar, List, Tuple, cast, Set, Dict, Union, Optional, Callable,
Any, TypeVar, List, Tuple, cast, Set, Dict, Union, Optional, Callable, Sequence,
)

import mypy.strconv
Expand Down Expand Up @@ -1808,11 +1808,11 @@ def accept(self, visitor: ExpressionVisitor[T]) -> T:
class NewTypeExpr(Expression):
"""NewType expression NewType(...)."""
name = None # type: str
old_type = None # type: mypy.types.Type
old_type = None # type: Optional[mypy.types.Type]

info = None # type: Optional[TypeInfo]

def __init__(self, name: str, old_type: 'mypy.types.Type', line: int) -> None:
def __init__(self, name: str, old_type: 'Optional[mypy.types.Type]', line: int) -> None:
self.name = name
self.old_type = old_type

Expand Down Expand Up @@ -2544,7 +2544,7 @@ def check_arg_kinds(arg_kinds: List[int], nodes: List[T], fail: Callable[[str, T
is_kw_arg = True


def check_arg_names(names: List[Optional[str]], nodes: List[T], fail: Callable[[str, T], None],
def check_arg_names(names: Sequence[Optional[str]], nodes: List[T], fail: Callable[[str, T], None],
description: str = 'function definition') -> None:
seen_names = set() # type: Set[Optional[str]]
for name, node in zip(names, nodes):
Expand Down
Loading