本文整理汇总了Python中mypy.server.trigger.make_trigger函数的典型用法代码示例。如果您正苦于以下问题:Python make_trigger函数的具体用法?Python make_trigger怎么用?Python make_trigger使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了make_trigger函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: attribute_triggers
def attribute_triggers(self, typ: Type, name: str) -> List[str]:
"""Return all triggers associated with the attribute of a type."""
if isinstance(typ, TypeVarType):
typ = typ.upper_bound
if isinstance(typ, TupleType):
typ = typ.fallback
if isinstance(typ, Instance):
member = '%s.%s' % (typ.type.fullname(), name)
return [make_trigger(member)]
elif isinstance(typ, FunctionLike) and typ.is_type_obj():
member = '%s.%s' % (typ.type_object().fullname(), name)
triggers = [make_trigger(member)]
triggers.extend(self.attribute_triggers(typ.fallback, name))
return triggers
elif isinstance(typ, UnionType):
targets = []
for item in typ.items:
targets.extend(self.attribute_triggers(item, name))
return targets
elif isinstance(typ, TypeType):
triggers = self.attribute_triggers(typ.item, name)
if isinstance(typ.item, Instance) and typ.item.type.metaclass_type is not None:
triggers.append(make_trigger('%s.%s' %
(typ.item.type.metaclass_type.type.fullname(),
name)))
return triggers
else:
return []
示例2: visit_member_expr
def visit_member_expr(self, e: MemberExpr) -> None:
if isinstance(e.expr, RefExpr) and isinstance(e.expr.node, TypeInfo):
# Special case class attribute so that we don't depend on "__init__".
self.add_dependency(make_trigger(e.expr.node.fullname()))
else:
super().visit_member_expr(e)
if e.kind is not None:
# Reference to a module attribute
self.process_global_ref_expr(e)
else:
# Reference to a non-module (or missing) attribute
if e.expr not in self.type_map:
# No type available -- this happens for unreachable code. Since it's unreachable,
# it wasn't type checked and we don't need to generate dependencies.
return
if isinstance(e.expr, RefExpr) and isinstance(e.expr.node, MypyFile):
# Special case: reference to a missing module attribute.
self.add_dependency(make_trigger(e.expr.node.fullname() + '.' + e.name))
return
typ = self.type_map[e.expr]
self.add_attribute_dependency(typ, e.name)
if self.use_logical_deps() and isinstance(typ, AnyType):
name = self.get_unimported_fullname(e, typ)
if name is not None:
# Generate a logical dependency from an unimported
# definition (which comes from a missing module).
# Example:
# import missing # "missing" not in build
#
# def g() -> None:
# missing.f() # Generate dependency from "missing.f"
self.add_dependency(make_trigger(name))
示例3: _snapshot_protocol_deps
def _snapshot_protocol_deps() -> Dict[str, Set[str]]:
"""Collect protocol attribute dependencies found so far from registered subtype checks.
There are three kinds of protocol dependencies. For example, after a subtype check:
x: Proto = C()
the following dependencies will be generated:
1. ..., <SuperProto[wildcard]>, <Proto[wildcard]> -> <Proto>
2. ..., <B.attr>, <C.attr> -> <C> [for every attr in Proto members]
3. <C> -> Proto # this one to invalidate the subtype cache
The first kind is generated immediately per-module in deps.py (see also an example there
for motivation why it is needed). While two other kinds are generated here after all
modules are type checked and we have recorded all the subtype checks. To understand these
two kinds, consider a simple example:
class A:
def __iter__(self) -> Iterator[int]:
...
it: Iterable[int] = A()
We add <a.A.__iter__> -> <a.A> to invalidate the assignment (module target in this case),
whenever the signature of a.A.__iter__ changes. We also add <a.A> -> typing.Iterable,
to invalidate the subtype caches of the latter. (Note that the same logic applies to
proper subtype checks, and calculating meets and joins, if this involves calling
'subtypes.is_protocol_implementation').
"""
deps = {} # type: Dict[str, Set[str]]
for info in TypeState._rechecked_types:
for attr in TypeState._checked_against_members[info.fullname()]:
# The need for full MRO here is subtle, during an update, base classes of
# a concrete class may not be reprocessed, so not all <B.x> -> <C.x> deps
# are added.
for base_info in info.mro[:-1]:
trigger = make_trigger('%s.%s' % (base_info.fullname(), attr))
if 'typing' in trigger or 'builtins' in trigger:
# TODO: avoid everything from typeshed
continue
deps.setdefault(trigger, set()).add(make_trigger(info.fullname()))
for proto in TypeState._attempted_protocols[info.fullname()]:
trigger = make_trigger(info.fullname())
if 'typing' in trigger or 'builtins' in trigger:
continue
# If any class that was checked against a protocol changes,
# we need to reset the subtype cache for the protocol.
#
# Note: strictly speaking, the protocol doesn't need to be
# re-checked, we only need to reset the cache, and its uses
# elsewhere are still valid (unless invalidated by other deps).
deps.setdefault(trigger, set()).add(proto)
return deps
示例4: visit_class_def
def visit_class_def(self, o: ClassDef) -> None:
self.scope.enter_class(o.info)
target = self.scope.current_full_target()
self.add_dependency(make_trigger(target), target)
old_is_class = self.is_class
self.is_class = True
# Add dependencies to type variables of a generic class.
for tv in o.type_vars:
self.add_dependency(make_trigger(tv.fullname), target)
self.process_type_info(o.info)
super().visit_class_def(o)
self.is_class = old_is_class
self.scope.leave()
示例5: process_global_ref_expr
def process_global_ref_expr(self, o: RefExpr) -> None:
if o.fullname is not None:
self.add_dependency(make_trigger(o.fullname))
# If this is a reference to a type, generate a dependency to its
# constructor.
# TODO: avoid generating spurious dependencies for isinstancce checks,
# except statements, class attribute reference, etc, if perf problem.
typ = self.type_map.get(o)
if isinstance(typ, FunctionLike) and typ.is_type_obj():
class_name = typ.type_object().fullname()
self.add_dependency(make_trigger(class_name + '.__init__'))
self.add_dependency(make_trigger(class_name + '.__new__'))
示例6: visit_class_def
def visit_class_def(self, o: ClassDef) -> None:
target = self.enter_class_scope(o.info)
self.add_dependency(make_trigger(target), target)
old_is_class = self.is_class
self.is_class = True
# Add dependencies to type variables of a generic class.
for tv in o.type_vars:
self.add_dependency(make_trigger(tv.fullname), target)
# Add dependencies to base types.
for base in o.info.bases:
self.add_type_dependencies(base, target=target)
# TODO: Add dependencies based on remaining TypeInfo attributes.
super().visit_class_def(o)
self.is_class = old_is_class
info = o.info
for name, node in info.names.items():
if isinstance(node.node, Var):
for base_info in non_trivial_bases(info):
# If the type of an attribute changes in a base class, we make references
# to the attribute in the subclass stale.
self.add_dependency(make_trigger(base_info.fullname() + '.' + name),
target=make_trigger(info.fullname() + '.' + name))
for base_info in non_trivial_bases(info):
for name, node in base_info.names.items():
self.add_dependency(make_trigger(base_info.fullname() + '.' + name),
target=make_trigger(info.fullname() + '.' + name))
self.add_dependency(make_trigger(base_info.fullname() + '.__init__'),
target=make_trigger(info.fullname() + '.__init__'))
self.leave_scope()
示例7: visit_decorator
def visit_decorator(self, o: Decorator) -> None:
# We don't need to recheck outer scope for an overload, only overload itself.
# Also if any decorator is nested, it is not externally visible, so we don't need to
# generate dependency.
if not o.func.is_overload and self.scope.current_function_name() is None:
self.add_dependency(make_trigger(o.func.fullname()))
super().visit_decorator(o)
示例8: visit_import_from
def visit_import_from(self, o: ImportFrom) -> None:
module_id, _ = correct_relative_import(self.scope.current_module_id(),
o.relative,
o.id,
self.is_package_init_file)
for name, as_name in o.names:
self.add_dependency(make_trigger(module_id + '.' + name))
示例9: process_lvalue
def process_lvalue(self, lvalue: Expression) -> None:
"""Generate additional dependencies for an lvalue."""
if isinstance(lvalue, IndexExpr):
self.add_operator_method_dependency(lvalue.base, '__setitem__')
elif isinstance(lvalue, NameExpr):
if lvalue.kind in (MDEF, GDEF):
# Assignment to an attribute in the class body, or direct assignment to a
# global variable.
lvalue_type = self.get_non_partial_lvalue_type(lvalue)
type_triggers = get_type_triggers(lvalue_type)
attr_trigger = make_trigger('%s.%s' % (self.scope.current_full_target(),
lvalue.name))
for type_trigger in type_triggers:
self.add_dependency(type_trigger, attr_trigger)
elif isinstance(lvalue, MemberExpr):
if lvalue.kind is None:
# Reference to a non-module attribute
if lvalue.expr not in self.type_map:
# Unreachable assignment -> not checked so no dependencies to generate.
return
object_type = self.type_map[lvalue.expr]
lvalue_type = self.get_non_partial_lvalue_type(lvalue)
type_triggers = get_type_triggers(lvalue_type)
for attr_trigger in self.attribute_triggers(object_type, lvalue.name):
for type_trigger in type_triggers:
self.add_dependency(type_trigger, attr_trigger)
elif isinstance(lvalue, TupleExpr):
for item in lvalue.items:
self.process_lvalue(item)
示例10: visit_instance
def visit_instance(self, typ: Instance) -> List[str]:
trigger = make_trigger(typ.type.fullname())
triggers = [trigger]
for arg in typ.args:
triggers.extend(self.get_type_triggers(arg))
if typ.final_value:
triggers.extend(self.get_type_triggers(typ.final_value))
return triggers
示例11: visit_type_var
def visit_type_var(self, typ: TypeVarType) -> List[str]:
triggers = []
if typ.fullname:
triggers.append(make_trigger(typ.fullname))
if typ.upper_bound:
triggers.extend(get_type_triggers(typ.upper_bound))
for val in typ.values:
triggers.extend(get_type_triggers(val))
return triggers
示例12: visit_assignment_stmt
def visit_assignment_stmt(self, o: AssignmentStmt) -> None:
rvalue = o.rvalue
if isinstance(rvalue, CallExpr) and isinstance(rvalue.analyzed, TypeVarExpr):
analyzed = rvalue.analyzed
self.add_type_dependencies(analyzed.upper_bound,
target=make_trigger(analyzed.fullname()))
for val in analyzed.values:
self.add_type_dependencies(val, target=make_trigger(analyzed.fullname()))
# We need to re-analyze the definition if bound or value is deleted.
super().visit_call_expr(rvalue)
elif isinstance(rvalue, CallExpr) and isinstance(rvalue.analyzed, NamedTupleExpr):
# Depend on types of named tuple items.
info = rvalue.analyzed.info
prefix = '%s.%s' % (self.scope.current_full_target(), info.name())
for name, symnode in info.names.items():
if not name.startswith('_') and isinstance(symnode.node, Var):
typ = symnode.node.type
if typ:
self.add_type_dependencies(typ)
self.add_type_dependencies(typ, target=make_trigger(prefix))
attr_target = make_trigger('%s.%s' % (prefix, name))
self.add_type_dependencies(typ, target=attr_target)
elif isinstance(rvalue, CallExpr) and isinstance(rvalue.analyzed, TypedDictExpr):
# Depend on the underlying typeddict type
info = rvalue.analyzed.info
assert info.typeddict_type is not None
prefix = '%s.%s' % (self.scope.current_full_target(), info.name())
self.add_type_dependencies(info.typeddict_type, target=make_trigger(prefix))
elif isinstance(rvalue, CallExpr) and isinstance(rvalue.analyzed, EnumCallExpr):
# Enum values are currently not checked, but for future we add the deps on them
for name, symnode in rvalue.analyzed.info.names.items():
if isinstance(symnode.node, Var) and symnode.node.type:
self.add_type_dependencies(symnode.node.type)
elif o.is_alias_def:
assert len(o.lvalues) == 1
lvalue = o.lvalues[0]
assert isinstance(lvalue, NameExpr)
# TODO: get rid of this extra dependency from __init__ to alias definition scope
typ = self.type_map.get(lvalue)
if isinstance(typ, FunctionLike) and typ.is_type_obj():
class_name = typ.type_object().fullname()
self.add_dependency(make_trigger(class_name + '.__init__'))
self.add_dependency(make_trigger(class_name + '.__new__'))
if isinstance(rvalue, IndexExpr) and isinstance(rvalue.analyzed, TypeAliasExpr):
self.add_type_dependencies(rvalue.analyzed.type)
else:
# Normal assignment
super().visit_assignment_stmt(o)
for lvalue in o.lvalues:
self.process_lvalue(lvalue)
items = o.lvalues + [rvalue]
for i in range(len(items) - 1):
lvalue = items[i]
rvalue = items[i + 1]
if isinstance(lvalue, TupleExpr):
self.add_attribute_dependency_for_expr(rvalue, '__iter__')
if o.type:
for trigger in get_type_triggers(o.type):
self.add_dependency(trigger)
示例13: visit_import_from
def visit_import_from(self, o: ImportFrom) -> None:
if self.use_logical_deps():
# Just importing a name doesn't create a logical dependency.
return
module_id, _ = correct_relative_import(self.scope.current_module_id(),
o.relative,
o.id,
self.is_package_init_file)
for name, as_name in o.names:
self.add_dependency(make_trigger(module_id + '.' + name))
示例14: visit_func_def
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()
示例15: visit_name_expr
def visit_name_expr(self, o: NameExpr) -> None:
if o.kind == LDEF:
# We don't track depdendencies to local variables, since they
# aren't externally visible.
return
if o.kind == MDEF:
# Direct reference to member is only possible in the scope that
# defined the name, so no dependency is required.
return
if o.fullname is not None:
trigger = make_trigger(o.fullname)
self.add_dependency(trigger)