allow callable for relationship.back_populates
The :paramref:`_orm.relationship.back_populates` argument to
:func:`_orm.relationship` may now be passed as a Python callable, which
resolves to either the direct linked ORM attribute, or a string value as
before. ORM attributes are also accepted directly by
:paramref:`_orm.relationship.back_populates`. This change allows type
checkers and IDEs to confirm the argument for
:paramref:`_orm.relationship.back_populates` is valid. Thanks to Priyanshu
Parikh for the help on suggesting and helping to implement this feature.
An attempt was made to do this for ForeignKey as well, however this
is not feasible since there is no "deferred configuration" step
for Table objects; Table objects set each other up on ForeignKey
as they are created, such as setting the type of a column
in a referencing Column when the referenced table is created. We
have no way to know which Table a foreign key intends to represent
when it's a callable whereas when it's a string, we do know, and
we actually make a lot of use of that string to match it to the
target table as that target is created (see _fk_memos). However
the commit keeps a little bit of the cleanup to ForeignKey intact.
Co-authored-by: Mike Bayer <mike_mp@zzzcomputing.com>
Fixes: #10050
Closes: #10260
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/10260
Pull-request-sha:
6f21002e1d5bbe291111655f33b19e4eb4b3cb84
Change-Id: I8e0a40c9898ec91d44f2df06dcc22f33b06745c3