Skip to content
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

Untangle most of the big import cycle #7397

Merged
merged 8 commits into from Aug 27, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 15 additions & 15 deletions mypy/checker.py
Expand Up @@ -48,7 +48,7 @@
analyze_descriptor_access, type_object_type,
)
from mypy.typeops import (
map_type_from_supertype, bind_self, erase_to_bound,
map_type_from_supertype, bind_self, erase_to_bound, make_simplified_union,
)
from mypy import message_registry
from mypy.subtypes import (
Expand Down Expand Up @@ -1541,7 +1541,7 @@ def get_op_other_domain(self, tp: FunctionLike) -> Optional[Type]:
raw_items = [self.get_op_other_domain(it) for it in tp.items()]
items = [it for it in raw_items if it]
if items:
return UnionType.make_simplified_union(items)
return make_simplified_union(items)
return None
else:
assert False, "Need to check all FunctionLike subtypes here"
Expand Down Expand Up @@ -1975,7 +1975,7 @@ def check_assignment(self, lvalue: Lvalue, rvalue: Expression, infer_lvalue_type
if not self.current_node_deferred:
# Partial type can't be final, so strip any literal values.
rvalue_type = remove_instance_last_known_values(rvalue_type)
inferred_type = UnionType.make_simplified_union(
inferred_type = make_simplified_union(
[rvalue_type, NoneType()])
self.set_inferred_type(var, lvalue, inferred_type)
else:
Expand Down Expand Up @@ -2419,7 +2419,7 @@ def check_multi_assignment_from_union(self, lvalues: List[Expression], rvalue: E
rv_type=item, undefined_rvalue=True)
for t, lv in zip(transposed, self.flatten_lvalues(lvalues)):
t.append(self.type_map.pop(lv, AnyType(TypeOfAny.special_form)))
union_types = tuple(UnionType.make_simplified_union(col) for col in transposed)
union_types = tuple(make_simplified_union(col) for col in transposed)
for expr, items in assignments.items():
# Bind a union of types collected in 'assignments' to every expression.
if isinstance(expr, StarExpr):
Expand All @@ -2434,8 +2434,8 @@ def check_multi_assignment_from_union(self, lvalues: List[Expression], rvalue: E

types, declared_types = zip(*clean_items)
self.binder.assign_type(expr,
UnionType.make_simplified_union(list(types)),
UnionType.make_simplified_union(list(declared_types)),
make_simplified_union(list(types)),
make_simplified_union(list(declared_types)),
False)
for union, lv in zip(union_types, self.flatten_lvalues(lvalues)):
# Properly store the inferred types.
Expand Down Expand Up @@ -2858,9 +2858,9 @@ def try_infer_partial_type_from_indexed_assignment(
# TODO: Don't infer things twice.
key_type = self.expr_checker.accept(lvalue.index)
value_type = self.expr_checker.accept(rvalue)
full_key_type = UnionType.make_simplified_union(
full_key_type = make_simplified_union(
[key_type, var.type.inner_types[0]])
full_value_type = UnionType.make_simplified_union(
full_value_type = make_simplified_union(
[value_type, var.type.inner_types[1]])
if (is_valid_inferred_type(full_key_type) and
is_valid_inferred_type(full_value_type)):
Expand Down Expand Up @@ -3178,7 +3178,7 @@ def check_except_handler_test(self, n: Expression) -> Type:

all_types.append(exc_type)

return UnionType.make_simplified_union(all_types)
return make_simplified_union(all_types)

def get_types_from_except_handler(self, typ: Type, n: Expression) -> List[Type]:
"""Helper for check_except_handler_test to retrieve handler types."""
Expand Down Expand Up @@ -4089,7 +4089,7 @@ def conditional_type_map(expr: Expression,
if it was not the proposed type, if any. None means bot, {} means top"""
if proposed_type_ranges:
proposed_items = [type_range.item for type_range in proposed_type_ranges]
proposed_type = UnionType.make_simplified_union(proposed_items)
proposed_type = make_simplified_union(proposed_items)
if current_type:
if isinstance(proposed_type, AnyType):
# We don't really know much about the proposed type, so we shouldn't
Expand Down Expand Up @@ -4172,7 +4172,7 @@ def builtin_item_type(tp: Type) -> Optional[Type]:
return tp.args[0]
elif isinstance(tp, TupleType) and all(not isinstance(it, AnyType)
for it in get_proper_types(tp.items)):
return UnionType.make_simplified_union(tp.items) # this type is not externally visible
return make_simplified_union(tp.items) # this type is not externally visible
elif isinstance(tp, TypedDictType):
# TypedDict always has non-optional string keys. Find the key type from the Mapping
# base class.
Expand Down Expand Up @@ -4223,7 +4223,7 @@ def or_conditional_maps(m1: TypeMap, m2: TypeMap) -> TypeMap:
for n1 in m1:
for n2 in m2:
if literal_hash(n1) == literal_hash(n2):
result[n1] = UnionType.make_simplified_union([m1[n1], m2[n2]])
result[n1] = make_simplified_union([m1[n1], m2[n2]])
return result


Expand Down Expand Up @@ -4691,7 +4691,7 @@ class Status(Enum):

if isinstance(typ, UnionType):
items = [try_expanding_enum_to_union(item, target_fullname) for item in typ.items]
return UnionType.make_simplified_union(items)
return make_simplified_union(items)
elif isinstance(typ, Instance) and typ.type.is_enum and typ.type.fullname() == target_fullname:
new_items = []
for name, symbol in typ.type.names.items():
Expand All @@ -4706,7 +4706,7 @@ class Status(Enum):
# only using CPython, but we might as well for the sake of full correctness.
if sys.version_info < (3, 7):
new_items.sort(key=lambda lit: lit.value)
return UnionType.make_simplified_union(new_items)
return make_simplified_union(new_items)
else:
return typ

Expand All @@ -4718,7 +4718,7 @@ def coerce_to_literal(typ: Type) -> ProperType:
typ = get_proper_type(typ)
if isinstance(typ, UnionType):
new_items = [coerce_to_literal(item) for item in typ.items]
return UnionType.make_simplified_union(new_items)
return make_simplified_union(new_items)
elif isinstance(typ, Instance) and typ.last_known_value:
return typ.last_known_value
else:
Expand Down
38 changes: 19 additions & 19 deletions mypy/checkexpr.py
Expand Up @@ -59,7 +59,7 @@
from mypy.typevars import fill_typevars
from mypy.visitor import ExpressionVisitor
from mypy.plugin import Plugin, MethodContext, MethodSigContext, FunctionContext
from mypy.typeops import tuple_fallback
from mypy.typeops import tuple_fallback, make_simplified_union
import mypy.errorcodes as codes

# Type of callback user for checking individual function arguments. See
Expand Down Expand Up @@ -528,7 +528,7 @@ def try_infer_partial_type(self, e: CallExpr) -> None:
if (typename in self.item_args and methodname in self.item_args[typename]
and e.arg_kinds == [ARG_POS]):
item_type = self.accept(e.args[0])
full_item_type = UnionType.make_simplified_union(
full_item_type = make_simplified_union(
[item_type, partial_type.inner_types[0]])
if mypy.checker.is_valid_inferred_type(full_item_type):
var.type = self.chk.named_generic_type(typename, [full_item_type])
Expand All @@ -541,7 +541,7 @@ def try_infer_partial_type(self, e: CallExpr) -> None:
arg_typename = arg_type.type.fullname()
if arg_typename in self.container_args[typename][methodname]:
full_item_types = [
UnionType.make_simplified_union([item_type, prev_type])
make_simplified_union([item_type, prev_type])
for item_type, prev_type
in zip(arg_type.args, partial_type.inner_types)
]
Expand Down Expand Up @@ -713,7 +713,7 @@ def check_union_call_expr(self, e: CallExpr, object_type: UnionType, member: str
item_object_type = typ if callable_name else None
res.append(self.check_call_expr_with_callee_type(narrowed, e, callable_name,
item_object_type))
return UnionType.make_simplified_union(res)
return make_simplified_union(res)

def check_call(self,
callee: Type,
Expand Down Expand Up @@ -1377,7 +1377,7 @@ def check_overload_call(self,
# a union of inferred callables because for example a call
# Union[int -> int, str -> str](Union[int, str]) is invalid and
# we don't want to introduce internal inconsistencies.
unioned_result = (UnionType.make_simplified_union(list(returns),
unioned_result = (make_simplified_union(list(returns),
context.line,
context.column),
self.combine_function_signatures(inferred_types))
Expand Down Expand Up @@ -1736,7 +1736,7 @@ def combine_function_signatures(self, types: Sequence[Type]) -> Union[AnyType, C
new_args[i].append(arg)
new_returns.append(target.ret_type)

union_return = UnionType.make_simplified_union(new_returns)
union_return = make_simplified_union(new_returns)
if too_complex:
any = AnyType(TypeOfAny.special_form)
return callables[0].copy_modified(
Expand All @@ -1749,7 +1749,7 @@ def combine_function_signatures(self, types: Sequence[Type]) -> Union[AnyType, C

final_args = []
for args_list in new_args:
new_type = UnionType.make_simplified_union(args_list)
new_type = make_simplified_union(args_list)
final_args.append(new_type)

return callables[0].copy_modified(
Expand Down Expand Up @@ -1818,7 +1818,7 @@ def check_union_call(self,
arg_messages=arg_messages)
for subtype in callee.relevant_items()]
self.msg.disable_type_names -= 1
return (UnionType.make_simplified_union([res[0] for res in results]),
return (make_simplified_union([res[0] for res in results]),
callee)

def visit_member_expr(self, e: MemberExpr, is_lvalue: bool = False) -> Type:
Expand Down Expand Up @@ -2168,7 +2168,7 @@ def check_union_method_call_by_name(self,
local_errors.disable_type_names -= 1
res.append(item)
meth_res.append(meth_item)
return UnionType.make_simplified_union(res), UnionType.make_simplified_union(meth_res)
return make_simplified_union(res), make_simplified_union(meth_res)

def check_method_call(self,
method_name: str,
Expand Down Expand Up @@ -2425,8 +2425,8 @@ def check_op(self, method: str, base_type: Type,
all_inferred.append(inferred)

if not msg.is_errors():
results_final = UnionType.make_simplified_union(all_results)
inferred_final = UnionType.make_simplified_union(all_inferred)
results_final = make_simplified_union(all_results)
inferred_final = make_simplified_union(all_inferred)
return results_final, inferred_final

# Step 2: If that fails, we try again but also destructure the right argument.
Expand Down Expand Up @@ -2475,7 +2475,7 @@ def check_op(self, method: str, base_type: Type,
# See the comment in 'check_overload_call' for more details on why
# we call 'combine_function_signature' instead of just unioning the inferred
# callable types.
results_final = UnionType.make_simplified_union(all_results)
results_final = make_simplified_union(all_results)
inferred_final = self.combine_function_signatures(all_inferred)
return results_final, inferred_final
else:
Expand Down Expand Up @@ -2560,7 +2560,7 @@ def check_boolean_op(self, e: OpExpr, context: Context) -> Type:
# The left operand is always the result
return left_type
else:
return UnionType.make_simplified_union([restricted_left_type, right_type])
return make_simplified_union([restricted_left_type, right_type])

def check_list_multiply(self, e: OpExpr) -> Type:
"""Type check an expression of form '[...] * e'.
Expand Down Expand Up @@ -2626,7 +2626,7 @@ def visit_index_with_type(self, left_type: Type, e: IndexExpr,

if isinstance(left_type, UnionType):
original_type = original_type or left_type
return UnionType.make_simplified_union([self.visit_index_with_type(typ, e,
return make_simplified_union([self.visit_index_with_type(typ, e,
original_type)
for typ in left_type.relevant_items()])
elif isinstance(left_type, TupleType) and self.chk.in_checked_function():
Expand All @@ -2646,7 +2646,7 @@ def visit_index_with_type(self, left_type: Type, e: IndexExpr,
else:
self.chk.fail(message_registry.TUPLE_INDEX_OUT_OF_RANGE, e)
return AnyType(TypeOfAny.from_error)
return UnionType.make_simplified_union(out)
return make_simplified_union(out)
else:
return self.nonliteral_tuple_index_helper(left_type, index)
elif isinstance(left_type, TypedDictType):
Expand Down Expand Up @@ -2687,7 +2687,7 @@ def visit_tuple_slice_helper(self, left_type: TupleType, slic: SliceExpr) -> Typ
items = [] # type: List[Type]
for b, e, s in itertools.product(begin, end, stride):
items.append(left_type.slice(b, e, s))
return UnionType.make_simplified_union(items)
return make_simplified_union(items)

def try_getting_int_literals(self, index: Expression) -> Optional[List[int]]:
"""If the given expression or type corresponds to an int literal
Expand Down Expand Up @@ -2732,7 +2732,7 @@ def nonliteral_tuple_index_helper(self, left_type: TupleType, index: Expression)
'actual type', 'expected type'):
return AnyType(TypeOfAny.from_error)
else:
union = UnionType.make_simplified_union(left_type.items)
union = make_simplified_union(left_type.items)
if isinstance(index, SliceExpr):
return self.chk.named_generic_type('builtins.tuple', [union])
else:
Expand Down Expand Up @@ -2767,7 +2767,7 @@ def visit_typeddict_index_expr(self, td_type: TypedDictType, index: Expression)
return AnyType(TypeOfAny.from_error)
else:
value_types.append(value_type)
return UnionType.make_simplified_union(value_types)
return make_simplified_union(value_types)

def visit_enum_index_expr(self, enum_type: TypeInfo, index: Expression,
context: Context) -> Type:
Expand Down Expand Up @@ -3471,7 +3471,7 @@ def visit_conditional_expr(self, e: ConditionalExpr) -> Type:
#
# TODO: Always create a union or at least in more cases?
if isinstance(get_proper_type(self.type_context[-1]), UnionType):
res = UnionType.make_simplified_union([if_type, else_type])
res = make_simplified_union([if_type, else_type])
else:
res = join.join_types(if_type, else_type)

Expand Down
7 changes: 4 additions & 3 deletions mypy/checkmember.py
Expand Up @@ -25,7 +25,8 @@
from mypy import subtypes
from mypy import meet
from mypy.typeops import (
tuple_fallback, bind_self, erase_to_bound, class_callable, type_object_type_from_function
tuple_fallback, bind_self, erase_to_bound, class_callable, type_object_type_from_function,
make_simplified_union,
)

if TYPE_CHECKING: # import for forward declaration only
Expand Down Expand Up @@ -292,7 +293,7 @@ def analyze_union_member_access(name: str, typ: UnionType, mx: MemberContext) ->
item_mx = mx.copy_modified(self_type=subtype)
results.append(_analyze_member_access(name, subtype, item_mx))
mx.msg.disable_type_names -= 1
return UnionType.make_simplified_union(results)
return make_simplified_union(results)


def analyze_none_member_access(name: str, typ: NoneType, mx: MemberContext) -> Type:
Expand Down Expand Up @@ -435,7 +436,7 @@ def analyze_descriptor_access(instance_type: Type,

if isinstance(descriptor_type, UnionType):
# Map the access over union types
return UnionType.make_simplified_union([
return make_simplified_union([
analyze_descriptor_access(instance_type, typ, builtin_type,
msg, context, chk=chk)
for typ in descriptor_type.items
Expand Down
5 changes: 4 additions & 1 deletion mypy/erasetype.py
Expand Up @@ -9,6 +9,7 @@
from mypy.nodes import ARG_STAR, ARG_STAR2



def erase_type(typ: Type) -> ProperType:
"""Erase any type variables from a type.

Expand Down Expand Up @@ -87,7 +88,9 @@ def visit_literal_type(self, t: LiteralType) -> ProperType:

def visit_union_type(self, t: UnionType) -> ProperType:
erased_items = [erase_type(item) for item in t.items]
return UnionType.make_simplified_union(erased_items)
from mypy.typeops import make_simplified_union # asdf
# XXX: does this need to be simplified?
Copy link
Member

Choose a reason for hiding this comment

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

I think sometimes it may be useful. For example we probably want Union[List[T], List[S]] become just List[Any], not Union[List[Any], List[Any]]. I think a similar applies to the question below.

return make_simplified_union(erased_items)

def visit_type_type(self, t: TypeType) -> ProperType:
return TypeType.make_normalized(t.item.accept(self), line=t.line)
Expand Down
4 changes: 3 additions & 1 deletion mypy/expandtype.py
Expand Up @@ -119,7 +119,9 @@ def visit_literal_type(self, t: LiteralType) -> ProperType:
def visit_union_type(self, t: UnionType) -> ProperType:
# After substituting for type variables in t.items,
# some of the resulting types might be subtypes of others.
return UnionType.make_simplified_union(self.expand_types(t.items), t.line, t.column)
from mypy.typeops import make_simplified_union # asdf
# XXX: does this need to be simplified?
return make_simplified_union(self.expand_types(t.items), t.line, t.column)

def visit_partial_type(self, t: PartialType) -> ProperType:
return t
Expand Down
6 changes: 3 additions & 3 deletions mypy/join.py
Expand Up @@ -43,7 +43,7 @@ def join_simple(declaration: Optional[Type], s: Type, t: Type) -> ProperType:
return s

if isinstance(declaration, UnionType):
return UnionType.make_simplified_union([s, t])
return mypy.typeops.make_simplified_union([s, t])

if isinstance(s, NoneType) and not isinstance(t, NoneType):
s, t = t, s
Expand Down Expand Up @@ -107,7 +107,7 @@ def visit_union_type(self, t: UnionType) -> ProperType:
if is_subtype(self.s, t):
return t
else:
return UnionType.make_simplified_union([self.s, t])
return mypy.typeops.make_simplified_union([self.s, t])

def visit_any(self, t: AnyType) -> ProperType:
return t
Expand All @@ -119,7 +119,7 @@ def visit_none_type(self, t: NoneType) -> ProperType:
elif isinstance(self.s, UnboundType):
return AnyType(TypeOfAny.special_form)
else:
return UnionType.make_simplified_union([self.s, t])
return mypy.typeops.make_simplified_union([self.s, t])
else:
return self.s

Expand Down