You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
from __future__ import annotations
from dataclasses import dataclass
import dataconf
@dataclass
class Test:
my_var: str
conf = """
my_var = test
"""
print(dataconf.string(conf, Test))
Getting this error:
File "...\.venv\Lib\site-packages\dataconf\main.py", line 108, in string
return multi.string(s, **kwargs).on(clazz)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "...\.venv\Lib\site-packages\dataconf\main.py", line 93, in on
return parse(conf, clazz, self.strict, **self.kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "...\.venv\Lib\site-packages\dataconf\main.py", line 26, in parse
return utils.__parse(conf, clazz, "", strict, ignore_unexpected)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "...\.venv\Lib\site-packages\dataconf\utils.py", line 91, in __parse
fs[f.name] = __parse(
^^^^^^^^
File "...\.venv\Lib\site-packages\dataconf\utils.py", line 269, in __parse
subtype = value.pop("_type", default=None)
^^^^^^^^^
AttributeError: 'str' object has no attribute 'pop'
This line does not pass because when from __future__ import annotations, dataclass.fields returns the type as a string whose value is "str" rather then the class str. There are some more details about this here: https://stackoverflow.com/a/55938344
According to the above StackOverflow, the way to fix it is to not rely on the type from the field, but get it from typing.get_type_hints:
resolved = typing.get_type_hints(C)
f = dataclasses.fields(C)[0]
ftype = resolved[f.name]
The text was updated successfully, but these errors were encountered:
Hi,
The following example will not work
Getting this error:
This line does not pass because when
from __future__ import annotations
, dataclass.fields returns the type as a string whose value is "str" rather then the classstr
. There are some more details about this here: https://stackoverflow.com/a/55938344According to the above StackOverflow, the way to fix it is to not rely on the type from the field, but get it from
typing.get_type_hints
:The text was updated successfully, but these errors were encountered: