classification
Title: inspect.getmembers does not retrive dataclass's __dataclass_fields__ properly
Type: Stage: resolved
Components: Library (Lib) Versions: Python 3.8, Python 3.7
process
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: corona10, eric.smith, serhiy.storchaka, xtreak, yselivanov
Priority: normal Keywords:

Created on 2018-07-16 04:12 by corona10, last changed 2018-07-16 06:13 by corona10. This issue is now closed.

Files
File name Uploaded Description Edit
inspect_dataclass.py corona10, 2018-07-16 04:12
Messages (3)
msg321706 - (view) Author: Dong-hee Na (corona10) * (Python committer) Date: 2018-07-16 04:12
When I execute this script.

import inspect
from dataclasses import *
import enum

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

print([a[0] for a in inspect.getmembers(SimpleDataObject)])


I expected

['__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']

but got

['__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_b']

If this behavior is not intended, I propose a new patch with providing Inspect.isdataclass for Python 3.8 and Inspect._is_dataclss for Python 3.7

https://github.com/corona10/cpython/commit/c2665176ce836a7b328ddc09c6c7d3de0a2b29a0
msg321707 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2018-07-16 05:53
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
msg321711 - (view) Author: Dong-hee Na (corona10) * (Python committer) Date: 2018-07-16 06:13
@xtreak
Yeah... It looks like intended.
Closing this issue!
History
Date User Action Args
2018-07-16 06:13:42corona10setstatus: open -> closed

messages: + msg321711
stage: resolved
2018-07-16 05:53:53xtreaksetmessages: + msg321707
2018-07-16 05:36:00xtreaksetnosy: + xtreak
2018-07-16 04:12:50corona10create