-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Expand imports in classes #11344
Closed
Closed
Expand imports in classes #11344
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d3cbf25
Expand imports in importing class
A5rocks 8159bae
Add tests
A5rocks f629a9c
Pass type-checking
A5rocks 458ed8f
Don't expand imports in methods
A5rocks 9886803
Add some more test cases
A5rocks 35d475d
Fix test cases
A5rocks 8d954a9
Merge branch 'master' into expand-imports
A5rocks efaecf4
Merge branch 'master' into expand-imports
A5rocks 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 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 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 |
---|---|---|
|
@@ -6968,3 +6968,68 @@ class B(A): # E: Final class __main__.B has abstract attributes "foo" | |
[case testUndefinedBaseclassInNestedClass] | ||
class C: | ||
class C1(XX): pass # E: Name "XX" is not defined | ||
|
||
[case testExpandImportedMethodInClass] | ||
# https://github.com/python/mypy/issues/7045 | ||
class Foo: | ||
from a import meth | ||
A5rocks marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
reveal_type(Foo().meth) # N: Revealed type is "def (x: builtins.int) -> builtins.int" | ||
[file a.py] | ||
def meth(self, x: int) -> int: | ||
... | ||
|
||
[case testExpandImportedFunctionInClass] | ||
class Foo: | ||
from a import func | ||
|
||
reveal_type(Foo.func) # N: Revealed type is "def () -> builtins.str" | ||
[file a.py] | ||
def func() -> str: | ||
... | ||
|
||
[case testExpandImportedMethodInNestedClass] | ||
class Foo: | ||
class Bar: | ||
from a import meth | ||
|
||
reveal_type(Foo.Bar().meth) # N: Revealed type is "def (x: builtins.int) -> builtins.int" | ||
[file a.py] | ||
def meth(self, x: int) -> int: | ||
... | ||
|
||
[case testExpandUndefinedImports] | ||
class Foo: | ||
from not_a_module import func | ||
|
||
reveal_type(Foo.func) | ||
reveal_type(Foo().func) | ||
[out] | ||
main:2: error: Cannot find implementation or library stub for module named "not_a_module" | ||
main:2: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports | ||
main:4: note: Revealed type is "Any" | ||
main:5: note: Revealed type is "Any" | ||
|
||
[case testExpandImportedMethodWithFollowImports] | ||
# flags: --follow-imports=skip | ||
class Foo: | ||
from a import meth | ||
|
||
reveal_type(Foo().meth) # N: Revealed type is "Any" | ||
[file a.py] | ||
def meth(self, x: int) -> int: | ||
... | ||
|
||
[case testExpandWithNonSelfFirstArgument] | ||
class Foo: | ||
from a import func | ||
|
||
reveal_type(Foo().func) | ||
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. Test what happens if we import two functions with the same name (from different modules)? Also test what happens if we import a function over an existing name. Test multiple classes that import the same function. Test calling the original function via module and instance. |
||
reveal_type(Foo.func) | ||
[file a.py] | ||
def func(self: str, x: int) -> int: | ||
... | ||
[out] | ||
main:4: error: Invalid self argument "Foo" to attribute function "func" with type "Callable[[str, int], int]" | ||
main:4: note: Revealed type is "def (x: builtins.int) -> builtins.int" | ||
main:5: note: Revealed type is "def (self: builtins.str, x: builtins.int) -> builtins.int" |
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.
Unfortunately this doesn't seem right/enough. We modify the function as defined in the original module, but it should only be treated as a method in this class, not elsewhere. I wonder what would happen if import the same function into multiple classes. For completeness, we should probably do this for
Decorator
andOverloadedFuncDef
as well.Implementing this in a full and correct way seems somewhat complicated. We could perhaps take a copy of the original
FuncDef
(or other node) and only modify the copy.Alternatively, you could detect this and generate a blocking error, and not add anything to the symbol table. This would be the easiest fix. The error message can contain a note that suggests doing something like
name = mod.func
instead.