Skip to content

cattr.structure isn't working for classes uncluding dicts #151

Closed

Description

@Natanela
  • cattrs version: 1.7.1 (attrs-21.2.0 cattrs-1.7.1)
  • Python version: 3.8.1
  • Operating System: both Windows 10 and Linux

Description

I'm trying to structure a JSON file into a class defined with attrs.

Here is my class definition:

@attr.s(frozen=True, auto_attribs=True)
class SchedulingDefinition:
    schedule: str = attr.ib()
    job_name: str = attr.ib(default=attr.Factory(lambda self: self.notebook_name, takes_self=True))
    job_id: str = attr.ib(default=attr.Factory(lambda: hashlib.sha224(uuid.uuid4().bytes).hexdigest()[:10]))
    timeout_in_min: int = 60
    retry_count: int = 1
    parameters: Optional[Dict] = attr.ib(default=None)

I'm using the following line to destructure the JSON file:

    with open(os.path.join(scheduling_definitions_dir, scheduling_definition_file), 'rb') as f:
            definition = cattr.structure(json.load(f), SchedulingDefinition)

JSON files that contain a parameters property fail with the following error message:
Unsupported type: ~KT. Register a structure hook for it.

here is the full trace:

File "./scheduling.py", line 170, in load_scheduling_definitions
  definition = cattr.structure(json.load(f), SchedulingDefinition)
File "/opt/hostedtoolcache/Python/3.7.10/x64/lib/python3.7/site-packages/cattr/converters.py", line 223, in structure
  return self._structure_func.dispatch(cl)(obj, cl)
File "", line 17, in structure_SchedulingDefinition
File "/opt/hostedtoolcache/Python/3.7.10/x64/lib/python3.7/site-packages/cattr/converters.py", line 439, in _structure_union
  return self._structure_func.dispatch(other)(obj, other)
File "", line 2, in structure_mapping
File "", line 2, in <dictcomp>
File "/opt/hostedtoolcache/Python/3.7.10/x64/lib/python3.7/site-packages/cattr/converters.py", line 304, in _structure_default
  raise ValueError(msg)

ValueError: Unsupported type: ~KT. Register a structure hook for it.

Here is an example JSON input file which fails to structure:

{
    "schedule": "0 15 8 ? * * *",
    "job_name": "Backfill",
    "job_id": "602608e0c9",
    "timeout_in_min": 60,
    "retry_count": 1,
    "parameters": {
        "days_back": 90,
        "shard_size": "2D"
    }
}

This logic worked perfectly fine up until v1.6.0 (included) but started to fail on 1.7.0 and 1.7.1

Activity

  1. changed the title [-]cattr.structure isn't working for unannotated dicts[/-] [+]cattr.structure isn't working for dicts[/+] on May 30, 2021
  2. changed the title [-]cattr.structure isn't working for dicts[/-] [+]cattr.structure isn't working for classes uncluding dicts[/+] on May 30, 2021
  3. Tinche commented on May 30, 2021

    @Tinche
    Member

    Hm, a regression. I will fix this! It looks like the issue is handling the parameters field.

    In the meantime, your options are:

    • keep using version 1.6 until this is fixed
    • change the field definition to read: parameters: Optional[dict] = attr.ib(default=None) (so, using the built-in dict instead of the Dict from typing). This is the preferred way in 3.9 forward.
    • change the field definition to read: parameters: Optional[Dict[Any, Any]] = attr.ib(default=None) (basically flesh out the type parameters)
    • upgrade to Python 3.9, where this use case seems to work out of the box
  4. Tinche commented on May 31, 2021

    @Tinche
    Member

    This should be fixed on master now!

  5. added a commit that references this issue on Jan 27, 2022
  6. 4 remaining items

  7. added a commit that references this issue on Oct 7, 2022
  8. added a commit that references this issue on Dec 7, 2022
  9. added a commit that references this issue on Jan 27, 2023
  10. added a commit that references this issue on Nov 7, 2024
  11. added a commit that references this issue on May 2, 2025
  12. added a commit that references this issue on May 23, 2025
  13. added a commit that references this issue on Sep 17, 2025
  14. added a commit that references this issue on Oct 16, 2025
  15. added a commit that references this issue on Feb 21, 2026
  16. added a commit that references this issue on Apr 27, 2026
  17. added a commit that references this issue on Aug 30, 2026
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