[ty] Fix __file__ type in completions to show str instead of str | None#22510
[ty] Fix __file__ type in completions to show str instead of str | None#22510charliermarsh merged 2 commits intomainfrom
__file__ type in completions to show str instead of str | None#22510Conversation
Diagnostic diff on typing conformance testsNo changes detected when running ty on typing conformance tests ✅ |
|
|
A related PR which is the semantics side for handling |
| builtin: true, | ||
| }), | ||
| ); | ||
|
|
There was a problem hiding this comment.
Do we still need to get completions from the builtins module below if we're doing this?
There was a problem hiding this comment.
yeah, this special case is only for the 8 or so "implicit module globals" that are present in every global namespace. Previously we only "accidentally" recognized them as being available in our autocompletion engine because they are present in the builtins-module global namespace just like every other module-global namespace. But treating them as builtin symbols was incorrect, because they might have more precise types in the global namespace of the current module than they do in the builtins module's global namespace.
So for all builtins symbols other than these 8 or so special-cased symbols, we still need to import the builtins module and lookup the global namespace as we do below.
| builtin: true, | ||
| }), | ||
| ); | ||
|
|
There was a problem hiding this comment.
yeah, this special case is only for the 8 or so "implicit module globals" that are present in every global namespace. Previously we only "accidentally" recognized them as being available in our autocompletion engine because they are present in the builtins-module global namespace just like every other module-global namespace. But treating them as builtin symbols was incorrect, because they might have more precise types in the global namespace of the current module than they do in the builtins module's global namespace.
So for all builtins symbols other than these 8 or so special-cased symbols, we still need to import the builtins module and lookup the global namespace as we do below.
Co-authored-by: Alex Waygood <[email protected]>
Summary
The type inference system already correctly special-cases
__file__to returnstrfor the current module (since the code is executing from an existing file). However, the completion system was bypassing this logic and pulling__file__: str | Nonedirectly fromtypes.ModuleTypein typeshed.This PR adds implicit module globals (like
__file__,__name__, etc.) with their correctly-typed values to completions, reusing the existingmodule_type_implicit_global_symbolfunction that already handles the special-casing.Closes astral-sh/ty#2445.