Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define gather global names visitor #657

Merged
merged 2 commits into from
Mar 9, 2022

Conversation

shannonzhu
Copy link
Contributor

Summary

Having a global name gatherer available helps us determine which names are in or out of scope when applying annotations. This is similar to how we are currently using the import gatherer visitor in a pass prior to applying annotations, but that gatherer seems to have use cases perhaps beyond typing codemods - having a stand alone one for names seemed appropriate.

Ideally we'd like to have better tracking for which names are in/out of scope that combines imports and globals/classes, which we could use in the apply type annotations codemod visitor to compare qualified names against. For now, having global names pre-calculated lets us determine whether or not a name in an annotation is a forward reference by checking if it's a name defined in the current module that we haven't yet seen (all others are considered builtins).

Test Plan

python3 -m unittest libcst.codemod.visitors.tests.test_gather_global_names
...
----------------------------------------------------------------------
Ran 3 tests in 0.070s

OK

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Mar 8, 2022
@codecov-commenter
Copy link

codecov-commenter commented Mar 8, 2022

Codecov Report

Merging #657 (184673f) into main (1713701) will increase coverage by 0.01%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #657      +/-   ##
==========================================
+ Coverage   94.78%   94.79%   +0.01%     
==========================================
  Files         247      249       +2     
  Lines       25378    25456      +78     
==========================================
+ Hits        24054    24132      +78     
  Misses       1324     1324              
Impacted Files Coverage Δ
libcst/codemod/visitors/__init__.py 100.00% <100.00%> (ø)
libcst/codemod/visitors/_apply_type_annotations.py 95.55% <100.00%> (+0.09%) ⬆️
libcst/codemod/visitors/_gather_global_names.py 100.00% <100.00%> (ø)
...emod/visitors/tests/test_apply_type_annotations.py 100.00% <100.00%> (ø)
...codemod/visitors/tests/test_gather_global_names.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1713701...184673f. Read the comment docs.

@shannonzhu shannonzhu requested a review from stroxler March 8, 2022 19:31
@shannonzhu shannonzhu force-pushed the global_names_visitor branch from da2dac2 to a96be02 Compare March 8, 2022 21:30
@shannonzhu shannonzhu force-pushed the global_names_visitor branch from a96be02 to 184673f Compare March 8, 2022 21:35
Copy link
Member

@zsol zsol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable, thanks!

@shannonzhu shannonzhu merged commit f863feb into Instagram:main Mar 9, 2022
@shannonzhu shannonzhu deleted the global_names_visitor branch March 9, 2022 18:53
facebook-github-bot pushed a commit to facebook/pyre-check that referenced this pull request Mar 9, 2022
Summary:
Pending changes introduced in PR: Instagram/LibCST#657

Make this available to unblock the last half of pyre infer diffs asap.

Reviewed By: dkgi

Differential Revision: D34729821

fbshipit-source-id: 19605dd92914cb101e5058db52d9c2a98c4502dc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants