Exemple #1
0
 def visit_func_def(self, o: FuncDef) -> None:
     self.scope.enter_function(o)
     target = self.scope.current_target()
     if o.type:
         if self.is_class and isinstance(o.type, FunctionLike):
             signature = bind_self(o.type)  # type: Type
         else:
             signature = o.type
         for trigger in self.get_type_triggers(signature):
             self.add_dependency(trigger)
             self.add_dependency(trigger, target=make_trigger(target))
     if o.info:
         for base in non_trivial_bases(o.info):
             # Base class __init__/__new__ doesn't generate a logical
             # dependency since the override can be incompatible.
             if not self.use_logical_deps() or o.name() not in ('__init__',
                                                                '__new__'):
                 self.add_dependency(
                     make_trigger(base.fullname() + '.' + o.name()))
     self.add_type_alias_deps(self.scope.current_target())
     super().visit_func_def(o)
     variants = set(o.expanded) - {o}
     for ex in variants:
         if isinstance(ex, FuncDef):
             super().visit_func_def(ex)
     self.scope.leave()
Exemple #2
0
def find_node_type(node: Union[Var, FuncBase], itype: Instance,
                   subtype: Type) -> Type:
    """Find type of a variable or method 'node' (maybe also a decorated method).
    Apply type arguments from 'itype', and bind 'self' to 'subtype'.
    """
    from mypy.checkmember import bind_self
    if isinstance(node, FuncBase):
        typ = function_type(node,
                            fallback=Instance(itype.type.mro[-1],
                                              []))  # type: Optional[Type]
    else:
        typ = node.type
    if typ is None:
        return AnyType(TypeOfAny.from_error)
    # We don't need to bind 'self' for static methods, since there is no 'self'.
    if isinstance(node, FuncBase) or isinstance(
            typ, FunctionLike) and not node.is_staticmethod:
        assert isinstance(typ, FunctionLike)
        signature = bind_self(typ, subtype)
        if node.is_property:
            assert isinstance(signature, CallableType)
            typ = signature.ret_type
        else:
            typ = signature
    itype = map_instance_to_supertype(itype, node.info)
    typ = expand_type_by_instance(typ, itype)
    return typ
Exemple #3
0
 def visit_func_def(self, o: FuncDef) -> None:
     target = self.push(o.name())
     if o.type:
         if self.is_class and isinstance(o.type, FunctionLike):
             signature = bind_self(o.type)  # type: Type
         else:
             signature = o.type
         for trigger in get_type_dependencies(signature):
             self.add_dependency(trigger)
             self.add_dependency(trigger, target=make_trigger(target))
     if o.info:
         for base in non_trivial_bases(o.info):
             self.add_dependency(make_trigger(base.fullname() + '.' + o.name()))
     super().visit_func_def(o)
     self.pop()
Exemple #4
0
 def visit_func_def(self, o: FuncDef) -> None:
     self.scope.enter_function(o)
     target = self.scope.current_target()
     if o.type:
         if self.is_class and isinstance(o.type, FunctionLike):
             signature = bind_self(o.type)  # type: Type
         else:
             signature = o.type
         for trigger in get_type_triggers(signature):
             self.add_dependency(trigger)
             self.add_dependency(trigger, target=make_trigger(target))
     if o.info:
         for base in non_trivial_bases(o.info):
             self.add_dependency(
                 make_trigger(base.fullname() + '.' + o.name()))
     if o in self.alias_deps:
         for alias in self.alias_deps[o]:
             self.add_dependency(make_trigger(alias))
     super().visit_func_def(o)
     self.scope.leave()
Exemple #5
0
 def visit_func_def(self, o: FuncDef) -> None:
     self.scope.enter_function(o)
     target = self.scope.current_target()
     if o.type:
         if self.is_class and isinstance(o.type, FunctionLike):
             signature = bind_self(o.type)  # type: Type
         else:
             signature = o.type
         for trigger in get_type_triggers(signature):
             self.add_dependency(trigger)
             self.add_dependency(trigger, target=make_trigger(target))
     if o.info:
         for base in non_trivial_bases(o.info):
             self.add_dependency(make_trigger(base.fullname() + '.' + o.name()))
     self.add_type_alias_deps(self.scope.current_target())
     super().visit_func_def(o)
     variants = set(o.expanded) - {o}
     for ex in variants:
         if isinstance(ex, FuncDef):
             super().visit_func_def(ex)
     self.scope.leave()
