Skip to content

Commit fd2afca

Browse files
committed
Fixed same-name imports from wrong package
1 parent e4c32c5 commit fd2afca

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Version history
1111
``sqlalchemy-citext``, resolving ``PackageNotFoundError`` (PR by @oaimtiaz)
1212
- Prevent double pluralization (PR by @dkratzert)
1313
- Fixes DOMAIN extending JSON/JSONB data types (PR by @sheinbergon)
14+
- Fixes imports from wrong package if name shared
1415

1516
**3.0.0**
1617

src/sqlacodegen/generators.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,9 @@ def add_import(self, obj: Any) -> None:
276276

277277
if type_.__name__ in dialect_pkg.__all__:
278278
pkgname = dialect_pkgname
279-
elif type_.__name__ in dir(sqlalchemy):
279+
elif type_.__name__ in dir(sqlalchemy) and type_ == getattr(
280+
sqlalchemy, type_.__name__
281+
):
280282
pkgname = "sqlalchemy"
281283
else:
282284
pkgname = type_.__module__

tests/test_generator_sqlmodel.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import pytest
44
from _pytest.fixtures import FixtureRequest
5+
from sqlalchemy import Uuid
56
from sqlalchemy.engine import Engine
67
from sqlalchemy.schema import (
78
CheckConstraint,
@@ -186,3 +187,27 @@ class SimpleOnetoone(SQLModel, table=True):
186187
back_populates='simple_onetoone')
187188
""",
188189
)
190+
191+
192+
def test_uuid(generator: CodeGenerator) -> None:
193+
Table(
194+
"simple_uuid",
195+
generator.metadata,
196+
Column("id", Uuid, primary_key=True),
197+
)
198+
199+
validate_code(
200+
generator.generate(),
201+
"""\
202+
from typing import Optional
203+
from uuid import UUID
204+
205+
from sqlalchemy import Column, Uuid
206+
from sqlmodel import Field, SQLModel
207+
208+
class SimpleUuid(SQLModel, table=True):
209+
__tablename__ = 'simple_uuid'
210+
211+
id: Optional[UUID] = Field(default=None, sa_column=Column('id', Uuid, primary_key=True))
212+
""",
213+
)

0 commit comments

Comments
 (0)