[issue34122] inspect.getmembers does not retrive dataclass's __dataclass_fields__ properly

Karthikeyan Singaravelan report at bugs.python.org
Mon Jul 16 01:53:53 EDT 2018


Karthikeyan Singaravelan <tir.karthi at gmail.com> added the comment:

I think there is some note about this on the doc as below : 

https://docs.python.org/3/library/dataclasses.html#dataclasses.field . Relevant commit : 98d50cb8f57eb227c373cb94b8680b12ec8aade5

If the default value of a field is specified by a call to field(), then the class attribute for this field will be replaced by the specified default value. If no default is provided, then the class attribute will be deleted. The intent is that after the dataclass() decorator runs, the class attributes will all contain the default values for the fields, just as if the default value itself were specified.

Using a default value returns the parameter in the inspect module as below : 


➜  cpython git:(master) ✗ cat foo.py
import inspect
from dataclasses import *
import enum

@dataclass
class SimpleDataObject(object):
    field_a: int = field(default=10)
    field_b: str = "asdad"

print([a[0] for a in inspect.getmembers(SimpleDataObject)])
➜  cpython git:(master) ✗ ./python foo.py
['__annotations__', '__class__', '__dataclass_fields__', '__dataclass_params__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'field_a', 'field_b']


I think this is an intended behavior as above and a test case could be added or docs could be improved about this with the inspect example?

Thanks

----------

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


More information about the Python-bugs-list mailing list