-
Notifications
You must be signed in to change notification settings - Fork 189
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
Add builtin scope #469
Conversation
There was a problem hiding this 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
There was a problem hiding this 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?
c8e786a
to
1078596
Compare
FWIW I've made the BuiltinAssignments be lazily created in 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. |
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? |
Yeah, it'll be better to point out the range code is not included in the example source code.
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.
Sure, feel free to propose change in another PR. |
Summary
Builtins were not being properly shadowed when redefined. There were two issues:
__getitem__
, making iteration unstableTo 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