Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Bad traceback whendataclasses.fields is called on a non-dataclass #102947

Closed
Assignees
AlexWaygood
Labels
3.10only security fixes3.11only security fixes3.12only security fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error
@AlexWaygood

Description

@AlexWaygood

Currently, if you calldataclasses.fields on a non-dataclass, quite a poor traceback is produced:

>>>import dataclasses>>> dataclasses.fields(object)Traceback (most recent call last):  File "C:\Users\alexw\AppData\Local\Programs\Python\Python311\Lib\dataclasses.py", line 1232, in fields    fields = getattr(class_or_instance, _FIELDS)             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^AttributeError: type object 'object' has no attribute '__dataclass_fields__'During handling of the above exception, another exception occurred:Traceback (most recent call last):  File "<stdin>", line 1, in <module>  File "C:\Users\alexw\AppData\Local\Programs\Python\Python311\Lib\dataclasses.py", line 1234, in fields    raise TypeError('must be called with a dataclass type or instance')TypeError: must be called with a dataclass type or instance

There are two issues here:

  • The traceback mentions the__dataclass_fields__ attribute, which is an internal implementation detail of thedataclasses module and should be hidden from the user.
  • The "during handling of the above exception, another exception occurred" message implies to the user that there's a bug in thedataclasses module itself, rather than this being intentional behaviour.

This one-line change todataclasses.py produces a much better traceback:

diff --git a/Lib/dataclasses.py b/Lib/dataclasses.pyindex 82b08fc017..e3fd0b3e38 100644--- a/Lib/dataclasses.py+++ b/Lib/dataclasses.py@@ -1248,7 +1248,7 @@ def fields(class_or_instance):     try:         fields = getattr(class_or_instance, _FIELDS)     except AttributeError:-        raise TypeError('must be called with a dataclass type or instance')+        raise TypeError('must be called with a dataclass type or instance') from None

With this change applied, we get this traceback instead:

>>>import dataclasses>>> dataclasses.fields(object)Traceback (most recent call last):  File "<stdin>", line 1, in <module>  File "C:\Users\alexw\coding\cpython\Lib\dataclasses.py", line 1251, in fields    raise TypeError('must be called with a dataclass type or instance') from NoneTypeError: must be called with a dataclass type or instance

Cc.@ericvsmith and@carljm, asdataclasses experts.

Linked PRs

Metadata

Metadata

Assignees

Labels

3.10only security fixes3.11only security fixes3.12only security fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions


    [8]ページ先頭

    ©2009-2026 Movatter.jp