How to use the mypy.types.Instance function in mypy

To help you get started, we’ve selected a few mypy examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github python / mypy / mypy / semanal_pass3.py View on Github external
# The key idea is that when we recursively return to a type already traversed,
                # then we break the cycle and put AnyType as a leaf.
                return AnyType(TypeOfAny.from_error)
            return tp.copy_modified(fallback=Instance(info.replaced, [],
                                                      line=t.line)).accept(self)
        # Same as above but for TypedDicts.
        if info.replaced and info.replaced.typeddict_type:
            td = info.replaced.typeddict_type
            if self.check_recursion(td):
                # We also break the cycles for TypedDicts as explained above for NamedTuples.
                return AnyType(TypeOfAny.from_error)
            return td.copy_modified(fallback=Instance(info.replaced, [],
                                                      line=t.line)).accept(self)
        if self.check_recursion(t):
            # We also need to break a potential cycle with normal (non-synthetic) instance types.
            return Instance(t.type, [AnyType(TypeOfAny.from_error)] * len(t.type.defn.type_vars),
                            line=t.line)
        return super().visit_instance(t)
github feisuzhu / thbattle / src / mypy_plugins / batchlist.py View on Github external
assert False, 'WTF?!'

    typeinfo = instance.type

    expr = ctx.context
    assert isinstance(expr, MemberExpr), expr

    field = expr.name

    if field in typeinfo.names:
        t = typeinfo.names[field].type
        assert t
        return t

    typeparam = instance.args[0]
    if not isinstance(typeparam, Instance):
        ctx.api.fail('BatchList[{}] not supported by checker'.format(typeparam), expr)
        return Instance(typeinfo, [AnyType(TypeOfAny.from_error)])

    names = typeparam.type.names

    if field not in names:
        ctx.api.fail(
            'BatchList item {} has no attribute "{}"'.format(
                instance.args[0], field,
            ), expr
        )
        return Instance(typeinfo, [AnyType(TypeOfAny.from_error)])

    stnode = typeparam.type.get(field)
    assert stnode
    node = stnode.node
github python / mypy / mypy / join.py View on Github external
def join_instances(t: Instance, s: Instance) -> Type:
    """Calculate the join of two instance types.

    Return ErrorType if the result is ambiguous.
    """
    if t.type == s.type:
        # Simplest case: join two types with the same base type (but
        # potentially different arguments).
        if is_subtype(t, s) or is_subtype(s, t):
            # Compatible; combine type arguments.
            args = []  # type: List[Type]
            for i in range(len(t.args)):
                args.append(join_types(t.args[i], s.args[i]))
            return Instance(t.type, args)
        else:
            # Incompatible; return trivial result object.
            return object_from_instance(t)
    elif t.type.bases and is_subtype_ignoring_tvars(t, s):
        return join_instances_via_supertype(t, s)
    else:
        # Now t is not a subtype of s, and t != s. Now s could be a subtype
        # of t; alternatively, we need to find a common supertype. This works
        # in of the both cases.
        return join_instances_via_supertype(s, t)
github typeddjango / django-stubs / mypy_django_plugin / lib / helpers.py View on Github external
def reparametrize_instance(instance: Instance, new_args: List[MypyType]) -> Instance:
    return Instance(instance.type, args=new_args,
                    line=instance.line, column=instance.column)
github QQuick / Transcrypt / transcrypt / modules / org / transcrypt / type_check / mypy-lang-0.4.4_and_api / mypy / typefixture.py View on Github external
# Instance types
        self.std_tuple = Instance(self.std_tuplei, [])        # tuple
        self.type_type = Instance(self.type_typei, [])        # type
        self.function = Instance(self.functioni, [])  # function TODO
        self.a = Instance(self.ai, [])          # A
        self.b = Instance(self.bi, [])          # B
        self.c = Instance(self.ci, [])          # C
        self.d = Instance(self.di, [])          # D

        self.e = Instance(self.ei, [])          # E
        self.e2 = Instance(self.e2i, [])        # E2
        self.e3 = Instance(self.e3i, [])        # E3

        self.f = Instance(self.fi, [])          # F
        self.f2 = Instance(self.f2i, [])        # F2
        self.f3 = Instance(self.f3i, [])        # F3

        # Generic instance types
        self.ga = Instance(self.gi, [self.a])        # G[A]
        self.gb = Instance(self.gi, [self.b])        # G[B]
        self.gd = Instance(self.gi, [self.d])        # G[D]
        self.go = Instance(self.gi, [self.o])        # G[object]
        self.gt = Instance(self.gi, [self.t])        # G[T`1]
        self.gtf = Instance(self.gi, [self.tf])      # G[T`-1]
        self.gtf2 = Instance(self.gi, [self.tf2])    # G[T`-2]
        self.gs = Instance(self.gi, [self.s])        # G[S]
        self.gdyn = Instance(self.gi, [self.anyt])    # G[Any]

        self.g2a = Instance(self.g2i, [self.a])      # G2[A]

        self.gsaa = Instance(self.gsi, [self.a, self.a])  # GS[A, A]
