test use of assoc prox as the default descriptor for a dataclasses.field. This exercises #8880
(self, dc_decl_base, embed_in_field, field_kw)
| 3966 | argnames="field_kw", |
| 3967 | ) |
| 3968 | def test_dc_decl_usage(self, dc_decl_base, embed_in_field, field_kw): |
| 3969 | """test use of assoc prox as the default descriptor for a |
| 3970 | dataclasses.field. |
| 3971 | |
| 3972 | This exercises #8880 |
| 3973 | |
| 3974 | """ |
| 3975 | |
| 3976 | if field_kw.pop("default_factory", False) and not embed_in_field: |
| 3977 | has_default_factory = True |
| 3978 | field_kw["default_factory"] = lambda: [ |
| 3979 | Keyword("l1"), |
| 3980 | Keyword("l2"), |
| 3981 | Keyword("l3"), |
| 3982 | ] |
| 3983 | else: |
| 3984 | has_default_factory = False |
| 3985 | |
| 3986 | class User(dc_decl_base): |
| 3987 | __allow_unmapped__ = True |
| 3988 | |
| 3989 | __tablename__ = "user" |
| 3990 | |
| 3991 | id: Mapped[int] = mapped_column( |
| 3992 | primary_key=True, repr=True, init=False |
| 3993 | ) |
| 3994 | |
| 3995 | user_keyword_associations: Mapped[List[UserKeywordAssociation]] = ( |
| 3996 | relationship( |
| 3997 | back_populates="user", |
| 3998 | cascade="all, delete-orphan", |
| 3999 | init=False, |
| 4000 | ) |
| 4001 | ) |
| 4002 | |
| 4003 | if embed_in_field: |
| 4004 | # this is an incorrect form to use with |
| 4005 | # MappedAsDataclass. However, we want to make sure it |
| 4006 | # works as kind of a test to ensure we are being as well |
| 4007 | # behaved as possible with an explicit dataclasses.field(), |
| 4008 | # by testing that it uses its normal descriptor-as-default |
| 4009 | # behavior |
| 4010 | keywords: AssociationProxy[list[str]] = dataclasses.field( |
| 4011 | default=association_proxy( |
| 4012 | "user_keyword_associations", "keyword" |
| 4013 | ), |
| 4014 | **field_kw, |
| 4015 | ) |
| 4016 | else: |
| 4017 | keywords: AssociationProxy[list[str]] = association_proxy( |
| 4018 | "user_keyword_associations", "keyword", **field_kw |
| 4019 | ) |
| 4020 | |
| 4021 | UserKeywordAssociation, Keyword = self._dc_keyword_mapping( |
| 4022 | User, dc_decl_base |
| 4023 | ) |
| 4024 | |
| 4025 | # simplify __qualname__ so we can test repr() more easily |
nothing calls this directly
no test coverage detected