Skip to content

Find SQLAlchemy Mapped model fields in parent classes - #4911

Closed
hrolfurgylfa wants to merge 1 commit into
facebook:mainfrom
hrolfurgylfa:bug-4903
Closed

hrolfurgylfa wants to merge 1 commit into
facebook:mainfrom
hrolfurgylfa:bug-4903

Conversation

@hrolfurgylfa

Copy link
Copy Markdown
Contributor

Summary

This solves #4903 by going through the method resolution order and finding all the Mapped fields on it or its parent classes, instead of just looking at the target class itself.

Fixes #4903

Test Plan

Added tests with both a parent class that is an SQLAlchemy model and one that isn't, and thus has to use it as a Mixin to get its Mapped fields.

@meta-codesync

meta-codesync Bot commented Sep 12, 2026

Copy link
Copy Markdown
Contributor

This pull request has been imported. If you are a Meta employee, you can view this in D119762949. (Because this pull request was imported automatically, there will not be any future comments.)

@github-actions

This comment has been minimized.

@hrolfurgylfa
hrolfurgylfa marked this pull request as ready for review September 12, 2026 01:59

@rchen152 rchen152 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This looks close, but I believe the MRO walk is backwards. I'd expect it to start with model, then go through ancestors_no_object in the regular direction, and collect only the names whose first appearance is a Mapped field (to handle attribute overrides).

This solves facebook#4903 by going through the method resolution order and
finding all the Mapped fields on it or its parent classes, instead of
just looking at the target class itself.
@hrolfurgylfa

Copy link
Copy Markdown
Contributor Author

Thanks! This looks close, but I believe the MRO walk is backwards. I'd expect it to start with model, then go through ancestors_no_object in the regular direction, and collect only the names whose first appearance is a Mapped field (to handle attribute overrides).

Thanks for the review, yep, forgot to consider what happens with overwritten fields. I've now added that to the test, reversed the mro and modified the walk to ignore fields it has already seen.

I also did some cleanup, found a way to skip the loop within loop with flat_map, and fixed the test, I was filtering on User.id for the AdminUser. I think that should still be valid SQLAlchemy, but it is very weird.

@github-actions

Copy link
Copy Markdown

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@rchen152 rchen152 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review automatically exported from Phabricator review in Meta.

@rchen152

Copy link
Copy Markdown
Contributor

LGTM, thanks!

@meta-codesync meta-codesync Bot closed this in 69ffdf4 Sep 14, 2026
@meta-codesync meta-codesync Bot added the Merged label Sep 14, 2026
@meta-codesync

meta-codesync Bot commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

This pull request has been merged in 69ffdf4.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

unexpected-keyword false positive from SQLAlchemy update().values() with models that use inheritance

3 participants