Exemple #6
0
 def visit_func_def(self, o: FuncDef) -> None:
     if not isinstance(self.current_scope(), FuncDef):
         # Not a nested function, so create a new target.
         new_scope = True
         target = self.enter_function_scope(o)
     else:
         # Treat nested functions as components of the parent function target.
         new_scope = False
         target = self.current_target()
     if o.type:
         if self.is_class and isinstance(o.type, FunctionLike):
             signature = bind_self(o.type)  # type: Type
         else:
             signature = o.type
         for trigger in get_type_triggers(signature):
             self.add_dependency(trigger)
             self.add_dependency(trigger, target=make_trigger(target))
     if o.info:
         for base in non_trivial_bases(o.info):
             self.add_dependency(make_trigger(base.fullname() + '.' + o.name()))
     super().visit_func_def(o)
     if new_scope:
         self.leave_scope()
Exemple #7
0
 def visit_func_def(self, o: FuncDef) -> None:
     if not isinstance(self.current_scope(), FuncDef):
         # Not a nested function, so create a new target.
         new_scope = True
         target = self.enter_function_scope(o)
     else:
         # Treat nested functions as components of the parent function target.
         new_scope = False
         target = self.current_target()
     if o.type:
         if self.is_class and isinstance(o.type, FunctionLike):
             signature = bind_self(o.type)  # type: Type
         else:
             signature = o.type
         for trigger in get_type_triggers(signature):
             self.add_dependency(trigger)
             self.add_dependency(trigger, target=make_trigger(target))
     if o.info:
         for base in non_trivial_bases(o.info):
             self.add_dependency(make_trigger(base.fullname() + '.' + o.name()))
     super().visit_func_def(o)
     if new_scope:
         self.leave_scope()
Exemple #8
0
 def visit_func_def(self, o: FuncDef) -> None:
     self.scope.enter_function(o)
     target = self.scope.current_target()
     if o.type:
         if self.is_class and isinstance(o.type, FunctionLike):
             signature = bind_self(o.type)  # type: Type
         else:
             signature = o.type
         for trigger in self.get_type_triggers(signature):
             self.add_dependency(trigger)
             self.add_dependency(trigger, target=make_trigger(target))
     if o.info:
         for base in non_trivial_bases(o.info):
             # Base class __init__/__new__ doesn't generate a logical
             # dependency since the override can be incompatible.
             if not self.use_logical_deps() or o.name() not in ('__init__', '__new__'):
                 self.add_dependency(make_trigger(base.fullname() + '.' + o.name()))
     self.add_type_alias_deps(self.scope.current_target())
     super().visit_func_def(o)
     variants = set(o.expanded) - {o}
     for ex in variants:
         if isinstance(ex, FuncDef):
             super().visit_func_def(ex)
     self.scope.leave()
Exemple #9
0
def find_node_type(node: Union[Var, FuncBase], itype: Instance, subtype: Type) -> Type:
    """Find type of a variable or method 'node' (maybe also a decorated method).
    Apply type arguments from 'itype', and bind 'self' to 'subtype'.
    """
    from mypy.checkmember import bind_self
    if isinstance(node, FuncBase):
        typ = function_type(node,
                            fallback=Instance(itype.type.mro[-1], []))  # type: Optional[Type]
    else:
        typ = node.type
    if typ is None:
        return AnyType(TypeOfAny.from_error)
    # We don't need to bind 'self' for static methods, since there is no 'self'.
    if isinstance(node, FuncBase) or isinstance(typ, FunctionLike) and not node.is_staticmethod:
        assert isinstance(typ, FunctionLike)
        signature = bind_self(typ, subtype)
        if node.is_property:
            assert isinstance(signature, CallableType)
            typ = signature.ret_type
        else:
            typ = signature
    itype = map_instance_to_supertype(itype, node.info)
    typ = expand_type_by_instance(typ, itype)
    return typ