github typeddjango / django-stubs / mypy_django_plugin / transformers / settings.py View on Github external
def get_user_model_hook(ctx: FunctionContext, django_context: DjangoContext) -> MypyType:
    auth_user_model = django_context.settings.AUTH_USER_MODEL
    model_cls = django_context.apps_registry.get_model(auth_user_model)
    model_cls_fullname = helpers.get_class_fullname(model_cls)

    model_info = helpers.lookup_fully_qualified_generic(model_cls_fullname,
                                                        helpers.get_typechecker_api(ctx).modules)
    assert isinstance(model_info, TypeInfo)

    return TypeType(Instance(model_info, []))
github python / mypy / mypy / typefixture.py View on Github external
self.c = Instance(self.ci, [])          # C
        self.d = Instance(self.di, [])          # D

        self.e = Instance(self.ei, [])          # E
        self.e2 = Instance(self.e2i, [])        # E2
        self.e3 = Instance(self.e3i, [])        # E3

        self.f = Instance(self.fi, [])          # F
        self.f2 = Instance(self.f2i, [])        # F2
        self.f3 = Instance(self.f3i, [])        # F3

        # Generic instance types
        self.ga = Instance(self.gi, [self.a])        # G[A]
        self.gb = Instance(self.gi, [self.b])        # G[B]
        self.gd = Instance(self.gi, [self.d])        # G[D]
        self.go = Instance(self.gi, [self.o])        # G[object]
        self.gt = Instance(self.gi, [self.t])        # G[T`1]
        self.gtf = Instance(self.gi, [self.tf])      # G[T`-1]
        self.gtf2 = Instance(self.gi, [self.tf2])    # G[T`-2]
        self.gs = Instance(self.gi, [self.s])        # G[S]
        self.gdyn = Instance(self.gi, [self.anyt])    # G[Any]

        self.g2a = Instance(self.g2i, [self.a])      # G2[A]

        self.gsaa = Instance(self.gsi, [self.a, self.a])  # GS[A, A]
        self.gsab = Instance(self.gsi, [self.a, self.b])  # GS[A, B]
        self.gsba = Instance(self.gsi, [self.b, self.a])  # GS[B, A]

        self.gs2a = Instance(self.gs2i, [self.a])    # GS2[A]
        self.gs2b = Instance(self.gs2i, [self.b])    # GS2[B]
        self.gs2d = Instance(self.gs2i, [self.d])    # GS2[D]
github python / mypy / mypy / join.py View on Github external
def is_better(t: Type, s: Type) -> bool:
    # Given two possible results from join_instances_via_supertype(),
    # indicate whether t is the better one.
    t = get_proper_type(t)
    s = get_proper_type(s)

    if isinstance(t, Instance):
        if not isinstance(s, Instance):
            return True
        # Use len(mro) as a proxy for the better choice.
        if len(t.type.mro) > len(s.type.mro):
            return True
    return False
github python / mypy / mypy / typefixture.py View on Github external
def __init__(self) -> None:
        super().__init__()
        # GF[T]
        self.gfi = self.make_type_info('GF', typevars=['T'], is_abstract=True)

        # M1 <: GF[A]
        self.m1i = self.make_type_info('M1',
                                       is_abstract=True,
                                       mro=[self.gfi, self.oi],
                                       bases=[Instance(self.gfi, [self.a])])

        self.gfa = Instance(self.gfi, [self.a])  # GF[A]
        self.gfb = Instance(self.gfi, [self.b])  # GF[B]

        self.m1 = Instance(self.m1i, [])  # M1