stubgen: fix package imports with aliases #9534
Merged
+138
−9
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.
Description
Running
stubgen
on modules that import multiple packages using aliases did not produce the correct results:Input:
Result:
Test Plan
I added a bunch of new tests to stubgen.test.
I had to change one of the expected results:
testConditionalImportAll_semanal
. Previously, if more than one module import introduced the same name into the namespace, the data structures used to track imports were simply allowed to become corrupted, and the generator would produceimport FIXME as whatever
. After correcting the internal state to fix the issue that prompted this PR, I realized it would be difficult to maintain the current output for this test, so I chose to change the behavior. I went with what I think is a sane solution: if multiple import statements target the same name, rather than produce invalid output, use the last import encountered. The last import would be the one used at runtime. I also considered using the first import, since this is what mypy would use.