Find SQLAlchemy Mapped model fields in parent classes - #4911
hrolfurgylfa wants to merge 1 commit into
Conversation
|
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.) |
This comment has been minimized.
This comment has been minimized.
rchen152
left a comment
There was a problem hiding this comment.
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.
51e6ed8 to
e54c27d
Compare
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 |
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
rchen152
left a comment
There was a problem hiding this comment.
Review automatically exported from Phabricator review in Meta.
|
LGTM, thanks! |
|
This pull request has been merged in 69ffdf4. |
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.