Skip to content

Cannot structure to list[str] field on Python 3.8 #279

@jwodder

Description

@jwodder
  • cattrs version: 22.1.0 and 22.2.0.dev0 (commit b1a681a)
  • Python version: 3.8.13
  • Operating System: Debian bullseye

The following code:

from __future__ import annotations
from dataclasses import dataclass
import cattrs

@dataclass
class Foo:
    items: list[str]

print(cattrs.structure({"items": ["red", "green", "blue"]}, Foo))

succeeds on Python 3.9 but fails on 3.8 with the following traceback:

Traceback (most recent call last):
  File "listitems01.py", line 9, in <module>
    print(cattrs.structure({"items": ["red", "green", "blue"]}, Foo))
  File "/docked/venv/lib/python3.8/site-packages/cattrs/converters.py", line 283, in structure
    return self._structure_func.dispatch(cl)(obj, cl)
  File "/docked/venv/lib/python3.8/site-packages/cattrs/dispatch.py", line 49, in _dispatch
    return self._function_dispatch.dispatch(cl)
  File "/docked/venv/lib/python3.8/site-packages/cattrs/dispatch.py", line 122, in dispatch
    return handler(typ)
  File "/docked/venv/lib/python3.8/site-packages/cattrs/converters.py", line 780, in gen_structure_attrs_fromdict
    h = make_dict_structure_fn(
  File "/docked/venv/lib/python3.8/site-packages/cattrs/gen.py", line 265, in make_dict_structure_fn
    attrs = adapted_fields(cl)
  File "/docked/venv/lib/python3.8/site-packages/cattrs/_compat.py", line 74, in adapted_fields
    type_hints = get_type_hints(cl)
  File "/usr/local/lib/python3.8/typing.py", line 1232, in get_type_hints
    value = _eval_type(value, base_globals, localns)
  File "/usr/local/lib/python3.8/typing.py", line 270, in _eval_type
    return t._evaluate(globalns, localns)
  File "/usr/local/lib/python3.8/typing.py", line 518, in _evaluate
    eval(self.__forward_code__, globalns, localns),
  File "<string>", line 1, in <module>
TypeError: 'type' object is not subscriptable

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