8000 Expand imports in classes by A5rocks · Pull Request #11344 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Expand imports in classes #11344

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

Closed
wants to merge 8 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Expand imports in importing class
  • Loading branch information
A5rocks committed Oct 16, 2021
commit d3cbf253cb660ae285a5a6a66baff97c1258297b
9 changes: 9 additions & 0 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1822,6 +1822,15 @@ def visit_import_from(self, imp: ImportFrom) -> None:
module_public = use_implicit_reexport or (as_id is not None and id == as_id)

if node and not node.module_hidden:
# expand into importing class, too... unless there's something already there.
# (deciding what expands is based on line order which cannot be checked here.)
if len(self.scope.classes) >= 1:
top_class = self.scope.classes[-1]
defn = node.node
if imported_id not in top_class.names.keys() and isinstance(defn, FuncDef):
if not defn.is_decorated and not defn.is_overload:
defn.info = self.type
self.add_symbol(defn.name, defn, imp)
Copy link
Collaborator

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 and OverloadedFuncDef 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.

self.process_imported_symbol(
node, module_id, id, imported_id, fullname, module_public, context=imp
)
Expand Down
0