-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[ty] support accessing __builtins__
global
#18118
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
[ty] support accessing __builtins__
global
#18118
Conversation
|
Thank you for your contribution! You found the right place to add the |
} else if name == "__builtins__" { | ||
Symbol::bound(KnownClass::ModuleType.to_instance(db)).into() |
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.
To quote the docs:
As an implementation detail, most modules have the name
__builtins__
made available as part of their globals. The value of__builtins__
is normally either this module or the value of this module’s__dict__
attribute. Since this is an implementation detail, it may not be used by alternate implementations of Python.
As such, the type of __builtins__
is, as David has pointed out, the builtins
module, and the symbol's boundness should be PossiblyUnbound
.
@@ -15,6 +15,7 @@ reveal_type(__package__) # revealed: str | None | |||
reveal_type(__doc__) # revealed: str | None | |||
reveal_type(__spec__) # revealed: ModuleSpec | None | |||
reveal_type(__path__) # revealed: MutableSequence[str] | |||
reveal_type(__builtins__) # revealed: ModuleType | |||
|
|||
class X: | |||
reveal_type(__name__) # revealed: str |
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.
As per the previous review comment, these tests should be added as well:
import sys
reveal_type(sys.__builtins__) # revealed: <module 'builtins'>
from builtins import __builtins__ as __bi__
reveal_type(__bi__) # revealed: <module 'builtins'>
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.
I added tests for these cases.
This is actually not that hard: all call sites of
As for |
I notice that one of the ecosystem hits is using So this suggests to me that we must either infer the type of |
(I agree with @carljm -- let's just use |
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.
Thank you for the updates!
The mypy_primer results also look as expected now. |
Hi, this is in regards to astral-sh/ty#393 and intends to support
__builtins__
access.Summary
The PR adds an explicit check for
"__builtins__"
during name lookup, similar to how"__file__"
is implemented. The inferred type isModuleType
.I'm new to working on ty, so any feedback is welcome:)
Test Plan
Added a markdown test for
__builtins__
.