-
Notifications
You must be signed in to change notification settings - Fork 3
Support resolving via "from ... import ..." in normal python modules #118
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
Open
mauvilsa
wants to merge
2
commits into
JelleZijlstra:master
Choose a base branch
from
mauvilsa:python-module-imports
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,10 +15,15 @@ class ImportedInfo(NamedTuple): | |
|
||
|
||
class Resolver: | ||
def __init__(self, search_context: Optional[SearchContext] = None) -> None: | ||
def __init__( | ||
self, | ||
search_context: Optional[SearchContext] = None, | ||
search_py_files: bool = False, | ||
) -> None: | ||
if search_context is None: | ||
search_context = get_search_context() | ||
self.ctx = search_context | ||
self.search_py_files = search_py_files | ||
self._module_cache: Dict[ModulePath, Module] = {} | ||
|
||
def get_module(self, module_name: ModulePath) -> "Module": | ||
|
@@ -34,7 +39,32 @@ def get_module(self, module_name: ModulePath) -> "Module": | |
|
||
def get_name(self, module_name: ModulePath, name: str) -> ResolvedName: | ||
module = self.get_module(module_name) | ||
return module.get_name(name, self) | ||
resolved_name = module.get_name(name, self) | ||
if resolved_name is None and self.search_py_files: | ||
resolved_name = self._get_py_imported_name(module_name, name) | ||
return resolved_name | ||
|
||
def _get_py_imported_name(self, module_name: ModulePath, name: str) -> ResolvedName: | ||
resolved_name = None | ||
try: | ||
named_imports, star_imports = parser.get_py_imported_name_sources( | ||
module_name | ||
) | ||
for star_import in star_imports: | ||
import_source = ModulePath(tuple(star_import.split("."))) | ||
if import_source != module_name: | ||
# Try to resolve stub from star import | ||
resolved_name = self.get_name(import_source, name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It can happen that a name imported from one module, is also imported in that module. So this needs to be recursive. |
||
if resolved_name is not None: | ||
break | ||
if resolved_name is None and name in named_imports: | ||
import_source = ModulePath(tuple(named_imports[name].split("."))) | ||
if import_source != module_name: | ||
# Try to resolve stub from named import | ||
resolved_name = self.get_name(import_source, name) | ||
except Exception: | ||
pass | ||
return resolved_name | ||
|
||
def get_fully_qualified_name(self, name: str) -> ResolvedName: | ||
"""Public API.""" | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 think the way I'd want to do this is more in
get_module
. Currently, that will only look for pyi files, but it should look for py files too as a fallback. Basically, the library should follow https://typing.python.org/en/latest/spec/distributing.html#import-resolution-ordering and look at all the places data can come from, both pyi and py files.Then when we parse the module, we should probably do it in a different 'mode' than we do parsing .pyi files, because when parsing .py files we need to be more forgiving about unrecognized AST nodes.
We may also need to put this new functionality behind an off-by-default flag so we don't interfere with existing users that look for .py files separately already.
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.
Done.
I looked at this but unfortunately did not understand. After
get_module
is called, it is not known yet ifname
is defined in that module. This is only known aftermodule.get_name(name, self)
is called. Please look again and explain a bit more how you think it should be changed.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.
This I am already doing with the
_PyImportedSourcesExtractor
visitor I implemented. Does this look okay?