本文整理汇总了Python中mypy.nodes.FuncDef.line方法的典型用法代码示例。如果您正苦于以下问题:Python FuncDef.line方法的具体用法?Python FuncDef.line怎么用?Python FuncDef.line使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mypy.nodes.FuncDef
的用法示例。
在下文中一共展示了FuncDef.line方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: add_method
# 需要导入模块: from mypy.nodes import FuncDef [as 别名]
# 或者: from mypy.nodes.FuncDef import line [as 别名]
def add_method(self,
method_name: str, args: List[Argument], ret_type: Type,
self_type: Optional[Type] = None,
tvd: Optional[TypeVarDef] = None) -> None:
"""Add a method: def <method_name>(self, <args>) -> <ret_type>): ... to info.
self_type: The type to use for the self argument or None to use the inferred self type.
tvd: If the method is generic these should be the type variables.
"""
from mypy.semanal import set_callable_name
self_type = self_type if self_type is not None else self.self_type
args = [Argument(Var('self'), self_type, None, ARG_POS)] + args
arg_types = [arg.type_annotation for arg in args]
arg_names = [arg.variable.name() for arg in args]
arg_kinds = [arg.kind for arg in args]
assert None not in arg_types
signature = CallableType(cast(List[Type], arg_types), arg_kinds, arg_names,
ret_type, self.function_type)
if tvd:
signature.variables = [tvd]
func = FuncDef(method_name, args, Block([PassStmt()]))
func.info = self.info
func.type = set_callable_name(signature, func)
func._fullname = self.info.fullname() + '.' + method_name
func.line = self.info.line
self.info.names[method_name] = SymbolTableNode(MDEF, func)
# Add the created methods to the body so that they can get further semantic analysis.
# e.g. Forward Reference Resolution.
self.info.defn.defs.body.append(func)
示例2: add_method
# 需要导入模块: from mypy.nodes import FuncDef [as 别名]
# 或者: from mypy.nodes.FuncDef import line [as 别名]
def add_method(
ctx: ClassDefContext,
name: str,
args: List[Argument],
return_type: Type,
self_type: Optional[Type] = None,
tvar_def: Optional[TypeVarDef] = None,
) -> None:
"""Adds a new method to a class.
"""
info = ctx.cls.info
self_type = self_type or fill_typevars(info)
function_type = ctx.api.named_type('__builtins__.function')
args = [Argument(Var('self'), self_type, None, ARG_POS)] + args
arg_types, arg_names, arg_kinds = [], [], []
for arg in args:
assert arg.type_annotation, 'All arguments must be fully typed.'
arg_types.append(arg.type_annotation)
arg_names.append(arg.variable.name())
arg_kinds.append(arg.kind)
signature = CallableType(arg_types, arg_kinds, arg_names, return_type, function_type)
if tvar_def:
signature.variables = [tvar_def]
func = FuncDef(name, args, Block([PassStmt()]))
func.info = info
func.type = set_callable_name(signature, func)
func._fullname = info.fullname() + '.' + name
func.line = info.line
info.names[name] = SymbolTableNode(MDEF, func, plugin_generated=True)
info.defn.defs.body.append(func)