class documentation
A project is a high-level class to analyze a collection of modules together.
Project instanciation example:
>>> # Create the project instance >>> p = Project(builtins=False, dependencies=False, ... python_version=sys.version_info[:2], ... platform=sys.platform, verbosity=1) >>> # Add the modules >>> src1 = p.add_module(ast.parse('''\ ... from deprecated import deprecated ... @deprecated ... def f():...'''), 'src1') >>> src2 = p.add_module(ast.parse('''\ ... import src1 ... @src1.deprecated ... class C:...'''), 'src2') >>> # Call analyze_project() >>> p.analyze_project() >>> # Use State accessors to collect informations >>> # about the definitions in the project and their relations. >>> # The following code dumps the expanded name of all Name and Attribute loads in the project >>> import itertools >>> result = [f'{NodeLocation.make(node, p.state.get_filename(node))} -> {p.state.expand_expr(node)}' for node in (n for n in itertools.chain.from_iterable(ast.walk(m.node) for m in p.state.get_all_modules())) if isinstance(node, (ast.Name, ast.Attribute)) and type(node.ctx).__name__=='Load'] >>> print('\n'.join(result)) ast.Name at src1:2:1 -> deprecated.deprecated ast.Attribute at src2:2:1 -> src1.deprecated ast.Name at src2:2:1 -> src1
| See Also | |
State |
| Method | __init__ |
Create a new project. |
| Method | add |
Add a module to the project, all module should be added before calling analyze_project. This will slightly transform the AST... see Transform. |
| Method | add |
Add a module from typeshed or from locally installed .pyi files or typed packages. |
| Method | analyze |
Put the project's in it's final, analyzed state. |
| Method | msg |
Log a message about this ast node. |
| Instance Variable | options |
Undocumented |
| Instance Variable | state |
Undocumented |
def add_module(self, node:
ast.Module, name: str, *, is_package: bool = False, filename: str | None = None) -> Mod:
(source)
¶
Add a module to the project, all module should be added before calling analyze_project.
This will slightly transform the AST... see Transform.
| Parameters | |
node:ast.Module | Parsed ast.Module instance, see ast.parse. |
name:str | The fully qualified name of the module. |
isbool | Whether the module is a package (the node represents __init__.py file) |
filename:str | None | The filename of the module or __init__.py file for packages. |
| Returns | |
Mod | Undocumented |
| Raises | |
StaticValueError | If the module name is already in the project. |