Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
for stmt in body:
if isinstance(stmt, pytd.Type):
exceptions.append(stmt) # raise stmt
continue
assert isinstance(stmt, tuple) and len(stmt) == 2, stmt
mutators.append(_Mutator(stmt[0], stmt[1]))
signature = pytd.Signature(params=tuple(params.required), return_type=ret,
starargs=params.starargs,
starstarargs=params.starstarargs,
exceptions=tuple(exceptions), template=())
for mutator in mutators:
try:
signature = signature.Visit(mutator)
except NotImplementedError as e:
raise ParseError(utils.message(e))
if not mutator.successful:
raise ParseError("No parameter named %s" % mutator.name)
# Remove ignored decorators, raise ParseError for invalid decorators.
decorators = {d for d in decorators if _keep_decorator(d)}
# Extract out abstractmethod and coroutine decorators, there should be at
# most one remaining decorator.
def _check_decorator(decorators, decorator_set):
exists = any([x in decorators for x in decorator_set])
if exists:
decorators -= decorator_set
return exists
is_abstract = _check_decorator(
decorators, {"abstractmethod", "abc.abstractmethod"})
is_coroutine = _check_decorator(
decorators, {"coroutine", "asyncio.coroutine", "coroutines.coroutine"})
is_package = file_utils.is_pyi_directory_init(filename)
self._package_name = module_utils.get_package_name(name, is_package)
self._parent_name = module_utils.get_package_name(self._package_name, False)
try:
defs = parser_ext.parse(self, src)
ast = self._build_type_decl_unit(defs)
except ParseError as e:
if self._error_location:
line = e.line or self._error_location[0]
try:
text = src.splitlines()[line-1]
except IndexError:
text = None
raise ParseError(utils.message(e), line=line, filename=self._filename,
column=self._error_location[1], text=text)
else:
raise e
ast = ast.Visit(_PropertyToConstant())
ast = ast.Visit(_InsertTypeParameters(ast.type_params))
ast = ast.Visit(_VerifyMutators())
# TODO(kramm): This is in the wrong place- it should happen after resolving
# local names, in load_pytd.
ast = ast.Visit(pep484.ConvertTypingToNative(name))
if name:
ast = ast.Replace(name=name)
ast = ast.Visit(visitors.AddNamePrefix())
else:
# If there's no unique name, hash the sourcecode.
def write_pickle(ast, options, loader=None):
"""Dump a pickle of the ast to a file."""
loader = loader or load_pytd.create_loader(options)
try:
ast = serialize_ast.PrepareForExport(
options.module_name, options.python_version, ast, loader)
except parser.ParseError as e:
if options.nofail:
ast = serialize_ast.PrepareForExport(
options.module_name, options.python_version,
pytd_builtins.GetDefaultAst(options.python_version), loader)
log.warn("***Caught exception: %s", str(e), exc_info=True)
else:
raise
if options.verify_pickle:
ast1 = ast.Visit(visitors.LateTypeToClassType())
ast1 = ast1.Visit(visitors.ClearClassPointers())
ast2 = loader.load_file(options.module_name, options.verify_pickle)
ast2 = ast2.Visit(visitors.ClearClassPointers())
if not pytd_utils.ASTeq(ast1, ast2):
raise AssertionError()
serialize_ast.StoreAst(ast, options.output)
triggered on any previous conditions. It returns a bool indicating
if the scope will now be active.
if_end(self, clauses): This should be called at the end of the entire if
statement where clauses is a list of (active, defs) pairs. Active is
the return value of the corresponding if_begin/if_elif/if_else call, and
defs is a list of definitions within that block. The function returns
the list of defs that should be processed (i.e. the defs in the tuple
where active was True, or [] if no such tuple is present).
See _eval_condition for a description of conditions.
"""
# Values for the parsing context.
ELLIPSIS = object() # Special object to signal ELLIPSIS as a parameter.
PARSE_ERROR = ParseError # The class object (not an instance of it).
NOTHING = pytd.NothingType()
ANYTHING = pytd.AnythingType()
TUPLE = pytd.NamedType("tuple")
# Attributes that all namedtuple instances have.
_NAMEDTUPLE_MEMBERS = ("_asdict", "__dict__", "_fields", "__getnewargs__",
"__getstate__", "_make", "_replace")
def __init__(self, version, platform):
"""Initialize the parser.
Args:
version: A version tuple.
platform: A platform string.
"""
assert version
"Expected 2 parameters to Callable, got %d" % len(parameters))
if len(parameters) == 1:
# We're usually happy to treat omitted parameters as "Any", but we
# need a return type for CallableType, or we wouldn't know whether the
# last parameter is an argument or return type.
parameters += (pytd.AnythingType(),)
if self._is_empty_tuple(parameters[0]):
parameters = parameters[1:]
else:
parameters = parameters[0].parameters + parameters[1:]
return pytd.CallableType(base_type=base_type, parameters=parameters)
else:
assert parameters
if (self._is_callable_base_type(base_type) and
not self._is_any(parameters[0])):
raise ParseError(
"First argument to Callable must be a list of argument types")
return pytd.GenericType(base_type=base_type, parameters=parameters)
def import_module(self, name, full_name, level):
"""Import a module and return the module object or None."""
if self.options.strict_import:
# Do not import new modules if we aren't in an IMPORT statement.
# The exception is if we have an implicit "package" module (e.g.
# `import a.b.c` adds `a.b` to the list of instantiable modules.)
if not (self._importing or self.loader.has_module_prefix(full_name)):
return None
try:
module = self._import_module(name, level)
# Since we have explicitly imported full_name, add it to the prefix list.
self.loader.add_module_prefixes(full_name)
except (parser.ParseError, load_pytd.BadDependencyError,
visitors.ContainerError, visitors.SymbolLookupError) as e:
self.errorlog.pyi_error(self.frames, full_name, e)
module = self.convert.unsolvable
return module
def __init__(self, msg, line=None, filename=None, column=None, text=None):
super(ParseError, self).__init__(msg)
self._line = line
self._filename = filename
self._column = column
self._text = text
def _qualify_name_with_special_dir(self, orig_name):
"""Handle the case of '.' and '..' as package names."""
if "__PACKAGE__." in orig_name:
# Generated from "from . import foo" - see parser.yy
prefix, _, name = orig_name.partition("__PACKAGE__.")
if prefix:
raise ParseError("Cannot resolve import: %s" % orig_name)
return self._package_name + "." + name
elif "__PARENT__." in orig_name:
# Generated from "from .. import foo" - see parser.yy
prefix, _, name = orig_name.partition("__PARENT__.")
if prefix:
raise ParseError("Cannot resolve import: %s" % orig_name)
if not self._parent_name:
raise ParseError(
"Cannot resolve relative import ..: Package %s has no parent" %
self._package_name)
return self._parent_name + "." + name
else:
return None
def new_alias_or_constant(self, name_and_value):
name, value = name_and_value
if name == "__slots__":
if not isinstance(value, list):
raise ParseError("__slots__ must be a list of strings")
return _SlotDecl(tuple(_handle_string_literal(s) for s in value))
elif value in [pytd.NamedType("True"), pytd.NamedType("False")]:
return pytd.Constant(name, pytd.NamedType("bool"))
else:
return pytd.Alias(name, value)
def _keep_decorator(decorator):
"""Return True iff the decorator requires processing."""
if decorator in ["overload"]:
# These are legal but ignored.
return False
elif (decorator in ["staticmethod", "classmethod", "abstractmethod",
"coroutine"] or
_is_property_decorator(decorator)):
return True
else:
raise ParseError("Decorator %s not supported" % decorator)