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

Add builtin scope #469

Merged
merged 7 commits into from
Mar 24, 2021
Merged

Add builtin scope #469

merged 7 commits into from
Mar 24, 2021

Conversation

lpetre
Copy link
Contributor

@lpetre lpetre commented Mar 23, 2021

Summary

Builtins were not being properly shadowed when redefined. There were two issues:

  • BuiltinAssignments were recorded in __getitem__, making iteration unstable
  • Two BaseAssignments would be recorded for an overridden builtin

To fix this, I've introduced a scope about GlobalScope called the BuiltinScope and I've moved the BuiltinAssignment logic there.

My justification for introducing the new scope: it feels like it fits the design well. Declarations in the GlobalScope will override declarations in the outer (ie builtin) scope.

Test Plan

python -m unittest libcst.metadata.tests.test_scope_provider.ScopeProviderTest

@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 23, 2021
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.

Looks good in general. I'd like @jimmylai to weigh in too, but before merging we'd need to update the docs to reflect this new scope

libcst/metadata/scope_provider.py Outdated Show resolved Hide resolved
libcst/metadata/scope_provider.py Outdated Show resolved Hide resolved
@lpetre lpetre requested a review from zsol March 23, 2021 13:11
Copy link
Contributor

@jimmylai jimmylai left a comment

Choose a reason for hiding this comment

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

Overall looks good. The implementation is simple.
The only concern I have is the overhead of creating a BuiltinScope.
There are 100+ names in builtins and the same number of BuiltinAssignment will be created.
@lpetre, can you run it on our large codebase to see it it's significantly slower?

docs/source/metadata.rst Show resolved Hide resolved
@lpetre
Copy link
Contributor Author

lpetre commented Mar 24, 2021

FWIW I've made the BuiltinAssignments be lazily created in __getitem__ instead of declaring them all in the BuiltinScope constructor.

It would be nice if we could change this in the future, as I'm worried about code that could iterate over the assignments and that iteration being mutable.

@jimmylai
Copy link
Contributor

Makes the Assignment creation lazy looks good.

Another issue is the updated scope example image looks confusing.
The original idea is to some example Python code with their scope visualized in different colors.
The source code of builtin scope is not included in the example Python code. Thus, the class range(...) may make it confusing.
How about change class range(...) as something like (names defined in builtins module).?
image

@lpetre
Copy link
Contributor Author

lpetre commented Mar 24, 2021

I thought doing the diagram like this would help convey that "range" (used multiple times in the example) is defined in the builtin scope, and that is where accesses will resolve to.

Another note about this diagram: it isn't accurate with respect to what parts of the code will be contained within the various scopes. class Cls is actually in the GlobalScope and def fn is actually in the ClassScope. Not sure if we also want to improve on that?

Maybe we can follow up in a separate PR with some clarifications?

@jimmylai
Copy link
Contributor

I thought doing the diagram like this would help convey that "range" (used multiple times in the example) is defined in the builtin scope, and that is where accesses will resolve to.

Yeah, it'll be better to point out the range code is not included in the example source code.

Another note about this diagram: it isn't accurate with respect to what parts of the code will be contained within the various scopes. class Cls is actually in the GlobalScope and def fn is actually in the ClassScope. Not sure if we also want to improve on that?

Good point. To make it more accurate, the class scope should only contain the code inside the class body, not the class definition line. It's the same for function.

Maybe we can follow up in a separate PR with some clarifications?

Sure, feel free to propose change in another PR.

@jimmylai jimmylai merged commit 4ab866e into Instagram:master Mar 24, 2021
@lpetre lpetre deleted the add_builtin_scope branch March 29, 2021 08:28
@zsol zsol mentioned this pull request Mar 29, 2021
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