Skip to content

disambiguators.create_uniq_field_dis_func fails to disambiguate ambiguous unions depending on parameter order #230

Closed

Description

@brakhane
  • cattrs version: 1.10.0
  • Python version: 3.10.0
  • Operating System: Win

Description

In some cases, create_uniq_field_dis_func fails to disambiguate certain unions. The order of arguments matter, which can cause some confusion if eg. the arguments are taken from a set with no strict ordering, because it sometimes works, and sometimes fails.

Consider the following program:

import attrs, cattrs

# the fields are named after the classes they share
@attrs.define
class A:
    ab: int
    a: str

@attrs.define
class B:
    ab: int
    bc: int

@attrs.define
class C:
    bc: int

cattrs.disambiguators.create_uniq_field_dis_func(A, B, C)  # no error
cattrs.disambiguators.create_uniq_field_dis_func(B, A, C)  # ValueError: <class '__main__.B'> has no usable unique attributes.

Another case where this can bite people is if Union[A, B, C] was defined, and the code works, then later on, somebody defines Union[B, A, C] in a module that is executed earlier. Python will convert the types into whatever was defined first, so suddenly, Union[A, B, C] is treated like Union[B, A, C]

Activity

  1. changed the title [-]disambiguators.create_uniq_field_dis_func fails to disambiguate unions depending on parameter order[/-] [+]disambiguators.create_uniq_field_dis_func fails to disambiguate ambiguous unions depending on parameter order[/+] on Feb 16, 2022
  2. added a commit that references this issue on Jul 6, 2026
    b2d0716
  3. added a commit that references this issue on Jul 11, 2026
    a6c0900
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions