-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Allow aliases to qualified imports #3680
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
Allow aliases to qualified imports #3680
Conversation
mypy/semanal.py
Outdated
@@ -3381,15 +3381,16 @@ def tvar_scope_frame(self, frame: TypeVarScope) -> Iterator[None]: | |||
yield | |||
self.tvar_scope = old_scope | |||
|
|||
def lookup(self, name: str, ctx: Context, suppres_errors: bool = False) -> SymbolTableNode: | |||
def lookup(self, name: str, ctx: Context, | |||
suppres_errors: bool = False) -> Optional[SymbolTableNode]: |
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.
suppress, not suppres
This causes a crash on one of the internal Dropbox codebases. Here's a traceback:
I'll try to come up with a repro after the release. We can go ahead with the release without this PR, but if that happens I'd rather not announce module aliases as a new feature yet. |
@JukkaL Obvious bug from my side, the problem is that |
Thanks! The latest change fixed the crash. If I have to time to review this carefully, this may get included in the release. I'll need to finish a few other tasks first. |
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.
LG, just some style nits.
mypy/semanal.py
Outdated
"""Look up an unqualified name in all active namespaces.""" | ||
implicit_name = False | ||
# 1a. Name declared using 'global x' takes precedence | ||
if name in self.global_decls[-1]: | ||
if name in self.globals: | ||
return self.globals[name] | ||
else: | ||
elif not suppress_errors: |
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 this ought to be if
-- it matches the other chunks in this diff and there if
block ends with return
.
test-data/unit/check-modules.test
Outdated
@@ -1641,6 +1641,40 @@ m = n # E: Cannot assign multiple modules to name 'm' without explicit 'types.M | |||
|
|||
[builtins fixtures/module.pyi] | |||
|
|||
[case testModuleAliasToQualifiedImport] | |||
import os.path | |||
|
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 recommend way fewer blank lines in test cases.
test-data/unit/check-modules.test
Outdated
@@ -1641,6 +1641,40 @@ m = n # E: Cannot assign multiple modules to name 'm' without explicit 'types.M | |||
|
|||
[builtins fixtures/module.pyi] | |||
|
|||
[case testModuleAliasToQualifiedImport] | |||
import os.path |
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 wonder if picking a different package.module name than os.path makes more sense here? I was confused by the whatever() call until I realized this has nothing to do with the stdlib os.path.
@gvanrossum Thanks! I implemented your comments in a new commit. |
(Whoops, committed without editing the commit message.) |
Fixes #3669
This is slightly hacky, since I didn't find anything similar to
checker.disable_errors()
insemanal
and I want to avoid duplicate errors likex has no attribute y
andname x.y is not defined
.