From: Mike Bayer Date: Fri, 24 Mar 2023 15:11:54 +0000 (-0400) Subject: warn for all unmapped expressions X-Git-Tag: rel_2_0_8~10^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d998b550a1857bd9f3b2418e0ac1592c4cb70ef9;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git warn for all unmapped expressions Expanded the warning emitted when a plain :func:`_sql.column` object is present in a Declarative mapping to include any arbitrary SQL expression that is not declared within an appropriate property type such as :func:`_orm.column_property`, :func:`_orm.deferred`, etc. These attributes are otherwise not mapped at all and remain unchanged within the class dictionary. As it seems likely that such an expression is usually not what's intended, this case now warns for all such otherwise ignored expressions, rather than just the :func:`_sql.column` case. Fixes: #9537 Change-Id: Ic4ca7a071a28adf4ea8680690025d927522a0805 --- diff --git a/doc/build/changelog/unreleased_20/9537.rst b/doc/build/changelog/unreleased_20/9537.rst new file mode 100644 index 0000000000..9d39d479e3 --- /dev/null +++ b/doc/build/changelog/unreleased_20/9537.rst @@ -0,0 +1,12 @@ +.. change:: + :tags: bug, orm + :tickets: 9537 + + Expanded the warning emitted when a plain :func:`_sql.column` object is + present in a Declarative mapping to include any arbitrary SQL expression + that is not declared within an appropriate property type such as + :func:`_orm.column_property`, :func:`_orm.deferred`, etc. These attributes + are otherwise not mapped at all and remain unchanged within the class + dictionary. As it seems likely that such an expression is usually not + what's intended, this case now warns for all such otherwise ignored + expressions, rather than just the :func:`_sql.column` case. diff --git a/lib/sqlalchemy/orm/decl_base.py b/lib/sqlalchemy/orm/decl_base.py index d01aad4395..6be5142765 100644 --- a/lib/sqlalchemy/orm/decl_base.py +++ b/lib/sqlalchemy/orm/decl_base.py @@ -1277,11 +1277,14 @@ class _ClassScanMapperConfig(_MapperConfig): def _warn_for_decl_attributes( self, cls: Type[Any], key: str, c: Any ) -> None: - if isinstance(c, expression.ColumnClause): + if isinstance(c, expression.ColumnElement): util.warn( f"Attribute '{key}' on class {cls} appears to " - "be a non-schema 'sqlalchemy.sql.column()' " - "object; this won't be part of the declarative mapping" + "be a non-schema SQLAlchemy expression " + "object; this won't be part of the declarative mapping. " + "To map arbitrary expressions, use ``column_property()`` " + "or a similar function such as ``deferred()``, " + "``query_expression()`` etc. " ) def _produce_column_copies( diff --git a/test/orm/declarative/test_basic.py b/test/orm/declarative/test_basic.py index 4aca4daa6d..2d712c823e 100644 --- a/test/orm/declarative/test_basic.py +++ b/test/orm/declarative/test_basic.py @@ -1301,10 +1301,10 @@ class DeclarativeMultiBaseTest( class_mapper(Bar).get_property("some_data").columns[0] is t.c.data ) - def test_lower_case_c_column_warning(self): + def test_non_sql_expression_warning_one(self): with assertions.expect_warnings( r"Attribute 'x' on class