Skip to content

Emit warning on direct instantiation of unmapped MappedAsDataclass - #13587

Open
VimalN2005 wants to merge 1 commit into
sqlalchemy:mainfrom
VimalN2005:fix-10064-mapped-as-dataclass-warn
Open

Emit warning on direct instantiation of unmapped MappedAsDataclass#13587
VimalN2005 wants to merge 1 commit into
sqlalchemy:mainfrom
VimalN2005:fix-10064-mapped-as-dataclass-warn

Conversation

@VimalN2005

Copy link
Copy Markdown

Fixes

Fixes: #10064

Description

Direct instantiation of unmapped classes (mixins or abstract base classes) using MappedAsDataclass is unsupported as ORM features, column expressions, and relationship attributes cannot be resolved without a concrete mapping.

Per issue discussion and maintainer guidance:

  • Installed an initialization wrapper in _UnmappedDataclassConfig for classes subclassing MappedAsDataclass that emits an SAWarning when the unmapped class is directly instantiated (type(self_) is cls_).
  • Concrete mapped subclasses remain unaffected and instantiate their own dataclass constructors normally without warning.

Checklist

  • Added unit tests in test/orm/declarative/test_dc_transforms.py (UnmappedDataclassInstantiationTest)
  • All 370 tests in test_dc_transforms.py and test_dataclasses.py pass cleanly
  • Added changelog fragment doc/build/changelog/unreleased_21/10064.rst

Direct instantiation of unmapped classes (mixins or abstract base
classes) using MappedAsDataclass is unsupported as ORM features and
attributes cannot be resolved without concrete mapping.

An SAWarning is now emitted when instantiating such classes directly.
Concrete mapped subclasses remain unaffected and instantiate normally.

Fixes: sqlalchemy#10064
@sqla-tester

Copy link
Copy Markdown
Collaborator

Thanks! Issue #10064 is now marked code review in progress and no longer open for pull requests, so this pull request holds the review for it and another one won't land on top of your work. If this pull request is abandoned, a maintainer can put open for pull requests back on #10064 to reopen it to others.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MappedAsDataclass, prohibit constructor use and/or creation for non-mapped classes, abstract, mixins

2 participants