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

[Reverted] Fixes to union simplification, isinstance and more #3025

Merged
merged 19 commits into from
Mar 29, 2017
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
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
77 changes: 39 additions & 38 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
Type, AnyType, CallableType, FunctionLike, Overloaded, TupleType, TypedDictType,
Instance, NoneTyp, ErrorType, strip_type, TypeType,
UnionType, TypeVarId, TypeVarType, PartialType, DeletedType, UninhabitedType, TypeVarDef,
true_only, false_only, function_type, is_named_instance
true_only, false_only, function_type, is_named_instance, union_items
)
from mypy.sametypes import is_same_type, is_same_types
from mypy.messages import MessageBuilder
Expand Down Expand Up @@ -768,44 +768,45 @@ def check_overlapping_op_methods(self,
# of x in __radd__ would not be A, the methods could be
# non-overlapping.

if isinstance(forward_type, CallableType):
# TODO check argument kinds
if len(forward_type.arg_types) < 1:
# Not a valid operator method -- can't succeed anyway.
return
for forward_item in union_items(forward_type):
if isinstance(forward_item, CallableType):
# TODO check argument kinds
if len(forward_item.arg_types) < 1:
# Not a valid operator method -- can't succeed anyway.
return

# Construct normalized function signatures corresponding to the
# operator methods. The first argument is the left operand and the
# second operand is the right argument -- we switch the order of
# the arguments of the reverse method.
forward_tweaked = CallableType(
[forward_base, forward_type.arg_types[0]],
[nodes.ARG_POS] * 2,
[None] * 2,
forward_type.ret_type,
forward_type.fallback,
name=forward_type.name)
reverse_args = reverse_type.arg_types
reverse_tweaked = CallableType(
[reverse_args[1], reverse_args[0]],
[nodes.ARG_POS] * 2,
[None] * 2,
reverse_type.ret_type,
fallback=self.named_type('builtins.function'),
name=reverse_type.name)

if is_unsafe_overlapping_signatures(forward_tweaked,
reverse_tweaked):
self.msg.operator_method_signatures_overlap(
reverse_class.name(), reverse_name,
forward_base.type.name(), forward_name, context)
elif isinstance(forward_type, Overloaded):
for item in forward_type.items():
self.check_overlapping_op_methods(
reverse_type, reverse_name, reverse_class,
item, forward_name, forward_base, context)
elif not isinstance(forward_type, AnyType):
self.msg.forward_operator_not_callable(forward_name, context)
# Construct normalized function signatures corresponding to the
# operator methods. The first argument is the left operand and the
# second operand is the right argument -- we switch the order of
# the arguments of the reverse method.
forward_tweaked = CallableType(
[forward_base, forward_item.arg_types[0]],
[nodes.ARG_POS] * 2,
[None] * 2,
forward_item.ret_type,
forward_item.fallback,
name=forward_item.name)
reverse_args = reverse_type.arg_types
reverse_tweaked = CallableType(
[reverse_args[1], reverse_args[0]],
[nodes.ARG_POS] * 2,
[None] * 2,
reverse_type.ret_type,
fallback=self.named_type('builtins.function'),
name=reverse_type.name)

if is_unsafe_overlapping_signatures(forward_tweaked,
reverse_tweaked):
self.msg.operator_method_signatures_overlap(
reverse_class.name(), reverse_name,
forward_base.type.name(), forward_name, context)
elif isinstance(forward_item, Overloaded):
for item in forward_item.items():
self.check_overlapping_op_methods(
reverse_type, reverse_name, reverse_class,
item, forward_name, forward_base, context)
elif not isinstance(forward_item, AnyType):
self.msg.forward_operator_not_callable(forward_name, context)

def check_inplace_operator_method(self, defn: FuncBase) -> None:
"""Check an inplace operator method such as __iadd__.
Expand Down
4 changes: 2 additions & 2 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from mypy import messages
from mypy.infer import infer_type_arguments, infer_function_type_arguments
from mypy import join
from mypy.meet import meet_simple
from mypy.meet import narrow_declared_type
from mypy.maptype import map_instance_to_supertype
from mypy.subtypes import is_subtype, is_equivalent
from mypy import applytype
Expand Down Expand Up @@ -2213,7 +2213,7 @@ def narrow_type_from_binder(self, expr: Expression, known_type: Type) -> Type:
if expr.literal >= LITERAL_TYPE:
restriction = self.chk.binder.get(expr)
if restriction:
ans = meet_simple(known_type, restriction)
ans = narrow_declared_type(known_type, restriction)
return ans
return known_type

Expand Down
3 changes: 2 additions & 1 deletion mypy/erasetype.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ def visit_typeddict_type(self, t: TypedDictType) -> Type:
return t.fallback.accept(self)

def visit_union_type(self, t: UnionType) -> Type:
return AnyType() # XXX: return underlying type if only one?
erased_items = [erase_type(item) for item in t.items]
return UnionType.make_simplified_union(erased_items)

def visit_type_type(self, t: TypeType) -> Type:
return TypeType(t.item.accept(self), line=t.line)
Expand Down
6 changes: 3 additions & 3 deletions mypy/join.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
UninhabitedType, TypeType, true_or_false
)
from mypy.maptype import map_instance_to_supertype
from mypy.subtypes import is_subtype, is_equivalent, is_subtype_ignoring_tvars
from mypy.subtypes import is_subtype, is_equivalent, is_subtype_ignoring_tvars, is_proper_subtype

from mypy import experiments

Expand All @@ -29,10 +29,10 @@ def join_simple(declaration: Type, s: Type, t: Type) -> Type:
if isinstance(s, ErasedType):
return t

if is_subtype(s, t):
if is_proper_subtype(s, t):
return t

if is_subtype(t, s):
if is_proper_subtype(t, s):
return s

if isinstance(declaration, UnionType):
Expand Down
31 changes: 20 additions & 11 deletions mypy/meet.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,26 @@ def meet_types(s: Type, t: Type) -> Type:
return t.accept(TypeMeetVisitor(s))


def meet_simple(s: Type, t: Type, default_right: bool = True) -> Type:
if s == t:
return s
if isinstance(s, UnionType):
return UnionType.make_simplified_union([meet_types(x, t) for x in s.items])
elif not is_overlapping_types(s, t, use_promotions=True):
def narrow_declared_type(declared: Type, narrowed: Type) -> Type:
"""Return the declared type narrowed down to another type."""
if declared == narrowed:
return declared
if isinstance(declared, UnionType):
return UnionType.make_simplified_union([narrow_declared_type(x, narrowed)
for x in declared.items])
elif not is_overlapping_types(declared, narrowed, use_promotions=True):
if experiments.STRICT_OPTIONAL:
return UninhabitedType()
else:
return NoneTyp()
else:
if default_right:
return t
else:
return s
elif isinstance(narrowed, UnionType):
return UnionType.make_simplified_union([narrow_declared_type(declared, x)
for x in narrowed.items])
elif isinstance(narrowed, AnyType):
return narrowed
elif isinstance(declared, (Instance, TupleType)):
return meet_types(declared, narrowed)
return narrowed


def is_overlapping_types(t: Type, s: Type, use_promotions: bool = False) -> bool:
Expand Down Expand Up @@ -248,6 +253,10 @@ def visit_tuple_type(self, t: TupleType) -> Type:
elif (isinstance(self.s, Instance) and
self.s.type.fullname() == 'builtins.tuple' and self.s.args):
return t.copy_modified(items=[meet_types(it, self.s.args[0]) for it in t.items])
elif (isinstance(self.s, Instance) and t.fallback.type == self.s.type):
# Uh oh, a broken named tuple type (https://github.com/python/mypy/issues/3016).
# Do something reasonable until that bug is fixed.
return t
else:
return self.default(self.s)

Expand Down
Loading