class documentation

class Project: (source)

Constructor: Project(**kw)

View In Hierarchy

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_module 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_typeshed_module Add a module from typeshed or from locally installed .pyi files or typed packages.
Method analyze_project 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 __init__(self, **kw: Any): (source)

Create a new project.

Parameters
**kw:AnyAll parameters are passed to Options constructor.
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.ModuleParsed ast.Module instance, see ast.parse.
name:strThe fully qualified name of the module.
is_package:boolWhether the module is a package (the node represents __init__.py file)
filename:str | NoneThe filename of the module or __init__.py file for packages.
Returns
ModUndocumented
Raises
StaticValueErrorIf the module name is already in the project.
def add_typeshed_module(self, modname: str) -> Mod | None: (source)

Add a module from typeshed or from locally installed .pyi files or typed packages.

def analyze_project(self): (source)

Put the project's in it's final, analyzed state.

def msg(self, msg: str, ctx: HasLocation | None = None, thresh: int = 0): (source)

Log a message about this ast node.

Undocumented

Undocumented