New submission from Misha Drachuk <mi...@drach.uk>:

Forward reference is not resolved by `dataclasses.fields()`, but it works with 
`typing.get_type_hints()`.

E.g. 

from dataclasses import dataclass, fields
from typing import Optional, get_type_hints

@dataclass
class Nestable:
    child: Optional['Nestable']

o = Nestable(None)
print('fields:', fields(o))
print('type hints:', get_type_hints(Nestable))

... outputs the following:
 
fields: (Field(name='child',type=typing.Union[ForwardRef('Nestable'), NoneType] 
... )
type hints: {'child': typing.Union[__main__.Nestable, NoneType]}

----------
components: Library (Lib)
messages: 340361
nosy: mdrachuk
priority: normal
severity: normal
status: open
title: Forward reference is not resolved by dataclasses.fields()
type: behavior
versions: Python 3.7

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue36643>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to