locally_collected_columns = self._produce_column_copies(
local_attributes_for_class,
attribute_is_overridden,
+ fixed_table,
)
else:
locally_collected_columns = {}
# acting like that for now.
if isinstance(obj, (Column, MappedColumn)):
- self._collect_annotation(
- name, annotation, is_dataclass_field, True, obj
- )
+ self._collect_annotation(name, annotation, True, obj)
# already copied columns to the mapped class.
continue
elif isinstance(obj, MapperProperty):
self._collect_annotation(
name,
obj._collect_return_annotation(),
- False,
True,
obj,
)
elif _is_mapped_annotation(annotation, cls):
- generated_obj = self._collect_annotation(
- name, annotation, is_dataclass_field, True, obj
- )
- if obj is None:
- if not fixed_table:
- collected_attributes[name] = (
- generated_obj
- if generated_obj is not None
- else MappedColumn()
- )
- else:
- collected_attributes[name] = obj
+ # Mapped annotation without any object.
+ # product_column_copies should have handled this.
+ # if future support for other MapperProperty,
+ # then test if this name is already handled and
+ # otherwise proceed to generate.
+ if not fixed_table:
+ assert name in collected_attributes
+ continue
else:
# here, the attribute is some other kind of
# property that we assume is not part of the
obj = obj.fget()
collected_attributes[name] = obj
- self._collect_annotation(
- name, annotation, True, False, obj
- )
+ self._collect_annotation(name, annotation, False, obj)
else:
generated_obj = self._collect_annotation(
- name, annotation, False, None, obj
+ name, annotation, None, obj
)
if (
obj is None
self,
name: str,
raw_annotation: _AnnotationScanType,
- is_dataclass: bool,
expect_mapped: Optional[bool],
attr_value: Any,
) -> Any:
[], Iterable[Tuple[str, Any, Any, bool]]
],
attribute_is_overridden: Callable[[str, Any], bool],
+ fixed_table: bool,
) -> Dict[str, Union[Column[Any], MappedColumn[Any]]]:
cls = self.cls
dict_ = self.clsdict_view
# copy mixin columns to the mapped class
for name, obj, annotation, is_dataclass in attributes_for_class():
- if isinstance(obj, (Column, MappedColumn)):
+ if (
+ not fixed_table
+ and obj is None
+ and _is_mapped_annotation(annotation, cls)
+ ):
+ obj = self._collect_annotation(name, annotation, True, obj)
+ if obj is None:
+ obj = MappedColumn()
+
+ locally_collected_attributes[name] = obj
+ setattr(cls, name, obj)
+
+ elif isinstance(obj, (Column, MappedColumn)):
if attribute_is_overridden(name, obj):
# if column has been overridden
# (like by the InstrumentedAttribute of the
from operator import is_not
+from typing_extensions import Annotated
+
import sqlalchemy as sa
from sqlalchemy import ForeignKey
from sqlalchemy import func
from sqlalchemy.orm import deferred
from sqlalchemy.orm import events as orm_events
from sqlalchemy.orm import has_inherited_table
+from sqlalchemy.orm import Mapped
from sqlalchemy.orm import registry
from sqlalchemy.orm import relationship
from sqlalchemy.orm import synonym
m2,
)
+ @testing.combinations(
+ "anno",
+ "anno_w_clsmeth",
+ "pep593",
+ "nonanno",
+ "legacy",
+ argnames="clstype",
+ )
+ def test_column_property_col_ref(self, decl_base, clstype):
+
+ if clstype == "anno":
+
+ class SomethingMixin:
+ x: Mapped[int]
+ y: Mapped[int] = mapped_column()
+
+ @declared_attr
+ def x_plus_y(cls) -> Mapped[int]:
+ return column_property(cls.x + cls.y)
+
+ elif clstype == "anno_w_clsmeth":
+ # this form works better w/ pylance, so support it
+ class SomethingMixin:
+ x: Mapped[int]
+ y: Mapped[int] = mapped_column()
+
+ @declared_attr
+ @classmethod
+ def x_plus_y(cls) -> Mapped[int]:
+ return column_property(cls.x + cls.y)
+
+ elif clstype == "nonanno":
+
+ class SomethingMixin:
+ x = mapped_column(Integer)
+ y = mapped_column(Integer)
+
+ @declared_attr
+ def x_plus_y(cls) -> Mapped[int]:
+ return column_property(cls.x + cls.y)
+
+ elif clstype == "pep593":
+ myint = Annotated[int, mapped_column(Integer)]
+
+ class SomethingMixin:
+ x: Mapped[myint]
+ y: Mapped[myint]
+
+ @declared_attr
+ def x_plus_y(cls) -> Mapped[int]:
+ return column_property(cls.x + cls.y)
+
+ elif clstype == "legacy":
+
+ class SomethingMixin:
+ x = Column(Integer)
+ y = Column(Integer)
+
+ @declared_attr
+ def x_plus_y(cls) -> Mapped[int]:
+ return column_property(cls.x + cls.y)
+
+ else:
+ assert False
+
+ class Something(SomethingMixin, Base):
+ __tablename__ = "something"
+
+ id: Mapped[int] = mapped_column(primary_key=True)
+
+ class SomethingElse(SomethingMixin, Base):
+ __tablename__ = "something_else"
+
+ id: Mapped[int] = mapped_column(primary_key=True)
+
+ # use the mixin twice, make sure columns are copied, etc
+ self.assert_compile(
+ select(Something.x_plus_y),
+ "SELECT something.x + something.y AS anon_1 FROM something",
+ )
+
+ self.assert_compile(
+ select(SomethingElse.x_plus_y),
+ "SELECT something_else.x + something_else.y AS anon_1 "
+ "FROM something_else",
+ )
+
def test_doc(self):
"""test documentation transfer.