Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Derived class disambiguating fails, but only sometimes. #544

Open
scottee opened this issue Jun 5, 2024 · 4 comments
Open

Derived class disambiguating fails, but only sometimes. #544

scottee opened this issue Jun 5, 2024 · 4 comments
Labels
more-info-needed More information required.

Comments

@scottee
Copy link

scottee commented Jun 5, 2024

  • cattrs version: 23.2.3
  • Python version: 3.9
  • Operating System: MacOS and Linux

Description

I have a set of classes that all inherit from one base class. I have one class that has a unique required attribute to differentiate it defined like this:

@define(kw_only=True, init=False)
class BadSubClass(BaseClass):
    # This "arg1" field is used in other derived classes, but is never required in any other class.
    # BTW, my converter has registered structure hooks for Union[str, List[str]], which forward to str_to_list.
    arg1: Union[List[str], str] = field(converter=str_to_list)  # Converter converts single str to a list.
    other_arg1: Union[List[str], str] = field(factory=list, converter=str_to_list)
    other_arg2: Optional[str] = None

I have a unit test which tries to structure json with this BadSubClass. About half the time I get an exception that the BaseClass deserializing got extra keys (the keys of BadSubClass). Well I say half the time, but now that I'm trying to recreate the error, it won't throw the error. (You might say I'm just crazy. You'd be right, but not for this reason.)

The question is, Has anyone seen this disambiguating problem, especially when it is transient like this?

What I Did

Test case looks like this:

def test_parse_bad_class():
    # Aargh: cattrs is non-deterministic in whether it can deserialize this class.
    # Should work all the time, but sometimes it throws an exception.
    json_obj = {
        "arg1": "foo",
        "other_arg1": "blah"
        "base_class_arg4": {  # This is a different non-sub-class
            "key1": "..."
        },
    }
    result = my_converter.structure(json_obj, BaseClass)
    assert isinstance(result, BadSubClass)
@Tinche
Copy link
Member

Tinche commented Jun 6, 2024

Can you paste in the exact exception that sometimes flies out? Maybe it'll help.

@Tinche
Copy link
Member

Tinche commented Jun 6, 2024

From your snippets I'm also going to assume you're using include_subclasses too? Otherwise that structure call would always just return BaseClass.

@Tinche Tinche added the more-info-needed More information required. label Jun 6, 2024
@scottee
Copy link
Author

scottee commented Jun 6, 2024

I'll add the exact exception when I can recreate it.

And yes, I am using include_subclasses.

@scottee
Copy link
Author

scottee commented Jun 6, 2024

Here is the exception that results periodically from this issues:

      | Exception Group Traceback (most recent call last):
      |   File "", line 5, in structure_mapping
      |   File "/opt/homebrew/Caskroom/mambaforge/base/lib/python3.10/site-packages/cattrs/strategies/_subclasses.py", line 125, in struct_hook
      |     return _base_hook(val, _cl)
      |   File "<cattrs generated structure blah.BaseClass>", line 91, in structure_BaseClass
      |     if errors: raise __c_cve('While structuring ' + 'BaseClass', errors, __cl)
      | cattrs.errors.ClassValidationError: While structuring BaseClass (1 sub-exception)
      | Structuring mapping value @ key 'arg1'
      +-+---------------- 1 ----------------
        | cattrs.errors.ForbiddenExtraKeysError: Extra fields in constructor for BaseClass: other_arg1, arg1, other_arg2
        +------------------------------------

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
more-info-needed More information required.
Projects
None yet
Development

No branches or pull requests

2 participants