Skip to content

Commit 6da9a0b

Browse files
authored
Fixed AssertionError with uppercased type decorators (#375)
Fixes #315.
1 parent 70e1a5c commit 6da9a0b

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Version history
99
- Fixed two rendering issues in ``ENUM`` columns when a non-default schema is used: an
1010
unwarranted positional argument and missing the ``schema`` argument
1111
- Fixed ``AttributeError`` when metadata contains user defined column types
12+
- Fixed ``AssertionError`` when metadata contains a column type that is a type decorator
13+
with an all-uppercase name
1214

1315
**3.0.0rc5**
1416

src/sqlacodegen/generators.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
String,
3737
Table,
3838
Text,
39+
TypeDecorator,
3940
UniqueConstraint,
4041
)
4142
from sqlalchemy.dialects.postgresql import JSONB
@@ -684,7 +685,7 @@ def get_adapted_type(self, coltype: Any) -> Any:
684685
supercls, "__visit_name__"
685686
):
686687
# Don't try to adapt UserDefinedType as it's not a proper column type
687-
if supercls is UserDefinedType:
688+
if supercls is UserDefinedType or issubclass(supercls, TypeDecorator):
688689
return coltype
689690

690691
# Hack to fix adaptation of the Enum class which is broken since

tests/test_generator_tables.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import pytest
66
from _pytest.fixtures import FixtureRequest
7+
from sqlalchemy import TypeDecorator
78
from sqlalchemy.dialects import mysql, postgresql
89
from sqlalchemy.engine import Engine
910
from sqlalchemy.schema import (
@@ -18,14 +19,19 @@
1819
UniqueConstraint,
1920
)
2021
from sqlalchemy.sql.expression import text
21-
from sqlalchemy.sql.sqltypes import NullType
22+
from sqlalchemy.sql.sqltypes import DateTime, NullType
2223
from sqlalchemy.types import INTEGER, NUMERIC, SMALLINT, VARCHAR, Text
2324

2425
from sqlacodegen.generators import CodeGenerator, TablesGenerator
2526

2627
from .conftest import validate_code
2728

2829

30+
# This needs to be uppercased to trigger #315
31+
class TIMESTAMP_DECORATOR(TypeDecorator[DateTime]):
32+
impl = DateTime
33+
34+
2935
@pytest.fixture
3036
def generator(
3137
request: FixtureRequest, metadata: MetaData, engine: Engine
@@ -45,12 +51,15 @@ def test_fancy_coltypes(generator: CodeGenerator) -> None:
4551
Column("bool", postgresql.BOOLEAN),
4652
Column("vector", VECTOR(3)),
4753
Column("number", NUMERIC(10, asdecimal=False)),
54+
Column("timestamp", TIMESTAMP_DECORATOR()),
4855
schema="someschema",
4956
)
5057

5158
validate_code(
5259
generator.generate(),
5360
"""\
61+
from tests.test_generator_tables import TIMESTAMP_DECORATOR
62+
5463
from pgvector.sqlalchemy.vector import VECTOR
5564
from sqlalchemy import Boolean, Column, Enum, MetaData, Numeric, Table
5665
@@ -63,6 +72,7 @@ def test_fancy_coltypes(generator: CodeGenerator) -> None:
6372
Column('bool', Boolean),
6473
Column('vector', VECTOR(3)),
6574
Column('number', Numeric(10, asdecimal=False)),
75+
Column('timestamp', TIMESTAMP_DECORATOR),
6676
schema='someschema'
6777
)
6878
""",

0 commit comments

Comments
 (0)