mirror of
https://github.com/Instagram/LibCST.git
synced 2025-12-23 10:35:53 +00:00
[ScopeProvider] Expose more granular Assignments and Accesses for dotted imports (#284)
This commit is contained in:
parent
30cb9f3c6a
commit
477a03e3a8
6 changed files with 175 additions and 61 deletions
|
|
@ -141,13 +141,26 @@ There are four different type of scope in Python:
|
|||
:class:`~libcst.metadata.ComprehensionScope`.
|
||||
|
||||
.. image:: _static/img/python_scopes.png
|
||||
:alt: LibCST
|
||||
:alt: Diagram showing how the above 4 scopes are nested in each other
|
||||
:width: 400
|
||||
:align: center
|
||||
|
||||
LibCST allows you to inspect these scopes to see what local variables are
|
||||
assigned or accessed within.
|
||||
|
||||
.. note::
|
||||
Import statements bring new symbols into scope that are declared in other files.
|
||||
As such, they are represented by :class:`~libcst.metadata.Assignment` for scope
|
||||
analysis purposes. Dotted imports (e.g. ``import a.b.c``) generate multiple
|
||||
:class:`~libcst.metadata.Assignment` objects — one for each module. When analyzing
|
||||
references, only the most specific access is recorded.
|
||||
|
||||
For example, the above ``import a.b.c`` statement generates three
|
||||
:class:`~libcst.metadata.Assignment` objects: one for ``a``, one for ``a.b``, and
|
||||
one for ``a.b.c``. A reference for ``a.b.c`` records an access only for the last
|
||||
assignment, while a reference for ``a.d`` only records an access for the
|
||||
:class:`~libcst.metadata.Assignment` representing ``a``.
|
||||
|
||||
.. autoclass:: libcst.metadata.ScopeProvider
|
||||
:no-undoc-members:
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue