本文整理汇总了Python中mypy.nodes.ClassDef类的典型用法代码示例。如果您正苦于以下问题:Python ClassDef类的具体用法?Python ClassDef怎么用?Python ClassDef使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ClassDef类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: analyze_namedtuple_classdef
def analyze_namedtuple_classdef(self, defn: ClassDef) -> Tuple[bool, Optional[TypeInfo]]:
"""Analyze if given class definition can be a named tuple definition.
Return a tuple where first item indicates whether this can possibly be a named tuple,
and the second item is the corresponding TypeInfo (may be None if not ready and should be
deferred).
"""
for base_expr in defn.base_type_exprs:
if isinstance(base_expr, RefExpr):
self.api.accept(base_expr)
if base_expr.fullname == 'typing.NamedTuple':
result = self.check_namedtuple_classdef(defn)
if result is None:
# This is a valid named tuple, but some types are incomplete.
return True, None
items, types, default_items = result
info = self.build_namedtuple_typeinfo(
defn.name, items, types, default_items, defn.line)
defn.info = info
defn.analyzed = NamedTupleExpr(info, is_typed=True)
defn.analyzed.line = defn.line
defn.analyzed.column = defn.column
# All done: this is a valid named tuple with all types known.
return True, info
# This can't be a valid named tuple.
return False, None
开发者ID:Michael0x2a,项目名称:mypy,代码行数:26,代码来源:semanal_namedtuple.py
示例2: make_type_info
def make_type_info(name: str,
is_abstract: bool = False,
mro: List[TypeInfo] = None,
bases: List[Instance] = None,
typevars: List[str] = None) -> TypeInfo:
"""Make a TypeInfo suitable for use in unit tests."""
class_def = ClassDef(name, Block([]), None, [])
class_def.fullname = name
if typevars:
v = [] # type: List[TypeVarDef]
id = 1
for n in typevars:
v.append(TypeVarDef(n, id, None))
id += 1
class_def.type_vars = v
info = TypeInfo(SymbolTable(), class_def)
if mro is None:
mro = []
info.mro = [info] + mro
if bases is None:
if mro:
# By default, assume that there is a single non-generic base.
bases = [Instance(mro[0], [])]
else:
bases = []
info.bases = bases
return info
开发者ID:akaihola,项目名称:mypy,代码行数:31,代码来源:typefixture.py
示例3: transform_class_def
def transform_class_def(self, tdef: ClassDef) -> List[Node]:
"""Transform a type definition.
The result may be one or two definitions. The first is the
transformation of the original ClassDef. The second is a
wrapper type, which is generated for generic types only.
"""
defs = [] # type: List[Node]
if tdef.info.type_vars:
# This is a generic type. Insert type variable slots in
# the class definition for new type variables, i.e. type
# variables not mapped to superclass type variables.
defs.extend(self.make_tvar_representation(tdef.info))
# Iterate over definitions and transform each of them.
vars = set() # type: Set[Var]
for d in tdef.defs.body:
if isinstance(d, FuncDef):
# Implicit cast from FuncDef[] to Node[] is safe below.
defs.extend(Any(self.func_tf.transform_method(d)))
elif isinstance(d, VarDef):
defs.extend(self.transform_var_def(d))
for n in d.items:
vars.add(n)
elif isinstance(d, AssignmentStmt):
self.transform_assignment(d)
defs.append(d)
# Add accessors for implicitly defined attributes.
for node in tdef.info.names.values():
if isinstance(node.node, Var):
v = cast(Var, node.node)
if v.info == tdef.info and v not in vars:
defs.extend(self.make_accessors(v))
# For generic classes, add an implicit __init__ wrapper.
defs.extend(self.make_init_wrapper(tdef))
if tdef.is_generic() or (tdef.info.bases and
tdef.info.mro[1].is_generic()):
self.make_instance_tvar_initializer(
cast(FuncDef, tdef.info.get_method('__init__')))
if not defs:
defs.append(PassStmt())
if tdef.is_generic():
gen_wrapper = self.generic_class_wrapper(tdef)
tdef.defs = Block(defs)
dyn_wrapper = self.make_type_object_wrapper(tdef)
if not tdef.is_generic():
return [tdef, dyn_wrapper]
else:
return [tdef, dyn_wrapper, gen_wrapper]
开发者ID:FlorianLudwig,项目名称:mypy,代码行数:58,代码来源:transformtype.py
示例4: stale_info
def stale_info() -> TypeInfo:
suggestion = "<stale cache: consider running mypy without --quick>"
dummy_def = ClassDef(suggestion, Block([]))
dummy_def.fullname = suggestion
info = TypeInfo(SymbolTable(), dummy_def, "<stale>")
info.mro = [info]
info.bases = []
return info
开发者ID:greatmazinger,项目名称:mypy,代码行数:9,代码来源:fixup.py
示例5: visit_class_def
def visit_class_def(self, node: ClassDef) -> Node:
new = ClassDef(node.name, self.block(node.defs), node.type_vars,
self.nodes(node.base_type_exprs), node.metaclass)
new.fullname = node.fullname
new.info = node.info
new.decorators = [
decorator.accept(self) for decorator in node.decorators
]
new.is_builtinclass = node.is_builtinclass
return new
开发者ID:matthiaskramm,项目名称:mypy,代码行数:10,代码来源:treetransform.py
示例6: stale_info
def stale_info(modules: Dict[str, MypyFile]) -> TypeInfo:
suggestion = "<stale cache: consider running mypy without --quick>"
dummy_def = ClassDef(suggestion, Block([]))
dummy_def.fullname = suggestion
info = TypeInfo(SymbolTable(), dummy_def, "<stale>")
obj_type = lookup_qualified(modules, 'builtins.object', False)
assert isinstance(obj_type, TypeInfo)
info.bases = [Instance(obj_type, [])]
info.mro = [info, obj_type]
return info
开发者ID:sixolet,项目名称:mypy,代码行数:11,代码来源:fixup.py
示例7: visit_ClassDef
def visit_ClassDef(self, n: ast27.ClassDef) -> ClassDef:
self.class_nesting += 1
cdef = ClassDef(n.name,
self.as_block(n.body, n.lineno),
None,
self.translate_expr_list(n.bases),
metaclass=None)
cdef.decorators = self.translate_expr_list(n.decorator_list)
self.class_nesting -= 1
return cdef
开发者ID:cocoatomo,项目名称:mypy,代码行数:11,代码来源:fastparse2.py
示例8: visit_class_def
def visit_class_def(self, node: ClassDef) -> None:
"""Strip class body and type info, but don't strip methods."""
to_delete = set(self.strip_type_info(node.info))
node.type_vars = []
node.base_type_exprs.extend(node.removed_base_type_exprs)
node.removed_base_type_exprs = []
node.defs.body = [s for s in node.defs.body
if s not in to_delete]
with self.enter_class(node.info):
super().visit_class_def(node)
开发者ID:Michael0x2a,项目名称:mypy,代码行数:11,代码来源:aststrip.py
示例9: add_info_hook
def add_info_hook(ctx) -> None:
class_def = ClassDef(ctx.name, Block([]))
class_def.fullname = ctx.api.qualified_name(ctx.name)
info = TypeInfo(SymbolTable(), class_def, ctx.api.cur_mod_id)
class_def.info = info
obj = ctx.api.builtin_type('builtins.object')
info.mro = [info, obj.type]
info.bases = [obj]
ctx.api.add_symbol_table_node(ctx.name, SymbolTableNode(GDEF, info))
info.metadata['magic'] = True
开发者ID:Michael0x2a,项目名称:mypy,代码行数:11,代码来源:common_api_incremental.py
示例10: missing_info
def missing_info(modules: Dict[str, MypyFile]) -> TypeInfo:
suggestion = "<missing info: *should* have gone away during fine-grained update>"
dummy_def = ClassDef(suggestion, Block([]))
dummy_def.fullname = suggestion
info = TypeInfo(SymbolTable(), dummy_def, "<missing>")
obj_type = lookup_qualified(modules, 'builtins.object', False)
assert isinstance(obj_type, TypeInfo)
info.bases = [Instance(obj_type, [])]
info.mro = [info, obj_type]
return info
开发者ID:python,项目名称:mypy,代码行数:11,代码来源:fixup.py
示例11: visit_class_def
def visit_class_def(self, node: ClassDef) -> ClassDef:
new = ClassDef(node.name,
self.block(node.defs),
node.type_vars,
self.expressions(node.base_type_exprs),
node.metaclass)
new.fullname = node.fullname
new.info = node.info
new.decorators = [self.expr(decorator)
for decorator in node.decorators]
return new
开发者ID:alexandrul,项目名称:mypy,代码行数:11,代码来源:treetransform.py
示例12: visit_class_def
def visit_class_def(self, cdef: ClassDef) -> None:
kind = self.kind_by_scope()
if kind == LDEF:
return
elif kind == GDEF:
self.sem.check_no_global(cdef.name, cdef)
cdef.fullname = self.sem.qualified_name(cdef.name)
info = TypeInfo(SymbolTable(), cdef, self.sem.cur_mod_id)
info.set_line(cdef.line, cdef.column)
cdef.info = info
if kind == GDEF:
self.sem.globals[cdef.name] = SymbolTableNode(kind, info)
self.process_nested_classes(cdef)
开发者ID:sixolet,项目名称:mypy,代码行数:13,代码来源:semanal_pass1.py
示例13: visit_ClassDef
def visit_ClassDef(self, n: ast3.ClassDef) -> ClassDef:
self.class_nesting += 1
keywords = [(kw.arg, self.visit(kw.value))
for kw in n.keywords if kw.arg]
cdef = ClassDef(n.name,
self.as_required_block(n.body, n.lineno),
None,
self.translate_expr_list(n.bases),
metaclass=dict(keywords).get('metaclass'),
keywords=keywords)
cdef.decorators = self.translate_expr_list(n.decorator_list)
self.class_nesting -= 1
return cdef
开发者ID:greatmazinger,项目名称:mypy,代码行数:14,代码来源:fastparse.py
示例14: visit_ClassDef
def visit_ClassDef(self, n: ast27.ClassDef) -> ClassDef:
self.class_and_function_stack.append('C')
cdef = ClassDef(n.name,
self.as_required_block(n.body, n.lineno),
None,
self.translate_expr_list(n.bases),
metaclass=None)
cdef.decorators = self.translate_expr_list(n.decorator_list)
cdef.line = n.lineno + len(n.decorator_list)
cdef.column = n.col_offset
cdef.end_line = n.lineno
self.class_and_function_stack.pop()
return cdef
开发者ID:python,项目名称:mypy,代码行数:14,代码来源:fastparse2.py
示例15: visit_ClassDef
def visit_ClassDef(self, n: ast35.ClassDef) -> Node:
self.class_nesting += 1
metaclass_arg = find(lambda x: x.arg == 'metaclass', n.keywords)
metaclass = None
if metaclass_arg:
metaclass = self.stringify_name(metaclass_arg.value)
cdef = ClassDef(n.name,
Block(self.fix_function_overloads(self.visit_list(n.body))),
None,
self.visit_list(n.bases),
metaclass=metaclass)
cdef.decorators = self.visit_list(n.decorator_list)
self.class_nesting -= 1
return cdef
开发者ID:edreamleo,项目名称:mypy,代码行数:15,代码来源:fastparse.py
示例16: visit_ClassDef
def visit_ClassDef(self, n):
self.in_class = True
metaclass_arg = find(lambda x: x.arg == 'metaclass', n.keywords)
metaclass = None
if metaclass_arg:
metaclass = self.stringify_name(metaclass_arg.value)
cdef = ClassDef(n.name,
Block(self.fix_function_overloads(self.visit(n.body))),
None,
self.visit(n.bases),
metaclass=metaclass)
cdef.decorators = self.visit(n.decorator_list)
self.in_class = False
return cdef
开发者ID:allthedata,项目名称:mypy,代码行数:15,代码来源:fastparse.py
示例17: visit_ClassDef
def visit_ClassDef(self, n: ast35.ClassDef) -> ClassDef:
self.class_nesting += 1
metaclass_arg = find(lambda x: x.arg == 'metaclass', n.keywords)
metaclass = None
if metaclass_arg:
metaclass = self.stringify_name(metaclass_arg.value)
if metaclass is None:
metaclass = '<error>' # To be reported later
cdef = ClassDef(n.name,
self.as_block(n.body, n.lineno),
None,
self.translate_expr_list(n.bases),
metaclass=metaclass)
cdef.decorators = self.translate_expr_list(n.decorator_list)
self.class_nesting -= 1
return cdef
开发者ID:rra,项目名称:mypy,代码行数:17,代码来源:fastparse.py
示例18: make_type_info
def make_type_info(self, name: str,
module_name: Optional[str] = None,
is_abstract: bool = False,
mro: Optional[List[TypeInfo]] = None,
bases: Optional[List[Instance]] = None,
typevars: Optional[List[str]] = None,
variances: Optional[List[int]] = None) -> TypeInfo:
"""Make a TypeInfo suitable for use in unit tests."""
class_def = ClassDef(name, Block([]), None, [])
class_def.fullname = name
if module_name is None:
if '.' in name:
module_name = name.rsplit('.', 1)[0]
else:
module_name = '__main__'
if typevars:
v = [] # type: List[TypeVarDef]
for id, n in enumerate(typevars, 1):
if variances:
variance = variances[id - 1]
else:
variance = COVARIANT
v.append(TypeVarDef(n, n, id, [], self.o, variance=variance))
class_def.type_vars = v
info = TypeInfo(SymbolTable(), class_def, module_name)
if mro is None:
mro = []
if name != 'builtins.object':
mro.append(self.oi)
info.mro = [info] + mro
if bases is None:
if mro:
# By default, assume that there is a single non-generic base.
bases = [Instance(mro[0], [])]
else:
bases = []
info.bases = bases
return info
开发者ID:python,项目名称:mypy,代码行数:43,代码来源:typefixture.py
示例19: visit_class_def
def visit_class_def(self, node: ClassDef) -> None:
"""Strip class body and type info, but don't strip methods."""
node.info.type_vars = []
node.info.bases = []
node.info.abstract_attributes = []
node.info.mro = []
node.info.add_type_vars()
node.base_type_exprs.extend(node.removed_base_type_exprs)
node.removed_base_type_exprs = []
with self.enter_class(node.info):
super().visit_class_def(node)
开发者ID:greatmazinger,项目名称:mypy,代码行数:11,代码来源:aststrip.py
示例20: analyze_namedtuple_classdef
def analyze_namedtuple_classdef(self, defn: ClassDef) -> Optional[TypeInfo]:
# special case for NamedTuple
for base_expr in defn.base_type_exprs:
if isinstance(base_expr, RefExpr):
self.api.accept(base_expr)
if base_expr.fullname == 'typing.NamedTuple':
node = self.api.lookup(defn.name, defn)
if node is not None:
node.kind = GDEF # TODO in process_namedtuple_definition also applies here
items, types, default_items = self.check_namedtuple_classdef(defn)
info = self.build_namedtuple_typeinfo(
defn.name, items, types, default_items)
node.node = info
defn.info.replaced = info
defn.info = info
defn.analyzed = NamedTupleExpr(info, is_typed=True)
defn.analyzed.line = defn.line
defn.analyzed.column = defn.column
return info
return None
开发者ID:python,项目名称:mypy,代码行数:20,代码来源:semanal_namedtuple.py
注:本文中的mypy.nodes.ClassDef类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论