def findAll(mod_ast: ast.Module, mod: model.Module) -> None: """Find and attempt to parse into a list of names the __all__ of a module's AST.""" for node in mod_ast.body: if isinstance(node, ast.Assign) and \ len(node.targets) == 1 and \ isinstance(node.targets[0], ast.Name) and \ node.targets[0].id == '__all__': if not isinstance(node.value, (ast.List, ast.Tuple)): mod.report( 'Cannot parse value assigned to "__all__"', section='all', lineno_offset=node.lineno) continue names = [] for idx, item in enumerate(node.value.elts): try: name: object = ast.literal_eval(item) except ValueError: mod.report( f'Cannot parse element {idx} of "__all__"', section='all', lineno_offset=node.lineno) else: if isinstance(name, str): names.append(name) else: mod.report( f'Element {idx} of "__all__" has ' f'type "{type(name).__name__}", expected "str"', section='all', lineno_offset=node.lineno) if mod.all is not None: mod.report( 'Assignment to "__all__" overrides previous assignment', section='all', lineno_offset=node.lineno) mod.all = names
def parseAll(node: ast.Assign, mod: model.Module) -> None: """Find and attempt to parse into a list of names the C{__all__} variable of a module's AST and set L{Module.all} accordingly.""" if not isinstance(node.value, (ast.List, ast.Tuple)): mod.report( 'Cannot parse value assigned to "__all__"', section='all', lineno_offset=node.lineno) return names = [] for idx, item in enumerate(node.value.elts): try: name: object = ast.literal_eval(item) except ValueError: mod.report( f'Cannot parse element {idx} of "__all__"', section='all', lineno_offset=node.lineno) else: if isinstance(name, str): names.append(name) else: mod.report( f'Element {idx} of "__all__" has ' f'type "{type(name).__name__}", expected "str"', section='all', lineno_offset=node.lineno) if mod.all is not None: mod.report( 'Assignment to "__all__" overrides previous assignment', section='all', lineno_offset=node.lineno) mod.all = names
def parseDocformat(node: ast.Assign, mod: model.Module) -> None: """ Find C{__docformat__} variable of this module's AST and set L{Module.docformat} accordingly. This is all valid:: __docformat__ = "reStructuredText en" __docformat__ = "epytext" __docformat__ = "restructuredtext" """ try: value = ast.literal_eval(node.value) except ValueError: mod.report( 'Cannot parse value assigned to "__docformat__": not a string', section='docformat', lineno_offset=node.lineno) return if not isinstance(value, str): mod.report( 'Cannot parse value assigned to "__docformat__": not a string', section='docformat', lineno_offset=node.lineno) return if not value.strip(): mod.report( 'Cannot parse value assigned to "__docformat__": empty value', section='docformat', lineno_offset=node.lineno) return # Language is ignored and parser name is lowercased. value = value.split(" ", 1)[0].lower() if mod._docformat is not None: mod.report( 'Assignment to "__docformat__" overrides previous assignment', section='docformat', lineno_offset=node.lineno) mod.docformat = value
def _uses_auto_attribs(call: ast.Call, module: model.Module) -> bool: """Does the given L{attr.s()} decoration contain C{auto_attribs=True}? @param call: AST of the call to L{attr.s()}. This function will assume that L{attr.s()} is called without verifying that. @param module: Module that contains the call, used for error reporting. @return: L{True} if L{True} is passed for C{auto_attribs}, L{False} in all other cases: if C{auto_attribs} is not passed, if an explicit L{False} is passed or if an error was reported. """ try: args = bind_args(_attrs_decorator_signature, call) except TypeError as ex: message = str(ex).replace("'", '"') module.report( f"Invalid arguments for attr.s(): {message}", lineno_offset=call.lineno ) return False auto_attribs_expr = args.arguments.get('auto_attribs') if auto_attribs_expr is None: return False try: value = ast.literal_eval(auto_attribs_expr) except ValueError: module.report( 'Unable to figure out value for "auto_attribs" argument ' 'to attr.s(), maybe too complex', lineno_offset=call.lineno ) return False if not isinstance(value, bool): module.report( f'Value for "auto_attribs" argument to attr.s() ' f'has type "{type(value).__name__}", expected "bool"', lineno_offset=call.lineno ) return False return value
def test_include_private_not_in_all(): system = TwistedSystem() m = Module(system, "somemodule", "module doc") m.all = [] c = Class(system, "_private", "some doc", m) assert c.isVisible