from ..sql.roles import DDLConstraintColumnRole
from ..sql.schema import Column
from ..sql.schema import SchemaConst
+from ..sql.type_api import TypeEngine
from ..util.typing import de_optionalize_union_types
from ..util.typing import de_stringify_annotation
from ..util.typing import de_stringify_union_elements
return
self._init_column_for_annotation(
- cls, registry, extracted_mapped_annotation, originating_module
+ cls,
+ registry,
+ extracted_mapped_annotation,
+ originating_module,
)
@util.preload_module("sqlalchemy.orm.decl_base")
if new_sqltype is not None:
break
else:
- raise sa_exc.ArgumentError(
- f"Could not locate SQLAlchemy Core "
- f"type for Python type: {our_type}"
- )
+ if isinstance(our_type, TypeEngine) or (
+ isinstance(our_type, type)
+ and issubclass(our_type, TypeEngine)
+ ):
+ raise sa_exc.ArgumentError(
+ f"The type provided inside the {self.column.key!r} "
+ "attribute Mapped annotation is the SQLAlchemy type "
+ f"{our_type}. Expected a Python type instead"
+ )
+ else:
+ raise sa_exc.ArgumentError(
+ "Could not locate SQLAlchemy Core type for Python "
+ f"type {our_type} inside the {self.column.key!r} "
+ "attribute Mapped annotation"
+ )
self.column._set_type(new_sqltype)
with expect_raises_message(
sa_exc.ArgumentError,
- "Could not locate SQLAlchemy Core type for Python type: .*MyClass",
+ "Could not locate SQLAlchemy Core type for Python type "
+ ".*MyClass.* inside the 'data' attribute Mapped annotation",
):
class User(decl_base):
id: Mapped[int] = mapped_column(primary_key=True)
data: Mapped[MyClass] = mapped_column()
+ def test_construct_lhs_sqlalchemy_type(self, decl_base):
+
+ with expect_raises_message(
+ sa_exc.ArgumentError,
+ "The type provided inside the 'data' attribute Mapped "
+ "annotation is the SQLAlchemy type .*BigInteger.*. Expected "
+ "a Python type instead",
+ ):
+
+ class User(decl_base):
+ __tablename__ = "users"
+
+ id: Mapped[int] = mapped_column(primary_key=True)
+ data: Mapped[BigInteger] = mapped_column()
+
def test_construct_rhs_type_override_lhs(self, decl_base):
class Element(decl_base):
__tablename__ = "element"
with expect_raises_message(
sa_exc.ArgumentError,
- "Could not locate SQLAlchemy Core type for Python type: .*MyClass",
+ "Could not locate SQLAlchemy Core type for Python type "
+ ".*MyClass.* inside the 'data' attribute Mapped annotation",
):
class User(decl_base):
id: Mapped[int] = mapped_column(primary_key=True)
data: Mapped[MyClass] = mapped_column()
+ def test_construct_lhs_sqlalchemy_type(self, decl_base):
+
+ with expect_raises_message(
+ sa_exc.ArgumentError,
+ "The type provided inside the 'data' attribute Mapped "
+ "annotation is the SQLAlchemy type .*BigInteger.*. Expected "
+ "a Python type instead",
+ ):
+
+ class User(decl_base):
+ __tablename__ = "users"
+
+ id: Mapped[int] = mapped_column(primary_key=True)
+ data: Mapped[BigInteger] = mapped_column()
+
def test_construct_rhs_type_override_lhs(self, decl_base):
class Element(decl_base):
__tablename__ = "element"