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

Stop parsing string annotations when no longer in a typing call #546

Merged
merged 4 commits into from
Nov 17, 2021

Conversation

lpetre
Copy link
Contributor

@lpetre lpetre commented Nov 10, 2021

Summary

# this should parse a string annotation
TypeVar("T", "int")

# this should not
TypeVar("T", func("int"))

To correct handle these nested function calls I've turned the type hint tracking in the ScopeVisitor into a stack rather than a set.

Test Plan

python -m unittest libcst.metadata.tests.test_scope_provider

@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 Nov 10, 2021
] = set()
self.__in_type_hint: Set[Union[cst.Call, cst.Annotation, cst.Subscript]] = set()
self.__in_annotation_stack: List[bool] = [False]
self.__in_type_hint_stack: List[bool] = [False]
self.__in_ignored_subscript: Set[cst.Subscript] = set()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I actually think I can remove __in_ignored_subscript and implement it by pushing False into __in_annotation_stack

@lpetre lpetre requested a review from zsol November 10, 2021 13:28
@lpetre lpetre force-pushed the fix_unparsable_string branch from 34316b5 to 948bfd8 Compare November 10, 2021 13:53
@lpetre
Copy link
Contributor Author

lpetre commented Nov 10, 2021

I didn't make the suggested change in #491 because this test is expecting that to fail for typing calls:
https://github.com/Instagram/LibCST/blob/main/libcst/metadata/tests/test_scope_provider.py#L1713-L1768

libcst/metadata/scope_provider.py Outdated Show resolved Hide resolved
@zsol
Copy link
Member

zsol commented Nov 10, 2021

I'd be happy to remove those offending tests if that'd make things easier. I think they're just there to document existing behavior.

@lpetre lpetre force-pushed the fix_unparsable_string branch from 948bfd8 to d842d40 Compare November 10, 2021 17:16
@codecov-commenter
Copy link

codecov-commenter commented Nov 10, 2021

Codecov Report

Merging #546 (cc98d39) into main (7db6ec5) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##             main     #546   +/-   ##
=======================================
  Coverage   94.77%   94.77%           
=======================================
  Files         235      235           
  Lines       23246    23254    +8     
=======================================
+ Hits        22032    22040    +8     
  Misses       1214     1214           
Impacted Files Coverage Δ
libcst/metadata/scope_provider.py 95.56% <100.00%> (+0.03%) ⬆️
libcst/metadata/tests/test_scope_provider.py 99.88% <100.00%> (+<0.01%) ⬆️

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 7db6ec5...cc98d39. Read the comment docs.

@lpetre lpetre force-pushed the fix_unparsable_string branch from 0943a39 to 3dbd5c9 Compare November 17, 2021 20:45
@lpetre lpetre changed the title Fix ScopeProvider when string type annotation is unparseable Stop parsing string annotations when no longer in a typing call Nov 17, 2021
@lpetre lpetre force-pushed the fix_unparsable_string branch from 1ec78be to cc98d39 Compare November 17, 2021 21:02
@lpetre lpetre merged commit 56386d7 into Instagram:main Nov 17, 2021
@lpetre lpetre deleted the fix_unparsable_string branch November 17, 2021 21:12
@@ -851,33 +849,36 @@ def visit_Attribute(self, node: cst.Attribute) -> Optional[bool]:

def visit_Call(self, node: cst.Call) -> Optional[bool]:
self.__top_level_attribute_stack.append(None)
self.__in_type_hint_stack.append(False)
self.__in_annotation_stack.append(False)
Copy link
Contributor Author

@lpetre lpetre Nov 23, 2021

Choose a reason for hiding this comment

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

Appending to __in_annotation_stack here was an error. Fixed in #561

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