-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix spurious name undefined error in class body within import cycle (#…
…10498) This could sometimes happen with protobuf stubs. The issue is a quite tricky one, since it only happens if files in an import cycle are checked in a specific order. The order may depend on the order files are given as arguments to mypy. The problem was with code like this, if `Foo` and `Base` are defined in different files within an import cycle: ``` # m.py from m2 import Base class Foo(Base): x: Bar # <<-- Unexpected error: "Bar" undefined class Bar: ... ``` Due to the import cycle, `Base` could be a placeholder node when semantically analyzing `m` for the first time. This caused another pass over `m`. On the second pass `Bar` was reported as undefined, because of an incorrect namespace completeness check. We were checking the completeness of the *module-level* namespace, when we should have looked at the completeness of the *class* namespace. If `Base` was ready during the first pass, the example worked as expected, since neither the module nor the class namespace was complete. Errors about undefined things are only supposed to be generated when the target namespace is complete (i.e., all names are included in the symbol table, possibly as placholders). This fixes the issue by keeping track of whether a class body is being processed for the first time. During the first time the namespace is being built, so it's incomplete. This may not work in some very tricky scenarios where we need to process the body of a class more than twice, but these cases are probably very rare, so this fix should get us most of the way there.
- Loading branch information
Showing
4 changed files
with
60 additions
and
3 deletions.
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
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