[New-bugs-announce] [issue39442] from __future__ import annotations breaks dataclasses.Field.type
Wojciech Łopata
report at bugs.python.org
Fri Jan 24 06:48:40 EST 2020
New submission from Wojciech Łopata <wlopata at gmail.com>:
I've checked this behaviour under Python 3.7.5 and 3.8.1.
```
from __future__ import annotations
from dataclasses import dataclass, fields
@dataclass
class Foo:
x: int
print(fields(Foo)[0].type)
```
With annotations imported, the `type` field of Field class becomes a string with a name of a type, and the program outputs 'int'.
Without annotations, the `type` field of Field class is a type, and the program outputs <class 'int'>.
I found this out when using dataclasses_serialization module. Following code works fine when we remove import of annotations:
```
from __future__ import annotations
from dataclasses import dataclass
from dataclasses_serialization.json import JSONSerializer
@dataclass
class Foo:
x: int
JSONSerializer.deserialize(Foo, {'x': 42})
```
TypeError: issubclass() arg 1 must be a class
----------
components: Library (Lib)
messages: 360611
nosy: lopek
priority: normal
severity: normal
status: open
title: from __future__ import annotations breaks dataclasses.Field.type
type: behavior
versions: Python 3.7, Python 3.8
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39442>
_______________________________________
More information about the New-bugs-announce
mailing list