[issue39442] from __future__ import annotations makes dataclasses.Field.type a string, not type

ARF1 report at bugs.python.org
Thu Nov 19 04:50:09 EST 2020


ARF1 <arik at funke.eu> added the comment:

Another counter-intuitive behaviour is the different behaviour of dataclasses depending on whether they were defined with the decorator or the make_dataclass factory method:


from __future__ import annotations
import dataclasses

mytype = int

@dataclasses.dataclass
class MyClass1:
    foo: mytype = 1

MyClass2 = dataclasses.make_dataclass(
    f'MyClass2',
    [('foo', mytype, 1)]
)

print(dataclasses.fields(MyClass1)[0].type)
print(dataclasses.fields(MyClass2)[0].type)


Results in:

mytype
<class 'int'>

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39442>
_______________________________________


More information about the Python-bugs-list mailing list