From 4531fd0cea0d2d8a0c5b8109f92ff6be886f5692 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Thu, 23 Mar 2023 15:13:05 +0000 Subject: [3.10] gh-102947: Improve traceback when calling `fields()` on a non-dataclass (#102948) (#102954) --- Lib/dataclasses.py | 2 +- Lib/test/test_dataclasses.py | 12 ++++++++++++ .../Library/2023-03-23-13-34-33.gh-issue-102947.cTwcpU.rst | 2 ++ 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2023-03-23-13-34-33.gh-issue-102947.cTwcpU.rst diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py index 9dc67fa..1742900 100644 --- a/Lib/dataclasses.py +++ b/Lib/dataclasses.py @@ -1195,7 +1195,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 # Exclude pseudo-fields. Note that fields is sorted by insertion # order, so the order of the tuple is as the fields were defined. diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py index a642ed9..d6170eb 100644 --- a/Lib/test/test_dataclasses.py +++ b/Lib/test/test_dataclasses.py @@ -5,10 +5,12 @@ from dataclasses import * import abc +import io import pickle import inspect import builtins import types +import traceback import unittest from unittest.mock import Mock from typing import ClassVar, Any, List, Union, Tuple, Dict, Generic, TypeVar, Optional, Protocol @@ -1414,6 +1416,16 @@ class TestCase(unittest.TestCase): with self.assertRaisesRegex(TypeError, 'dataclass type or instance'): fields(C()) + def test_clean_traceback_from_fields_exception(self): + stdout = io.StringIO() + try: + fields(object) + except TypeError as exc: + traceback.print_exception(exc, file=stdout) + printed_traceback = stdout.getvalue() + self.assertNotIn("AttributeError", printed_traceback) + self.assertNotIn("__dataclass_fields__", printed_traceback) + def test_helper_asdict(self): # Basic tests for asdict(), it should return a new dictionary. @dataclass diff --git a/Misc/NEWS.d/next/Library/2023-03-23-13-34-33.gh-issue-102947.cTwcpU.rst b/Misc/NEWS.d/next/Library/2023-03-23-13-34-33.gh-issue-102947.cTwcpU.rst new file mode 100644 index 0000000..b59c982 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-03-23-13-34-33.gh-issue-102947.cTwcpU.rst @@ -0,0 +1,2 @@ +Improve traceback when :func:`dataclasses.fields` is called on a +non-dataclass. Patch by Alex Waygood -- cgit v0.12