当前位置: 首页>>代码示例>>Python>>正文


Python TypeInfo.abstract_attributes方法代码示例

本文整理汇总了Python中mypy.nodes.TypeInfo.abstract_attributes方法的典型用法代码示例。如果您正苦于以下问题:Python TypeInfo.abstract_attributes方法的具体用法?Python TypeInfo.abstract_attributes怎么用?Python TypeInfo.abstract_attributes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在mypy.nodes.TypeInfo的用法示例。


在下文中一共展示了TypeInfo.abstract_attributes方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: calculate_class_abstract_status

# 需要导入模块: from mypy.nodes import TypeInfo [as 别名]
# 或者: from mypy.nodes.TypeInfo import abstract_attributes [as 别名]
def calculate_class_abstract_status(typ: TypeInfo, is_stub_file: bool, errors: Errors) -> None:
    """Calculate abstract status of a class.

    Set is_abstract of the type to True if the type has an unimplemented
    abstract attribute.  Also compute a list of abstract attributes.
    Report error is required ABCMeta metaclass is missing.
    """
    concrete = set()  # type: Set[str]
    abstract = []  # type: List[str]
    abstract_in_this_class = []  # type: List[str]
    for base in typ.mro:
        for name, symnode in base.names.items():
            node = symnode.node
            if isinstance(node, OverloadedFuncDef):
                # Unwrap an overloaded function definition. We can just
                # check arbitrarily the first overload item. If the
                # different items have a different abstract status, there
                # should be an error reported elsewhere.
                func = node.items[0]  # type: Optional[Node]
            else:
                func = node
            if isinstance(func, Decorator):
                fdef = func.func
                if fdef.is_abstract and name not in concrete:
                    typ.is_abstract = True
                    abstract.append(name)
                    if base is typ:
                        abstract_in_this_class.append(name)
            elif isinstance(node, Var):
                if node.is_abstract_var and name not in concrete:
                    typ.is_abstract = True
                    abstract.append(name)
                    if base is typ:
                        abstract_in_this_class.append(name)
            concrete.add(name)
    # In stubs, abstract classes need to be explicitly marked because it is too
    # easy to accidentally leave a concrete class abstract by forgetting to
    # implement some methods.
    typ.abstract_attributes = sorted(abstract)
    if is_stub_file:
        if typ.declared_metaclass and typ.declared_metaclass.type.fullname() == 'abc.ABCMeta':
            return
        if typ.is_protocol:
            return
        if abstract and not abstract_in_this_class:
            def report(message: str, severity: str) -> None:
                errors.report(typ.line, typ.column, message, severity=severity)

            attrs = ", ".join('"{}"'.format(attr) for attr in sorted(abstract))
            report("Class {} has abstract attributes {}".format(typ.fullname(), attrs), 'error')
            report("If it is meant to be abstract, add 'abc.ABCMeta' as an explicit metaclass",
                   'note')
开发者ID:mananpal1997,项目名称:mypy,代码行数:54,代码来源:semanal_classprop.py

示例2: strip_type_info

# 需要导入模块: from mypy.nodes import TypeInfo [as 别名]
# 或者: from mypy.nodes.TypeInfo import abstract_attributes [as 别名]
    def strip_type_info(self, info: TypeInfo) -> List[SymbolNode]:
        info.type_vars = []
        info.bases = []
        info.is_abstract = False
        info.abstract_attributes = []
        info.mro = []
        info.add_type_vars()
        info.tuple_type = None
        info.typeddict_type = None
        info.tuple_type = None
        TypeState.reset_subtype_caches_for(info)
        info.declared_metaclass = None
        info.metaclass_type = None

        # We need to delete any entries that were generated by plugins,
        # since they will get regenerated.
        to_delete = [(k, v) for k, v in info.names.items() if v.plugin_generated]
        for k, _ in to_delete:
            del info.names[k]
        return [v.node for k, v in to_delete if v.node]
开发者ID:Michael0x2a,项目名称:mypy,代码行数:22,代码来源:aststrip.py


注:本文中的mypy.nodes.TypeInfo.abstract_attributes方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。