[ScopeProvider] Expose more granular Assignments and Accesses for dotted imports (#284)

This commit is contained in:
Zsolt Dollenstein 2020-04-21 10:27:47 +01:00 committed by GitHub
parent 30cb9f3c6a
commit 477a03e3a8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 175 additions and 61 deletions

View file

@ -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: