Skip to content

Commit

Permalink
fix(python): Fix Case of Multiple Star Imports (#5394)
Browse files Browse the repository at this point in the history
Fix case of multiple star imports
  • Loading branch information
noanflaherty authored Dec 12, 2024
1 parent 9c06e8e commit 3d35a75
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
5 changes: 5 additions & 0 deletions generators/python-v2/ast/src/PythonFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ export class PythonFile extends AstNode {
const usedNames = this.getInitialUsedNames();

references.forEach((reference) => {
// Skip star imports since we should never override their import alias
if (reference instanceof StarImport) {
return;
}

const name = reference.alias ?? reference.name;
const fullyQualifiedModulePath = reference.getFullyQualifiedModulePath();

Expand Down
7 changes: 5 additions & 2 deletions generators/python-v2/ast/src/__test__/PythonFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,10 @@ describe("PythonFile", () => {
const file = python.file({
path: ["root"],
comments: [python.comment({ docs: "flake8: noqa: F401, F403" })],
imports: [python.starImport({ modulePath: ["root", "my_module"] })],
imports: [
python.starImport({ modulePath: ["root", "my_module_a"] }),
python.starImport({ modulePath: ["root", "my_module_b"] })
],
statements: [
python.field({
name: "my_id",
Expand All @@ -314,7 +317,7 @@ describe("PythonFile", () => {

file.write(writer);
expect(await writer.toStringFormatted()).toMatchSnapshot();
expect(file.getReferences()).toHaveLength(2);
expect(file.getReferences()).toHaveLength(3);
});

it("Write duplicative import names", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ exports[`PythonFile > Write star imports 1`] = `
"# flake8: noqa: F401, F403
from uuid import UUID
from .my_module import *
from .my_module_a import *
from .my_module_b import *
my_id = UUID("1234")
"
Expand Down

0 comments on commit 3d35a75

Please sign in to comment.