1from contextlib import contextmanager
2
3from typing import Any, List, Optional, Callable, Tuple, Iterator, Set, Union, cast, TypeVar
4from typing_extensions import Final
5
6from mypy.types import (
7    Type, AnyType, TypeGuardType, UnboundType, TypeVisitor, FormalArgument, NoneType,
8    Instance, TypeVarType, CallableType, TupleType, TypedDictType, UnionType, Overloaded,
9    ErasedType, PartialType, DeletedType, UninhabitedType, TypeType, is_named_instance,
10    FunctionLike, TypeOfAny, LiteralType, get_proper_type, TypeAliasType
11)
12import mypy.applytype
13import mypy.constraints
14import mypy.typeops
15import mypy.sametypes
16from mypy.erasetype import erase_type
17# Circular import; done in the function instead.
18# import mypy.solve
19from mypy.nodes import (
20    FuncBase, Var, Decorator, OverloadedFuncDef, TypeInfo, CONTRAVARIANT, COVARIANT,
21    ARG_POS, ARG_OPT, ARG_STAR, ARG_STAR2
22)
23from mypy.maptype import map_instance_to_supertype
24from mypy.expandtype import expand_type_by_instance
25from mypy.typestate import TypeState, SubtypeKind
26from mypy import state
27
28# Flags for detected protocol members
29IS_SETTABLE = 1  # type: Final
30IS_CLASSVAR = 2  # type: Final
31IS_CLASS_OR_STATIC = 3  # type: Final
32
33TypeParameterChecker = Callable[[Type, Type, int], bool]
34
35
36def check_type_parameter(lefta: Type, righta: Type, variance: int) -> bool:
37    if variance == COVARIANT:
38        return is_subtype(lefta, righta)
39    elif variance == CONTRAVARIANT:
40        return is_subtype(righta, lefta)
41    else:
42        return is_equivalent(lefta, righta)
43
44
45def ignore_type_parameter(s: Type, t: Type, v: int) -> bool:
46    return True
47
48
49def is_subtype(left: Type, right: Type,
50               *,
51               ignore_type_params: bool = False,
52               ignore_pos_arg_names: bool = False,
53               ignore_declared_variance: bool = False,
54               ignore_promotions: bool = False) -> bool:
55    """Is 'left' subtype of 'right'?
56
57    Also consider Any to be a subtype of any type, and vice versa. This
58    recursively applies to components of composite types (List[int] is subtype
59    of List[Any], for example).
60
61    type_parameter_checker is used to check the type parameters (for example,
62    A with B in is_subtype(C[A], C[B]). The default checks for subtype relation
63    between the type arguments (e.g., A and B), taking the variance of the
64    type var into account.
65    """
66    if TypeState.is_assumed_subtype(left, right):
67        return True
68    if (isinstance(left, TypeAliasType) and isinstance(right, TypeAliasType) and
69            left.is_recursive and right.is_recursive):
70        # This case requires special care because it may cause infinite recursion.
71        # Our view on recursive types is known under a fancy name of equirecursive mu-types.
72        # Roughly this means that a recursive type is defined as an alias where right hand side
73        # can refer to the type as a whole, for example:
74        #     A = Union[int, Tuple[A, ...]]
75        # and an alias unrolled once represents the *same type*, in our case all these represent
76        # the same type:
77        #    A
78        #    Union[int, Tuple[A, ...]]
79        #    Union[int, Tuple[Union[int, Tuple[A, ...]], ...]]
80        # The algorithm for subtyping is then essentially under the assumption that left <: right,
81        # check that get_proper_type(left) <: get_proper_type(right). On the example above,
82        # If we start with:
83        #     A = Union[int, Tuple[A, ...]]
84        #     B = Union[int, Tuple[B, ...]]
85        # When checking if A <: B we push pair (A, B) onto 'assuming' stack, then when after few
86        # steps we come back to initial call is_subtype(A, B) and immediately return True.
87        with pop_on_exit(TypeState._assuming, left, right):
88            return _is_subtype(left, right,
89                               ignore_type_params=ignore_type_params,
90                               ignore_pos_arg_names=ignore_pos_arg_names,
91                               ignore_declared_variance=ignore_declared_variance,
92                               ignore_promotions=ignore_promotions)
93    return _is_subtype(left, right,
94                       ignore_type_params=ignore_type_params,
95                       ignore_pos_arg_names=ignore_pos_arg_names,
96                       ignore_declared_variance=ignore_declared_variance,
97                       ignore_promotions=ignore_promotions)
98
99
100def _is_subtype(left: Type, right: Type,
101                *,
102                ignore_type_params: bool = False,
103                ignore_pos_arg_names: bool = False,
104                ignore_declared_variance: bool = False,
105                ignore_promotions: bool = False) -> bool:
106    orig_right = right
107    orig_left = left
108    left = get_proper_type(left)
109    right = get_proper_type(right)
110
111    if (isinstance(right, AnyType) or isinstance(right, UnboundType)
112            or isinstance(right, ErasedType)):
113        return True
114    elif isinstance(right, UnionType) and not isinstance(left, UnionType):
115        # Normally, when 'left' is not itself a union, the only way
116        # 'left' can be a subtype of the union 'right' is if it is a
117        # subtype of one of the items making up the union.
118        is_subtype_of_item = any(is_subtype(orig_left, item,
119                                            ignore_type_params=ignore_type_params,
120                                            ignore_pos_arg_names=ignore_pos_arg_names,
121                                            ignore_declared_variance=ignore_declared_variance,
122                                            ignore_promotions=ignore_promotions)
123                                 for item in right.items)
124        # Recombine rhs literal types, to make an enum type a subtype
125        # of a union of all enum items as literal types. Only do it if
126        # the previous check didn't succeed, since recombining can be
127        # expensive.
128        if not is_subtype_of_item and isinstance(left, Instance) and left.type.is_enum:
129            right = UnionType(mypy.typeops.try_contracting_literals_in_union(right.items))
130            is_subtype_of_item = any(is_subtype(orig_left, item,
131                                                ignore_type_params=ignore_type_params,
132                                                ignore_pos_arg_names=ignore_pos_arg_names,
133                                                ignore_declared_variance=ignore_declared_variance,
134                                                ignore_promotions=ignore_promotions)
135                                     for item in right.items)
136        # However, if 'left' is a type variable T, T might also have
137        # an upper bound which is itself a union. This case will be
138        # handled below by the SubtypeVisitor. We have to check both
139        # possibilities, to handle both cases like T <: Union[T, U]
140        # and cases like T <: B where B is the upper bound of T and is
141        # a union. (See #2314.)
142        if not isinstance(left, TypeVarType):
143            return is_subtype_of_item
144        elif is_subtype_of_item:
145            return True
146        # otherwise, fall through
147    return left.accept(SubtypeVisitor(orig_right,
148                                      ignore_type_params=ignore_type_params,
149                                      ignore_pos_arg_names=ignore_pos_arg_names,
150                                      ignore_declared_variance=ignore_declared_variance,
151                                      ignore_promotions=ignore_promotions))
152
153
154def is_subtype_ignoring_tvars(left: Type, right: Type) -> bool:
155    return is_subtype(left, right, ignore_type_params=True)
156
157
158def is_equivalent(a: Type, b: Type,
159                  *,
160                  ignore_type_params: bool = False,
161                  ignore_pos_arg_names: bool = False
162                  ) -> bool:
163    return (
164        is_subtype(a, b, ignore_type_params=ignore_type_params,
165                   ignore_pos_arg_names=ignore_pos_arg_names)
166        and is_subtype(b, a, ignore_type_params=ignore_type_params,
167                       ignore_pos_arg_names=ignore_pos_arg_names))
168
169
170class SubtypeVisitor(TypeVisitor[bool]):
171
172    def __init__(self, right: Type,
173                 *,
174                 ignore_type_params: bool,
175                 ignore_pos_arg_names: bool = False,
176                 ignore_declared_variance: bool = False,
177                 ignore_promotions: bool = False) -> None:
178        self.right = get_proper_type(right)
179        self.orig_right = right
180        self.ignore_type_params = ignore_type_params
181        self.ignore_pos_arg_names = ignore_pos_arg_names
182        self.ignore_declared_variance = ignore_declared_variance
183        self.ignore_promotions = ignore_promotions
184        self.check_type_parameter = (ignore_type_parameter if ignore_type_params else
185                                     check_type_parameter)
186        self._subtype_kind = SubtypeVisitor.build_subtype_kind(
187            ignore_type_params=ignore_type_params,
188            ignore_pos_arg_names=ignore_pos_arg_names,
189            ignore_declared_variance=ignore_declared_variance,
190            ignore_promotions=ignore_promotions)
191
192    @staticmethod
193    def build_subtype_kind(*,
194                           ignore_type_params: bool = False,
195                           ignore_pos_arg_names: bool = False,
196                           ignore_declared_variance: bool = False,
197                           ignore_promotions: bool = False) -> SubtypeKind:
198        return (False,  # is proper subtype?
199                ignore_type_params,
200                ignore_pos_arg_names,
201                ignore_declared_variance,
202                ignore_promotions)
203
204    def _is_subtype(self, left: Type, right: Type) -> bool:
205        return is_subtype(left, right,
206                          ignore_type_params=self.ignore_type_params,
207                          ignore_pos_arg_names=self.ignore_pos_arg_names,
208                          ignore_declared_variance=self.ignore_declared_variance,
209                          ignore_promotions=self.ignore_promotions)
210
211    # visit_x(left) means: is left (which is an instance of X) a subtype of
212    # right?
213
214    def visit_unbound_type(self, left: UnboundType) -> bool:
215        return True
216
217    def visit_any(self, left: AnyType) -> bool:
218        return True
219
220    def visit_none_type(self, left: NoneType) -> bool:
221        if state.strict_optional:
222            if isinstance(self.right, NoneType) or is_named_instance(self.right,
223                                                                     'builtins.object'):
224                return True
225            if isinstance(self.right, Instance) and self.right.type.is_protocol:
226                members = self.right.type.protocol_members
227                # None is compatible with Hashable (and other similar protocols). This is
228                # slightly sloppy since we don't check the signature of "__hash__".
229                return not members or members == ["__hash__"]
230            return False
231        else:
232            return True
233
234    def visit_uninhabited_type(self, left: UninhabitedType) -> bool:
235        return True
236
237    def visit_erased_type(self, left: ErasedType) -> bool:
238        return True
239
240    def visit_deleted_type(self, left: DeletedType) -> bool:
241        return True
242
243    def visit_instance(self, left: Instance) -> bool:
244        if left.type.fallback_to_any:
245            if isinstance(self.right, NoneType):
246                # NOTE: `None` is a *non-subclassable* singleton, therefore no class
247                # can by a subtype of it, even with an `Any` fallback.
248                # This special case is needed to treat descriptors in classes with
249                # dynamic base classes correctly, see #5456.
250                return False
251            return True
252        right = self.right
253        if isinstance(right, TupleType) and mypy.typeops.tuple_fallback(right).type.is_enum:
254            return self._is_subtype(left, mypy.typeops.tuple_fallback(right))
255        if isinstance(right, Instance):
256            if TypeState.is_cached_subtype_check(self._subtype_kind, left, right):
257                return True
258            if not self.ignore_promotions:
259                for base in left.type.mro:
260                    if base._promote and self._is_subtype(base._promote, self.right):
261                        TypeState.record_subtype_cache_entry(self._subtype_kind, left, right)
262                        return True
263            rname = right.type.fullname
264            # Always try a nominal check if possible,
265            # there might be errors that a user wants to silence *once*.
266            if ((left.type.has_base(rname) or rname == 'builtins.object') and
267                    not self.ignore_declared_variance):
268                # Map left type to corresponding right instances.
269                t = map_instance_to_supertype(left, right.type)
270                nominal = all(self.check_type_parameter(lefta, righta, tvar.variance)
271                              for lefta, righta, tvar in
272                              zip(t.args, right.args, right.type.defn.type_vars))
273                if nominal:
274                    TypeState.record_subtype_cache_entry(self._subtype_kind, left, right)
275                return nominal
276            if right.type.is_protocol and is_protocol_implementation(left, right):
277                return True
278            return False
279        if isinstance(right, TypeType):
280            item = right.item
281            if isinstance(item, TupleType):
282                item = mypy.typeops.tuple_fallback(item)
283            if is_named_instance(left, 'builtins.type'):
284                return self._is_subtype(TypeType(AnyType(TypeOfAny.special_form)), right)
285            if left.type.is_metaclass():
286                if isinstance(item, AnyType):
287                    return True
288                if isinstance(item, Instance):
289                    return is_named_instance(item, 'builtins.object')
290        if isinstance(right, CallableType):
291            # Special case: Instance can be a subtype of Callable.
292            call = find_member('__call__', left, left, is_operator=True)
293            if call:
294                return self._is_subtype(call, right)
295            return False
296        else:
297            return False
298
299    def visit_type_var(self, left: TypeVarType) -> bool:
300        right = self.right
301        if isinstance(right, TypeVarType) and left.id == right.id:
302            return True
303        if left.values and self._is_subtype(
304                mypy.typeops.make_simplified_union(left.values), right):
305            return True
306        return self._is_subtype(left.upper_bound, self.right)
307
308    def visit_callable_type(self, left: CallableType) -> bool:
309        right = self.right
310        if isinstance(right, CallableType):
311            return is_callable_compatible(
312                left, right,
313                is_compat=self._is_subtype,
314                ignore_pos_arg_names=self.ignore_pos_arg_names)
315        elif isinstance(right, Overloaded):
316            return all(self._is_subtype(left, item) for item in right.items())
317        elif isinstance(right, Instance):
318            if right.type.is_protocol and right.type.protocol_members == ['__call__']:
319                # OK, a callable can implement a protocol with a single `__call__` member.
320                # TODO: we should probably explicitly exclude self-types in this case.
321                call = find_member('__call__', right, left, is_operator=True)
322                assert call is not None
323                if self._is_subtype(left, call):
324                    return True
325            return self._is_subtype(left.fallback, right)
326        elif isinstance(right, TypeType):
327            # This is unsound, we don't check the __init__ signature.
328            return left.is_type_obj() and self._is_subtype(left.ret_type, right.item)
329        else:
330            return False
331
332    def visit_tuple_type(self, left: TupleType) -> bool:
333        right = self.right
334        if isinstance(right, Instance):
335            if is_named_instance(right, 'typing.Sized'):
336                return True
337            elif (is_named_instance(right, 'builtins.tuple') or
338                  is_named_instance(right, 'typing.Iterable') or
339                  is_named_instance(right, 'typing.Container') or
340                  is_named_instance(right, 'typing.Sequence') or
341                  is_named_instance(right, 'typing.Reversible')):
342                if right.args:
343                    iter_type = right.args[0]
344                else:
345                    iter_type = AnyType(TypeOfAny.special_form)
346                return all(self._is_subtype(li, iter_type) for li in left.items)
347            elif self._is_subtype(mypy.typeops.tuple_fallback(left), right):
348                return True
349            return False
350        elif isinstance(right, TupleType):
351            if len(left.items) != len(right.items):
352                return False
353            for l, r in zip(left.items, right.items):
354                if not self._is_subtype(l, r):
355                    return False
356            rfallback = mypy.typeops.tuple_fallback(right)
357            if is_named_instance(rfallback, 'builtins.tuple'):
358                # No need to verify fallback. This is useful since the calculated fallback
359                # may be inconsistent due to how we calculate joins between unions vs.
360                # non-unions. For example, join(int, str) == object, whereas
361                # join(Union[int, C], Union[str, C]) == Union[int, str, C].
362                return True
363            lfallback = mypy.typeops.tuple_fallback(left)
364            if not self._is_subtype(lfallback, rfallback):
365                return False
366            return True
367        else:
368            return False
369
370    def visit_typeddict_type(self, left: TypedDictType) -> bool:
371        right = self.right
372        if isinstance(right, Instance):
373            return self._is_subtype(left.fallback, right)
374        elif isinstance(right, TypedDictType):
375            if not left.names_are_wider_than(right):
376                return False
377            for name, l, r in left.zip(right):
378                if not is_equivalent(l, r,
379                                     ignore_type_params=self.ignore_type_params):
380                    return False
381                # Non-required key is not compatible with a required key since
382                # indexing may fail unexpectedly if a required key is missing.
383                # Required key is not compatible with a non-required key since
384                # the prior doesn't support 'del' but the latter should support
385                # it.
386                #
387                # NOTE: 'del' support is currently not implemented (#3550). We
388                #       don't want to have to change subtyping after 'del' support
389                #       lands so here we are anticipating that change.
390                if (name in left.required_keys) != (name in right.required_keys):
391                    return False
392            # (NOTE: Fallbacks don't matter.)
393            return True
394        else:
395            return False
396
397    def visit_literal_type(self, left: LiteralType) -> bool:
398        if isinstance(self.right, LiteralType):
399            return left == self.right
400        else:
401            return self._is_subtype(left.fallback, self.right)
402
403    def visit_overloaded(self, left: Overloaded) -> bool:
404        right = self.right
405        if isinstance(right, Instance):
406            if right.type.is_protocol and right.type.protocol_members == ['__call__']:
407                # same as for CallableType
408                call = find_member('__call__', right, left, is_operator=True)
409                assert call is not None
410                if self._is_subtype(left, call):
411                    return True
412            return self._is_subtype(left.fallback, right)
413        elif isinstance(right, CallableType):
414            for item in left.items():
415                if self._is_subtype(item, right):
416                    return True
417            return False
418        elif isinstance(right, Overloaded):
419            if left == self.right:
420                # When it is the same overload, then the types are equal.
421                return True
422
423            # Ensure each overload in the right side (the supertype) is accounted for.
424            previous_match_left_index = -1
425            matched_overloads = set()
426            possible_invalid_overloads = set()
427
428            for right_index, right_item in enumerate(right.items()):
429                found_match = False
430
431                for left_index, left_item in enumerate(left.items()):
432                    subtype_match = self._is_subtype(left_item, right_item)
433
434                    # Order matters: we need to make sure that the index of
435                    # this item is at least the index of the previous one.
436                    if subtype_match and previous_match_left_index <= left_index:
437                        if not found_match:
438                            # Update the index of the previous match.
439                            previous_match_left_index = left_index
440                            found_match = True
441                            matched_overloads.add(left_item)
442                            possible_invalid_overloads.discard(left_item)
443                    else:
444                        # If this one overlaps with the supertype in any way, but it wasn't
445                        # an exact match, then it's a potential error.
446                        if (is_callable_compatible(left_item, right_item,
447                                    is_compat=self._is_subtype, ignore_return=True,
448                                    ignore_pos_arg_names=self.ignore_pos_arg_names) or
449                                is_callable_compatible(right_item, left_item,
450                                        is_compat=self._is_subtype, ignore_return=True,
451                                        ignore_pos_arg_names=self.ignore_pos_arg_names)):
452                            # If this is an overload that's already been matched, there's no
453                            # problem.
454                            if left_item not in matched_overloads:
455                                possible_invalid_overloads.add(left_item)
456
457                if not found_match:
458                    return False
459
460            if possible_invalid_overloads:
461                # There were potentially invalid overloads that were never matched to the
462                # supertype.
463                return False
464            return True
465        elif isinstance(right, UnboundType):
466            return True
467        elif isinstance(right, TypeType):
468            # All the items must have the same type object status, so
469            # it's sufficient to query only (any) one of them.
470            # This is unsound, we don't check all the __init__ signatures.
471            return left.is_type_obj() and self._is_subtype(left.items()[0], right)
472        else:
473            return False
474
475    def visit_union_type(self, left: UnionType) -> bool:
476        return all(self._is_subtype(item, self.orig_right) for item in left.items)
477
478    def visit_type_guard_type(self, left: TypeGuardType) -> bool:
479        raise RuntimeError("TypeGuard should not appear here")
480
481    def visit_partial_type(self, left: PartialType) -> bool:
482        # This is indeterminate as we don't really know the complete type yet.
483        raise RuntimeError
484
485    def visit_type_type(self, left: TypeType) -> bool:
486        right = self.right
487        if isinstance(right, TypeType):
488            return self._is_subtype(left.item, right.item)
489        if isinstance(right, CallableType):
490            # This is unsound, we don't check the __init__ signature.
491            return self._is_subtype(left.item, right.ret_type)
492        if isinstance(right, Instance):
493            if right.type.fullname in ['builtins.object', 'builtins.type']:
494                return True
495            item = left.item
496            if isinstance(item, TypeVarType):
497                item = get_proper_type(item.upper_bound)
498            if isinstance(item, Instance):
499                metaclass = item.type.metaclass_type
500                return metaclass is not None and self._is_subtype(metaclass, right)
501        return False
502
503    def visit_type_alias_type(self, left: TypeAliasType) -> bool:
504        assert False, "This should be never called, got {}".format(left)
505
506
507T = TypeVar('T', Instance, TypeAliasType)
508
509
510@contextmanager
511def pop_on_exit(stack: List[Tuple[T, T]],
512                left: T, right: T) -> Iterator[None]:
513    stack.append((left, right))
514    yield
515    stack.pop()
516
517
518def is_protocol_implementation(left: Instance, right: Instance,
519                               proper_subtype: bool = False) -> bool:
520    """Check whether 'left' implements the protocol 'right'.
521
522    If 'proper_subtype' is True, then check for a proper subtype.
523    Treat recursive protocols by using the 'assuming' structural subtype matrix
524    (in sparse representation, i.e. as a list of pairs (subtype, supertype)),
525    see also comment in nodes.TypeInfo. When we enter a check for classes
526    (A, P), defined as following::
527
528      class P(Protocol):
529          def f(self) -> P: ...
530      class A:
531          def f(self) -> A: ...
532
533    this results in A being a subtype of P without infinite recursion.
534    On every false result, we pop the assumption, thus avoiding an infinite recursion
535    as well.
536    """
537    assert right.type.is_protocol
538    # We need to record this check to generate protocol fine-grained dependencies.
539    TypeState.record_protocol_subtype_check(left.type, right.type)
540    # nominal subtyping currently ignores '__init__' and '__new__' signatures
541    members_not_to_check = {'__init__', '__new__'}
542    # Trivial check that circumvents the bug described in issue 9771:
543    if left.type.is_protocol:
544        members_right = set(right.type.protocol_members) - members_not_to_check
545        members_left = set(left.type.protocol_members) - members_not_to_check
546        if not members_right.issubset(members_left):
547            return False
548    assuming = right.type.assuming_proper if proper_subtype else right.type.assuming
549    for (l, r) in reversed(assuming):
550        if (mypy.sametypes.is_same_type(l, left)
551                and mypy.sametypes.is_same_type(r, right)):
552            return True
553    with pop_on_exit(assuming, left, right):
554        for member in right.type.protocol_members:
555            if member in members_not_to_check:
556                continue
557            ignore_names = member != '__call__'  # __call__ can be passed kwargs
558            # The third argument below indicates to what self type is bound.
559            # We always bind self to the subtype. (Similarly to nominal types).
560            supertype = get_proper_type(find_member(member, right, left))
561            assert supertype is not None
562            subtype = get_proper_type(find_member(member, left, left))
563            # Useful for debugging:
564            # print(member, 'of', left, 'has type', subtype)
565            # print(member, 'of', right, 'has type', supertype)
566            if not subtype:
567                return False
568            if isinstance(subtype, PartialType):
569                subtype = NoneType() if subtype.type is None else Instance(
570                    subtype.type, [AnyType(TypeOfAny.unannotated)] * len(subtype.type.type_vars)
571                )
572            if not proper_subtype:
573                # Nominal check currently ignores arg names
574                # NOTE: If we ever change this, be sure to also change the call to
575                # SubtypeVisitor.build_subtype_kind(...) down below.
576                is_compat = is_subtype(subtype, supertype, ignore_pos_arg_names=ignore_names)
577            else:
578                is_compat = is_proper_subtype(subtype, supertype)
579            if not is_compat:
580                return False
581            if isinstance(subtype, NoneType) and isinstance(supertype, CallableType):
582                # We want __hash__ = None idiom to work even without --strict-optional
583                return False
584            subflags = get_member_flags(member, left.type)
585            superflags = get_member_flags(member, right.type)
586            if IS_SETTABLE in superflags:
587                # Check opposite direction for settable attributes.
588                if not is_subtype(supertype, subtype):
589                    return False
590            if (IS_CLASSVAR in subflags) != (IS_CLASSVAR in superflags):
591                return False
592            if IS_SETTABLE in superflags and IS_SETTABLE not in subflags:
593                return False
594            # This rule is copied from nominal check in checker.py
595            if IS_CLASS_OR_STATIC in superflags and IS_CLASS_OR_STATIC not in subflags:
596                return False
597
598    if not proper_subtype:
599        # Nominal check currently ignores arg names, but __call__ is special for protocols
600        ignore_names = right.type.protocol_members != ['__call__']
601        subtype_kind = SubtypeVisitor.build_subtype_kind(ignore_pos_arg_names=ignore_names)
602    else:
603        subtype_kind = ProperSubtypeVisitor.build_subtype_kind()
604    TypeState.record_subtype_cache_entry(subtype_kind, left, right)
605    return True
606
607
608def find_member(name: str,
609                itype: Instance,
610                subtype: Type,
611                is_operator: bool = False) -> Optional[Type]:
612    """Find the type of member by 'name' in 'itype's TypeInfo.
613
614    Find the member type after applying type arguments from 'itype', and binding
615    'self' to 'subtype'. Return None if member was not found.
616    """
617    # TODO: this code shares some logic with checkmember.analyze_member_access,
618    # consider refactoring.
619    info = itype.type
620    method = info.get_method(name)
621    if method:
622        if method.is_property:
623            assert isinstance(method, OverloadedFuncDef)
624            dec = method.items[0]
625            assert isinstance(dec, Decorator)
626            return find_node_type(dec.var, itype, subtype)
627        return find_node_type(method, itype, subtype)
628    else:
629        # don't have such method, maybe variable or decorator?
630        node = info.get(name)
631        if not node:
632            v = None
633        else:
634            v = node.node
635        if isinstance(v, Decorator):
636            v = v.var
637        if isinstance(v, Var):
638            return find_node_type(v, itype, subtype)
639        if (not v and name not in ['__getattr__', '__setattr__', '__getattribute__'] and
640                not is_operator):
641            for method_name in ('__getattribute__', '__getattr__'):
642                # Normally, mypy assumes that instances that define __getattr__ have all
643                # attributes with the corresponding return type. If this will produce
644                # many false negatives, then this could be prohibited for
645                # structural subtyping.
646                method = info.get_method(method_name)
647                if method and method.info.fullname != 'builtins.object':
648                    getattr_type = get_proper_type(find_node_type(method, itype, subtype))
649                    if isinstance(getattr_type, CallableType):
650                        return getattr_type.ret_type
651        if itype.type.fallback_to_any:
652            return AnyType(TypeOfAny.special_form)
653    return None
654
655
656def get_member_flags(name: str, info: TypeInfo) -> Set[int]:
657    """Detect whether a member 'name' is settable, whether it is an
658    instance or class variable, and whether it is class or static method.
659
660    The flags are defined as following:
661    * IS_SETTABLE: whether this attribute can be set, not set for methods and
662      non-settable properties;
663    * IS_CLASSVAR: set if the variable is annotated as 'x: ClassVar[t]';
664    * IS_CLASS_OR_STATIC: set for methods decorated with @classmethod or
665      with @staticmethod.
666    """
667    method = info.get_method(name)
668    setattr_meth = info.get_method('__setattr__')
669    if method:
670        # this could be settable property
671        if method.is_property:
672            assert isinstance(method, OverloadedFuncDef)
673            dec = method.items[0]
674            assert isinstance(dec, Decorator)
675            if dec.var.is_settable_property or setattr_meth:
676                return {IS_SETTABLE}
677        return set()
678    node = info.get(name)
679    if not node:
680        if setattr_meth:
681            return {IS_SETTABLE}
682        return set()
683    v = node.node
684    if isinstance(v, Decorator):
685        if v.var.is_staticmethod or v.var.is_classmethod:
686            return {IS_CLASS_OR_STATIC}
687    # just a variable
688    if isinstance(v, Var) and not v.is_property:
689        flags = {IS_SETTABLE}
690        if v.is_classvar:
691            flags.add(IS_CLASSVAR)
692        return flags
693    return set()
694
695
696def find_node_type(node: Union[Var, FuncBase], itype: Instance, subtype: Type) -> Type:
697    """Find type of a variable or method 'node' (maybe also a decorated method).
698    Apply type arguments from 'itype', and bind 'self' to 'subtype'.
699    """
700    from mypy.typeops import bind_self
701
702    if isinstance(node, FuncBase):
703        typ = mypy.typeops.function_type(
704            node, fallback=Instance(itype.type.mro[-1], []))  # type: Optional[Type]
705    else:
706        typ = node.type
707    typ = get_proper_type(typ)
708    if typ is None:
709        return AnyType(TypeOfAny.from_error)
710    # We don't need to bind 'self' for static methods, since there is no 'self'.
711    if (isinstance(node, FuncBase)
712            or (isinstance(typ, FunctionLike)
713                and node.is_initialized_in_class
714                and not node.is_staticmethod)):
715        assert isinstance(typ, FunctionLike)
716        signature = bind_self(typ, subtype)
717        if node.is_property:
718            assert isinstance(signature, CallableType)
719            typ = signature.ret_type
720        else:
721            typ = signature
722    itype = map_instance_to_supertype(itype, node.info)
723    typ = expand_type_by_instance(typ, itype)
724    return typ
725
726
727def non_method_protocol_members(tp: TypeInfo) -> List[str]:
728    """Find all non-callable members of a protocol."""
729
730    assert tp.is_protocol
731    result = []  # type: List[str]
732    anytype = AnyType(TypeOfAny.special_form)
733    instance = Instance(tp, [anytype] * len(tp.defn.type_vars))
734
735    for member in tp.protocol_members:
736        typ = get_proper_type(find_member(member, instance, instance))
737        if not isinstance(typ, CallableType):
738            result.append(member)
739    return result
740
741
742def is_callable_compatible(left: CallableType, right: CallableType,
743                           *,
744                           is_compat: Callable[[Type, Type], bool],
745                           is_compat_return: Optional[Callable[[Type, Type], bool]] = None,
746                           ignore_return: bool = False,
747                           ignore_pos_arg_names: bool = False,
748                           check_args_covariantly: bool = False,
749                           allow_partial_overlap: bool = False) -> bool:
750    """Is the left compatible with the right, using the provided compatibility check?
751
752    is_compat:
753        The check we want to run against the parameters.
754
755    is_compat_return:
756        The check we want to run against the return type.
757        If None, use the 'is_compat' check.
758
759    check_args_covariantly:
760        If true, check if the left's args is compatible with the right's
761        instead of the other way around (contravariantly).
762
763        This function is mostly used to check if the left is a subtype of the right which
764        is why the default is to check the args contravariantly. However, it's occasionally
765        useful to check the args using some other check, so we leave the variance
766        configurable.
767
768        For example, when checking the validity of overloads, it's useful to see if
769        the first overload alternative has more precise arguments then the second.
770        We would want to check the arguments covariantly in that case.
771
772        Note! The following two function calls are NOT equivalent:
773
774            is_callable_compatible(f, g, is_compat=is_subtype, check_args_covariantly=False)
775            is_callable_compatible(g, f, is_compat=is_subtype, check_args_covariantly=True)
776
777        The two calls are similar in that they both check the function arguments in
778        the same direction: they both run `is_subtype(argument_from_g, argument_from_f)`.
779
780        However, the two calls differ in which direction they check things like
781        keyword arguments. For example, suppose f and g are defined like so:
782
783            def f(x: int, *y: int) -> int: ...
784            def g(x: int) -> int: ...
785
786        In this case, the first call will succeed and the second will fail: f is a
787        valid stand-in for g but not vice-versa.
788
789    allow_partial_overlap:
790        By default this function returns True if and only if *all* calls to left are
791        also calls to right (with respect to the provided 'is_compat' function).
792
793        If this parameter is set to 'True', we return True if *there exists at least one*
794        call to left that's also a call to right.
795
796        In other words, we perform an existential check instead of a universal one;
797        we require left to only overlap with right instead of being a subset.
798
799        For example, suppose we set 'is_compat' to some subtype check and compare following:
800
801            f(x: float, y: str = "...", *args: bool) -> str
802            g(*args: int) -> str
803
804        This function would normally return 'False': f is not a subtype of g.
805        However, we would return True if this parameter is set to 'True': the two
806        calls are compatible if the user runs "f_or_g(3)". In the context of that
807        specific call, the two functions effectively have signatures of:
808
809            f2(float) -> str
810            g2(int) -> str
811
812        Here, f2 is a valid subtype of g2 so we return True.
813
814        Specifically, if this parameter is set this function will:
815
816        -   Ignore optional arguments on either the left or right that have no
817            corresponding match.
818        -   No longer mandate optional arguments on either side are also optional
819            on the other.
820        -   No longer mandate that if right has a *arg or **kwarg that left must also
821            have the same.
822
823        Note: when this argument is set to True, this function becomes "symmetric" --
824        the following calls are equivalent:
825
826            is_callable_compatible(f, g,
827                                   is_compat=some_check,
828                                   check_args_covariantly=False,
829                                   allow_partial_overlap=True)
830            is_callable_compatible(g, f,
831                                   is_compat=some_check,
832                                   check_args_covariantly=True,
833                                   allow_partial_overlap=True)
834
835        If the 'some_check' function is also symmetric, the two calls would be equivalent
836        whether or not we check the args covariantly.
837    """
838    if is_compat_return is None:
839        is_compat_return = is_compat
840
841    # If either function is implicitly typed, ignore positional arg names too
842    if left.implicit or right.implicit:
843        ignore_pos_arg_names = True
844
845    # Non-type cannot be a subtype of type.
846    if right.is_type_obj() and not left.is_type_obj():
847        return False
848
849    # A callable L is a subtype of a generic callable R if L is a
850    # subtype of every type obtained from R by substituting types for
851    # the variables of R. We can check this by simply leaving the
852    # generic variables of R as type variables, effectively varying
853    # over all possible values.
854
855    # It's okay even if these variables share ids with generic
856    # type variables of L, because generating and solving
857    # constraints for the variables of L to make L a subtype of R
858    # (below) treats type variables on the two sides as independent.
859    if left.variables:
860        # Apply generic type variables away in left via type inference.
861        unified = unify_generic_callable(left, right, ignore_return=ignore_return)
862        if unified is None:
863            return False
864        else:
865            left = unified
866
867    # If we allow partial overlaps, we don't need to leave R generic:
868    # if we can find even just a single typevar assignment which
869    # would make these callables compatible, we should return True.
870
871    # So, we repeat the above checks in the opposite direction. This also
872    # lets us preserve the 'symmetry' property of allow_partial_overlap.
873    if allow_partial_overlap and right.variables:
874        unified = unify_generic_callable(right, left, ignore_return=ignore_return)
875        if unified is not None:
876            right = unified
877
878    # Check return types.
879    if not ignore_return and not is_compat_return(left.ret_type, right.ret_type):
880        return False
881
882    if check_args_covariantly:
883        is_compat = flip_compat_check(is_compat)
884
885    if right.is_ellipsis_args:
886        return True
887
888    left_star = left.var_arg()
889    left_star2 = left.kw_arg()
890    right_star = right.var_arg()
891    right_star2 = right.kw_arg()
892
893    # Match up corresponding arguments and check them for compatibility. In
894    # every pair (argL, argR) of corresponding arguments from L and R, argL must
895    # be "more general" than argR if L is to be a subtype of R.
896
897    # Arguments are corresponding if they either share a name, share a position,
898    # or both. If L's corresponding argument is ambiguous, L is not a subtype of R.
899
900    # If left has one corresponding argument by name and another by position,
901    # consider them to be one "merged" argument (and not ambiguous) if they're
902    # both optional, they're name-only and position-only respectively, and they
903    # have the same type.  This rule allows functions with (*args, **kwargs) to
904    # properly stand in for the full domain of formal arguments that they're
905    # used for in practice.
906
907    # Every argument in R must have a corresponding argument in L, and every
908    # required argument in L must have a corresponding argument in R.
909
910    # Phase 1: Confirm every argument in R has a corresponding argument in L.
911
912    # Phase 1a: If left and right can both accept an infinite number of args,
913    #           their types must be compatible.
914    #
915    #           Furthermore, if we're checking for compatibility in all cases,
916    #           we confirm that if R accepts an infinite number of arguments,
917    #           L must accept the same.
918    def _incompatible(left_arg: Optional[FormalArgument],
919                      right_arg: Optional[FormalArgument]) -> bool:
920        if right_arg is None:
921            return False
922        if left_arg is None:
923            return not allow_partial_overlap
924        return not is_compat(right_arg.typ, left_arg.typ)
925
926    if _incompatible(left_star, right_star) or _incompatible(left_star2, right_star2):
927        return False
928
929    # Phase 1b: Check non-star args: for every arg right can accept, left must
930    #           also accept. The only exception is if we are allowing partial
931    #           partial overlaps: in that case, we ignore optional args on the right.
932    for right_arg in right.formal_arguments():
933        left_arg = mypy.typeops.callable_corresponding_argument(left, right_arg)
934        if left_arg is None:
935            if allow_partial_overlap and not right_arg.required:
936                continue
937            return False
938        if not are_args_compatible(left_arg, right_arg, ignore_pos_arg_names,
939                                   allow_partial_overlap, is_compat):
940            return False
941
942    # Phase 1c: Check var args. Right has an infinite series of optional positional
943    #           arguments. Get all further positional args of left, and make sure
944    #           they're more general then the corresponding member in right.
945    if right_star is not None:
946        # Synthesize an anonymous formal argument for the right
947        right_by_position = right.try_synthesizing_arg_from_vararg(None)
948        assert right_by_position is not None
949
950        i = right_star.pos
951        assert i is not None
952        while i < len(left.arg_kinds) and left.arg_kinds[i] in (ARG_POS, ARG_OPT):
953            if allow_partial_overlap and left.arg_kinds[i] == ARG_OPT:
954                break
955
956            left_by_position = left.argument_by_position(i)
957            assert left_by_position is not None
958
959            if not are_args_compatible(left_by_position, right_by_position,
960                                       ignore_pos_arg_names, allow_partial_overlap,
961                                       is_compat):
962                return False
963            i += 1
964
965    # Phase 1d: Check kw args. Right has an infinite series of optional named
966    #           arguments. Get all further named args of left, and make sure
967    #           they're more general then the corresponding member in right.
968    if right_star2 is not None:
969        right_names = {name for name in right.arg_names if name is not None}
970        left_only_names = set()
971        for name, kind in zip(left.arg_names, left.arg_kinds):
972            if name is None or kind in (ARG_STAR, ARG_STAR2) or name in right_names:
973                continue
974            left_only_names.add(name)
975
976        # Synthesize an anonymous formal argument for the right
977        right_by_name = right.try_synthesizing_arg_from_kwarg(None)
978        assert right_by_name is not None
979
980        for name in left_only_names:
981            left_by_name = left.argument_by_name(name)
982            assert left_by_name is not None
983
984            if allow_partial_overlap and not left_by_name.required:
985                continue
986
987            if not are_args_compatible(left_by_name, right_by_name, ignore_pos_arg_names,
988                                       allow_partial_overlap, is_compat):
989                return False
990
991    # Phase 2: Left must not impose additional restrictions.
992    #          (Every required argument in L must have a corresponding argument in R)
993    #          Note: we already checked the *arg and **kwarg arguments in phase 1a.
994    for left_arg in left.formal_arguments():
995        right_by_name = (right.argument_by_name(left_arg.name)
996                         if left_arg.name is not None
997                         else None)
998
999        right_by_pos = (right.argument_by_position(left_arg.pos)
1000                        if left_arg.pos is not None
1001                        else None)
1002
1003        # If the left hand argument corresponds to two right-hand arguments,
1004        # neither of them can be required.
1005        if (right_by_name is not None
1006                and right_by_pos is not None
1007                and right_by_name != right_by_pos
1008                and (right_by_pos.required or right_by_name.required)):
1009            return False
1010
1011        # All *required* left-hand arguments must have a corresponding
1012        # right-hand argument.  Optional args do not matter.
1013        if left_arg.required and right_by_pos is None and right_by_name is None:
1014            return False
1015
1016    return True
1017
1018
1019def are_args_compatible(
1020        left: FormalArgument,
1021        right: FormalArgument,
1022        ignore_pos_arg_names: bool,
1023        allow_partial_overlap: bool,
1024        is_compat: Callable[[Type, Type], bool]) -> bool:
1025    def is_different(left_item: Optional[object], right_item: Optional[object]) -> bool:
1026        """Checks if the left and right items are different.
1027
1028        If the right item is unspecified (e.g. if the right callable doesn't care
1029        about what name or position its arg has), we default to returning False.
1030
1031        If we're allowing partial overlap, we also default to returning False
1032        if the left callable also doesn't care."""
1033        if right_item is None:
1034            return False
1035        if allow_partial_overlap and left_item is None:
1036            return False
1037        return left_item != right_item
1038
1039    # If right has a specific name it wants this argument to be, left must
1040    # have the same.
1041    if is_different(left.name, right.name):
1042        # But pay attention to whether we're ignoring positional arg names
1043        if not ignore_pos_arg_names or right.pos is None:
1044            return False
1045
1046    # If right is at a specific position, left must have the same:
1047    if is_different(left.pos, right.pos):
1048        return False
1049
1050    # If right's argument is optional, left's must also be
1051    # (unless we're relaxing the checks to allow potential
1052    # rather then definite compatibility).
1053    if not allow_partial_overlap and not right.required and left.required:
1054        return False
1055
1056    # If we're allowing partial overlaps and neither arg is required,
1057    # the types don't actually need to be the same
1058    if allow_partial_overlap and not left.required and not right.required:
1059        return True
1060
1061    # Left must have a more general type
1062    return is_compat(right.typ, left.typ)
1063
1064
1065def flip_compat_check(is_compat: Callable[[Type, Type], bool]) -> Callable[[Type, Type], bool]:
1066    def new_is_compat(left: Type, right: Type) -> bool:
1067        return is_compat(right, left)
1068    return new_is_compat
1069
1070
1071def unify_generic_callable(type: CallableType, target: CallableType,
1072                           ignore_return: bool,
1073                           return_constraint_direction: Optional[int] = None,
1074                           ) -> Optional[CallableType]:
1075    """Try to unify a generic callable type with another callable type.
1076
1077    Return unified CallableType if successful; otherwise, return None.
1078    """
1079    import mypy.solve
1080
1081    if return_constraint_direction is None:
1082        return_constraint_direction = mypy.constraints.SUBTYPE_OF
1083
1084    constraints = []  # type: List[mypy.constraints.Constraint]
1085    for arg_type, target_arg_type in zip(type.arg_types, target.arg_types):
1086        c = mypy.constraints.infer_constraints(
1087            arg_type, target_arg_type, mypy.constraints.SUPERTYPE_OF)
1088        constraints.extend(c)
1089    if not ignore_return:
1090        c = mypy.constraints.infer_constraints(
1091            type.ret_type, target.ret_type, return_constraint_direction)
1092        constraints.extend(c)
1093    type_var_ids = [tvar.id for tvar in type.variables]
1094    inferred_vars = mypy.solve.solve_constraints(type_var_ids, constraints)
1095    if None in inferred_vars:
1096        return None
1097    non_none_inferred_vars = cast(List[Type], inferred_vars)
1098    had_errors = False
1099
1100    def report(*args: Any) -> None:
1101        nonlocal had_errors
1102        had_errors = True
1103
1104    applied = mypy.applytype.apply_generic_arguments(type, non_none_inferred_vars, report,
1105                                                     context=target)
1106    if had_errors:
1107        return None
1108    return applied
1109
1110
1111def restrict_subtype_away(t: Type, s: Type, *, ignore_promotions: bool = False) -> Type:
1112    """Return t minus s for runtime type assertions.
1113
1114    If we can't determine a precise result, return a supertype of the
1115    ideal result (just t is a valid result).
1116
1117    This is used for type inference of runtime type checks such as
1118    isinstance(). Currently this just removes elements of a union type.
1119    """
1120    t = get_proper_type(t)
1121    s = get_proper_type(s)
1122
1123    if isinstance(t, UnionType):
1124        new_items = [restrict_subtype_away(item, s, ignore_promotions=ignore_promotions)
1125                     for item in t.relevant_items()
1126                     if (isinstance(get_proper_type(item), AnyType) or
1127                         not covers_at_runtime(item, s, ignore_promotions))]
1128        return UnionType.make_union(new_items)
1129    else:
1130        return t
1131
1132
1133def covers_at_runtime(item: Type, supertype: Type, ignore_promotions: bool) -> bool:
1134    """Will isinstance(item, supertype) always return True at runtime?"""
1135    item = get_proper_type(item)
1136
1137    # Since runtime type checks will ignore type arguments, erase the types.
1138    supertype = erase_type(supertype)
1139    if is_proper_subtype(erase_type(item), supertype, ignore_promotions=ignore_promotions,
1140                         erase_instances=True):
1141        return True
1142    if isinstance(supertype, Instance) and supertype.type.is_protocol:
1143        # TODO: Implement more robust support for runtime isinstance() checks, see issue #3827.
1144        if is_proper_subtype(item, supertype, ignore_promotions=ignore_promotions):
1145            return True
1146    if isinstance(item, TypedDictType) and isinstance(supertype, Instance):
1147        # Special case useful for selecting TypedDicts from unions using isinstance(x, dict).
1148        if supertype.type.fullname == 'builtins.dict':
1149            return True
1150    # TODO: Add more special cases.
1151    return False
1152
1153
1154def is_proper_subtype(left: Type, right: Type, *,
1155                      ignore_promotions: bool = False,
1156                      erase_instances: bool = False,
1157                      keep_erased_types: bool = False) -> bool:
1158    """Is left a proper subtype of right?
1159
1160    For proper subtypes, there's no need to rely on compatibility due to
1161    Any types. Every usable type is a proper subtype of itself.
1162
1163    If erase_instances is True, erase left instance *after* mapping it to supertype
1164    (this is useful for runtime isinstance() checks). If keep_erased_types is True,
1165    do not consider ErasedType a subtype of all types (used by type inference against unions).
1166    """
1167    if TypeState.is_assumed_proper_subtype(left, right):
1168        return True
1169    if (isinstance(left, TypeAliasType) and isinstance(right, TypeAliasType) and
1170            left.is_recursive and right.is_recursive):
1171        # This case requires special care because it may cause infinite recursion.
1172        # See is_subtype() for more info.
1173        with pop_on_exit(TypeState._assuming_proper, left, right):
1174            return _is_proper_subtype(left, right,
1175                                      ignore_promotions=ignore_promotions,
1176                                      erase_instances=erase_instances,
1177                                      keep_erased_types=keep_erased_types)
1178    return _is_proper_subtype(left, right,
1179                              ignore_promotions=ignore_promotions,
1180                              erase_instances=erase_instances,
1181                              keep_erased_types=keep_erased_types)
1182
1183
1184def _is_proper_subtype(left: Type, right: Type, *,
1185                       ignore_promotions: bool = False,
1186                       erase_instances: bool = False,
1187                       keep_erased_types: bool = False) -> bool:
1188    orig_left = left
1189    orig_right = right
1190    left = get_proper_type(left)
1191    right = get_proper_type(right)
1192
1193    if isinstance(right, UnionType) and not isinstance(left, UnionType):
1194        return any([is_proper_subtype(orig_left, item,
1195                                      ignore_promotions=ignore_promotions,
1196                                      erase_instances=erase_instances,
1197                                      keep_erased_types=keep_erased_types)
1198                    for item in right.items])
1199    return left.accept(ProperSubtypeVisitor(orig_right,
1200                                            ignore_promotions=ignore_promotions,
1201                                            erase_instances=erase_instances,
1202                                            keep_erased_types=keep_erased_types))
1203
1204
1205class ProperSubtypeVisitor(TypeVisitor[bool]):
1206    def __init__(self, right: Type, *,
1207                 ignore_promotions: bool = False,
1208                 erase_instances: bool = False,
1209                 keep_erased_types: bool = False) -> None:
1210        self.right = get_proper_type(right)
1211        self.orig_right = right
1212        self.ignore_promotions = ignore_promotions
1213        self.erase_instances = erase_instances
1214        self.keep_erased_types = keep_erased_types
1215        self._subtype_kind = ProperSubtypeVisitor.build_subtype_kind(
1216            ignore_promotions=ignore_promotions,
1217            erase_instances=erase_instances,
1218            keep_erased_types=keep_erased_types
1219        )
1220
1221    @staticmethod
1222    def build_subtype_kind(*,
1223                           ignore_promotions: bool = False,
1224                           erase_instances: bool = False,
1225                           keep_erased_types: bool = False) -> SubtypeKind:
1226        return True, ignore_promotions, erase_instances, keep_erased_types
1227
1228    def _is_proper_subtype(self, left: Type, right: Type) -> bool:
1229        return is_proper_subtype(left, right,
1230                                 ignore_promotions=self.ignore_promotions,
1231                                 erase_instances=self.erase_instances,
1232                                 keep_erased_types=self.keep_erased_types)
1233
1234    def visit_unbound_type(self, left: UnboundType) -> bool:
1235        # This can be called if there is a bad type annotation. The result probably
1236        # doesn't matter much but by returning True we simplify these bad types away
1237        # from unions, which could filter out some bogus messages.
1238        return True
1239
1240    def visit_any(self, left: AnyType) -> bool:
1241        return isinstance(self.right, AnyType)
1242
1243    def visit_none_type(self, left: NoneType) -> bool:
1244        if state.strict_optional:
1245            return (isinstance(self.right, NoneType) or
1246                    is_named_instance(self.right, 'builtins.object'))
1247        return True
1248
1249    def visit_uninhabited_type(self, left: UninhabitedType) -> bool:
1250        return True
1251
1252    def visit_erased_type(self, left: ErasedType) -> bool:
1253        # This may be encountered during type inference. The result probably doesn't
1254        # matter much.
1255        # TODO: it actually does matter, figure out more principled logic about this.
1256        if self.keep_erased_types:
1257            return False
1258        return True
1259
1260    def visit_deleted_type(self, left: DeletedType) -> bool:
1261        return True
1262
1263    def visit_instance(self, left: Instance) -> bool:
1264        right = self.right
1265        if isinstance(right, Instance):
1266            if TypeState.is_cached_subtype_check(self._subtype_kind, left, right):
1267                return True
1268            if not self.ignore_promotions:
1269                for base in left.type.mro:
1270                    if base._promote and self._is_proper_subtype(base._promote, right):
1271                        TypeState.record_subtype_cache_entry(self._subtype_kind, left, right)
1272                        return True
1273
1274            if left.type.has_base(right.type.fullname):
1275                def check_argument(leftarg: Type, rightarg: Type, variance: int) -> bool:
1276                    if variance == COVARIANT:
1277                        return self._is_proper_subtype(leftarg, rightarg)
1278                    elif variance == CONTRAVARIANT:
1279                        return self._is_proper_subtype(rightarg, leftarg)
1280                    else:
1281                        return mypy.sametypes.is_same_type(leftarg, rightarg)
1282                # Map left type to corresponding right instances.
1283                left = map_instance_to_supertype(left, right.type)
1284                if self.erase_instances:
1285                    erased = erase_type(left)
1286                    assert isinstance(erased, Instance)
1287                    left = erased
1288
1289                nominal = all(check_argument(ta, ra, tvar.variance) for ta, ra, tvar in
1290                              zip(left.args, right.args, right.type.defn.type_vars))
1291                if nominal:
1292                    TypeState.record_subtype_cache_entry(self._subtype_kind, left, right)
1293                return nominal
1294            if (right.type.is_protocol and
1295                    is_protocol_implementation(left, right, proper_subtype=True)):
1296                return True
1297            return False
1298        if isinstance(right, CallableType):
1299            call = find_member('__call__', left, left, is_operator=True)
1300            if call:
1301                return self._is_proper_subtype(call, right)
1302            return False
1303        return False
1304
1305    def visit_type_var(self, left: TypeVarType) -> bool:
1306        if isinstance(self.right, TypeVarType) and left.id == self.right.id:
1307            return True
1308        if left.values and self._is_proper_subtype(
1309                mypy.typeops.make_simplified_union(left.values), self.right):
1310            return True
1311        return self._is_proper_subtype(left.upper_bound, self.right)
1312
1313    def visit_callable_type(self, left: CallableType) -> bool:
1314        right = self.right
1315        if isinstance(right, CallableType):
1316            return is_callable_compatible(left, right, is_compat=self._is_proper_subtype)
1317        elif isinstance(right, Overloaded):
1318            return all(self._is_proper_subtype(left, item)
1319                       for item in right.items())
1320        elif isinstance(right, Instance):
1321            return self._is_proper_subtype(left.fallback, right)
1322        elif isinstance(right, TypeType):
1323            # This is unsound, we don't check the __init__ signature.
1324            return left.is_type_obj() and self._is_proper_subtype(left.ret_type, right.item)
1325        return False
1326
1327    def visit_tuple_type(self, left: TupleType) -> bool:
1328        right = self.right
1329        if isinstance(right, Instance):
1330            if (is_named_instance(right, 'builtins.tuple') or
1331                    is_named_instance(right, 'typing.Iterable') or
1332                    is_named_instance(right, 'typing.Container') or
1333                    is_named_instance(right, 'typing.Sequence') or
1334                    is_named_instance(right, 'typing.Reversible')):
1335                if not right.args:
1336                    return False
1337                iter_type = get_proper_type(right.args[0])
1338                if is_named_instance(right, 'builtins.tuple') and isinstance(iter_type, AnyType):
1339                    # TODO: We shouldn't need this special case. This is currently needed
1340                    #       for isinstance(x, tuple), though it's unclear why.
1341                    return True
1342                return all(self._is_proper_subtype(li, iter_type) for li in left.items)
1343            return self._is_proper_subtype(mypy.typeops.tuple_fallback(left), right)
1344        elif isinstance(right, TupleType):
1345            if len(left.items) != len(right.items):
1346                return False
1347            for l, r in zip(left.items, right.items):
1348                if not self._is_proper_subtype(l, r):
1349                    return False
1350            return self._is_proper_subtype(mypy.typeops.tuple_fallback(left),
1351                                           mypy.typeops.tuple_fallback(right))
1352        return False
1353
1354    def visit_typeddict_type(self, left: TypedDictType) -> bool:
1355        right = self.right
1356        if isinstance(right, TypedDictType):
1357            for name, typ in left.items.items():
1358                if (name in right.items
1359                        and not mypy.sametypes.is_same_type(typ, right.items[name])):
1360                    return False
1361            for name, typ in right.items.items():
1362                if name not in left.items:
1363                    return False
1364            return True
1365        return self._is_proper_subtype(left.fallback, right)
1366
1367    def visit_literal_type(self, left: LiteralType) -> bool:
1368        if isinstance(self.right, LiteralType):
1369            return left == self.right
1370        else:
1371            return self._is_proper_subtype(left.fallback, self.right)
1372
1373    def visit_overloaded(self, left: Overloaded) -> bool:
1374        # TODO: What's the right thing to do here?
1375        return False
1376
1377    def visit_union_type(self, left: UnionType) -> bool:
1378        return all([self._is_proper_subtype(item, self.orig_right) for item in left.items])
1379
1380    def visit_type_guard_type(self, left: TypeGuardType) -> bool:
1381        if isinstance(self.right, TypeGuardType):
1382            # TypeGuard[bool] is a subtype of TypeGuard[int]
1383            return self._is_proper_subtype(left.type_guard, self.right.type_guard)
1384        else:
1385            # TypeGuards aren't a subtype of anything else for now (but see #10489)
1386            return False
1387
1388    def visit_partial_type(self, left: PartialType) -> bool:
1389        # TODO: What's the right thing to do here?
1390        return False
1391
1392    def visit_type_type(self, left: TypeType) -> bool:
1393        right = self.right
1394        if isinstance(right, TypeType):
1395            # This is unsound, we don't check the __init__ signature.
1396            return self._is_proper_subtype(left.item, right.item)
1397        if isinstance(right, CallableType):
1398            # This is also unsound because of __init__.
1399            return right.is_type_obj() and self._is_proper_subtype(left.item, right.ret_type)
1400        if isinstance(right, Instance):
1401            if right.type.fullname == 'builtins.type':
1402                # TODO: Strictly speaking, the type builtins.type is considered equivalent to
1403                #       Type[Any]. However, this would break the is_proper_subtype check in
1404                #       conditional_type_map for cases like isinstance(x, type) when the type
1405                #       of x is Type[int]. It's unclear what's the right way to address this.
1406                return True
1407            if right.type.fullname == 'builtins.object':
1408                return True
1409            item = left.item
1410            if isinstance(item, TypeVarType):
1411                item = get_proper_type(item.upper_bound)
1412            if isinstance(item, Instance):
1413                metaclass = item.type.metaclass_type
1414                return metaclass is not None and self._is_proper_subtype(metaclass, right)
1415        return False
1416
1417    def visit_type_alias_type(self, left: TypeAliasType) -> bool:
1418        assert False, "This should be never called, got {}".format(left)
1419
1420
1421def is_more_precise(left: Type, right: Type, *, ignore_promotions: bool = False) -> bool:
1422    """Check if left is a more precise type than right.
1423
1424    A left is a proper subtype of right, left is also more precise than
1425    right. Also, if right is Any, left is more precise than right, for
1426    any left.
1427    """
1428    # TODO Should List[int] be more precise than List[Any]?
1429    right = get_proper_type(right)
1430    if isinstance(right, AnyType):
1431        return True
1432    return is_proper_subtype(left, right, ignore_promotions=ignore_promotions)
1433