Closed eddawley closed 4 years ago
Hi @eddawley - thank you for using dacite
:)
PEP 563 (from __future__ import annotations
) fixes forward references problem, so if you want to use some eg. class before it's defined you don't have to use ""
to use it as a type hint. Short example:
from __future__ import annotations
from dataclasses import dataclass
@dataclass
class A:
b: B # <- you don't have to use ""
@dataclass
class B:
i: int
In your example I don't see any forward reference, but it looks that instead of typing.List
you are using list
- this is the reason why you get object is not subscriptable
error.
That's the wrong PEP for this error. PEP 585 describes the other feature that future annotations provides. The bug has nothing to do with forward references.
Sorry I was confused because you are using Python 3.7.
PEP 585 works in dacite
, but only with Python 3.9. dacite
plays with type hints during the runtime so with annotations like list[int]
and Python <3.9 it's impossible to run functions like e.g. typing.get_type_hints
(which dacite
is using).
The following example....
from __future__ import annotations
from typing import get_type_hints
def foo(x: list[int]): pass
print(get_type_hints(foo))
... will pass with 3.9 but it will raise object is not subscriptable
with <3.9.
I don't know if I can do something here.
Understood. Mostly I wanted to document the bug for any future users who hit it.
Does something similar as the following solve the issue here? https://github.com/pydantic/pydantic/blob/f2c3a49d662378998dd1962fe0ca0ec160a7ad53/pydantic/typing.py#L369-L398
Dacite throws the following error under Python 3.7 with
__future__.annotations
features: