Aaryan123456679 commented on code in PR #70793:
URL: https://github.com/apache/airflow/pull/70793#discussion_r3951462043


##########
airflow-core/src/airflow/utils/db.py:
##########
@@ -1104,11 +1104,20 @@ def reflect_tables(tables: list[MappedClassProtocol | 
str] | None, session, sche
     else:
         for tbl in tables:
             try:
-                table_name = tbl if isinstance(tbl, str) else tbl.__tablename__
                 tbl_schema: str | None
-                tbl_schema, sep, name = table_name.partition(".")
-                if not sep:
-                    tbl_schema, name = None, table_name
+                if isinstance(tbl, str):
+                    tbl_schema, sep, name = tbl.partition(".")
+                    if not sep:
+                        tbl_schema, name = None, tbl
+                else:
+                    # A mapped class already carries its configured schema 
(e.g. via
+                    # ``sql_alchemy_schema``) on its table; a bare 
``__tablename__`` would
+                    # discard it and fall back to the connection's default 
schema.
+                    # ``__table__`` isn't declared on ``MappedClassProtocol`` 
(SQLAlchemy sets
+                    # it dynamically, invisible to mypy's static checks here), 
so read it via
+                    # ``getattr`` instead of a direct attribute access.
+                    name = tbl.__tablename__
+                    tbl_schema = getattr(tbl, "__table__").schema

Review Comment:
   Done — switched to inspect(tbl, raiseerr=True).local_table.schema. 
Mapper.local_table is typed as FromClause, which declares schema: Optional[str] 
directly, so this is fully typed — no getattr needed.
   
   One nuance: inspect() on this Protocol-typed argument resolves mypy to an 
overload returning Any | None by default. Passing raiseerr=True explicitly (its 
existing default — no behavior change) steers mypy to the correct overload, so 
no cast/assert was needed either.
   
   This also caught a bug in the test for this: it used a plain class with 
__table__/__tablename__ set as bare attributes, never actually 
SQLAlchemy-mapped, so inspect() on it raises and the test was silently 
asserting on a no-op. Fixed it to build the test class via a real 
declarative_base(metadata=...) mapping.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to