Skip to content

Commit

Permalink
Fixes agronholm#285 drop duplicate imports
Browse files Browse the repository at this point in the history
  • Loading branch information
hyoj0942 committed Sep 27, 2023
1 parent 44eec23 commit 3aa604c
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/sqlacodegen/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,15 @@ def group_imports(self) -> list[list[str]]:
future_imports: list[str] = []
stdlib_imports: list[str] = []
thirdparty_imports: list[str] = []
duplicate_imports = set()

for package in sorted(self.imports):
imports = ", ".join(sorted(self.imports[package]))
imports = sorted(self.imports[package])
unique_imports = [
import_ for import_ in imports if import_ not in duplicate_imports
]
duplicate_imports.update(imports)
imports_str = ", ".join(unique_imports)
collection = thirdparty_imports
if package == "__future__":
collection = future_imports
Expand All @@ -305,7 +311,7 @@ def group_imports(self) -> list[list[str]]:
if "site-packages" not in (sys.modules[package].__file__ or ""):
collection = stdlib_imports

collection.append(f"from {package} import {imports}")
collection.append(f"from {package} import {imports_str}")

for module in sorted(self.module_imports):
thirdparty_imports.append(f"import {module}")
Expand Down

0 comments on commit 3aa604c

Please sign in to comment.