This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: `is_dataclass` returns `True` if `getattr` always succeeds.
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.7
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: eric.smith Nosy List: Johan Hidding, eric.smith, miss-islington, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2019-08-15 12:02 by Johan Hidding, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 15325 merged eric.smith, 2019-08-18 02:42
PR 15339 merged miss-islington, 2019-08-20 05:40
PR 15340 merged miss-islington, 2019-08-20 05:40
Messages (7)
msg349802 - (view) Author: Johan Hidding (Johan Hidding) Date: 2019-08-15 12:02
Given a class `A` that overloads `__getattr__`

class A:
  def __getattr__(self, key):
    return 0

An instance of this class is always identified as a dataclass.

from dataclasses import is_dataclass

a = A()

gives the output `True`.

Possible fix: check for the instance type.


does give the correct answer.
msg349821 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2019-08-15 19:48
I'm guessing I'm looking up the attribute on the instance, not the class.
msg349822 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-08-15 20:04
I am not sure that it is good idea to accept a type and an instance, but if it is a goal, is_dataclass() should be defined as:

def is_dataclass(obj):
    cls = obj if isinstance(obj, type) else type(obj)
    return hasattr(cls, _FIELDS)

_is_dataclass_instance() should be changed too:

def _is_dataclass_instance(obj):
    return hasattr(type(obj), _FIELDS)
msg349832 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2019-08-15 21:53
Yeah, I agree it's not an awesome design to work with classes or instances, but it's documented that way. As soon as I write some tests I'll check this in.
msg349989 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2019-08-20 05:40
New changeset b0f4dab8735f692bcfedcf0fa9a25e238a554bab by Eric V. Smith in branch 'master':
bpo-37868: Improve is_dataclass for instances. (GH-15325)
msg349990 - (view) Author: miss-islington (miss-islington) Date: 2019-08-20 05:59
New changeset 1271ee8187df31debda7c556882a51ec356ca534 by Miss Islington (bot) in branch '3.8':
bpo-37868: Improve is_dataclass for instances. (GH-15325)
msg349991 - (view) Author: miss-islington (miss-islington) Date: 2019-08-20 06:01
New changeset 02c1457a036c2af3e91beb952afdb66d9c806435 by Miss Islington (bot) in branch '3.7':
bpo-37868: Improve is_dataclass for instances. (GH-15325)
Date User Action Args
2022-04-11 14:59:19adminsetgithub: 82049
2019-08-20 06:05:07eric.smithsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2019-08-20 06:01:59miss-islingtonsetmessages: + msg349991
2019-08-20 05:59:30miss-islingtonsetnosy: + miss-islington
messages: + msg349990
2019-08-20 05:40:45miss-islingtonsetpull_requests: + pull_request15057
2019-08-20 05:40:39miss-islingtonsetpull_requests: + pull_request15056
2019-08-20 05:40:31eric.smithsetmessages: + msg349989
2019-08-18 02:42:16eric.smithsetkeywords: + patch
stage: patch review
pull_requests: + pull_request15042
2019-08-15 21:53:44eric.smithsetmessages: + msg349832
2019-08-15 20:04:31serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg349822
2019-08-15 19:48:50eric.smithsetmessages: + msg349821
2019-08-15 12:33:32eric.smithsetassignee: eric.smith

nosy: + eric.smith
2019-08-15 12:02:15Johan Hiddingcreate