summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_dataclasses.py
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2023-03-23 14:45:59 (GMT)
committerGitHub <noreply@github.com>2023-03-23 14:45:59 (GMT)
commit6ffeeb2199176b8433c1b7b46e669132e44d394d (patch)
tree5e1860659a81b86473436861a067d81d5de40218 /Lib/test/test_dataclasses.py
parentf79cfb662589a0c1a5fdb35a5ed429c07c93ebd6 (diff)
downloadcpython-6ffeeb2199176b8433c1b7b46e669132e44d394d.zip
cpython-6ffeeb2199176b8433c1b7b46e669132e44d394d.tar.gz
cpython-6ffeeb2199176b8433c1b7b46e669132e44d394d.tar.bz2
gh-102947: Improve traceback when calling `fields()` on a non-dataclass (GH-102948)
(cherry picked from commit baf4eb083c09b323cc12b8636c28c14089b87de8) Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Diffstat (limited to 'Lib/test/test_dataclasses.py')
-rw-r--r--Lib/test/test_dataclasses.py12
1 files changed, 12 insertions, 0 deletions
diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py
index 3a24777..abe02e3 100644
--- a/Lib/test/test_dataclasses.py
+++ b/Lib/test/test_dataclasses.py
@@ -5,11 +5,13 @@
from dataclasses import *
import abc
+import io
import pickle
import inspect
import builtins
import types
import weakref
+import traceback
import unittest
from unittest.mock import Mock
from typing import ClassVar, Any, List, Union, Tuple, Dict, Generic, TypeVar, Optional, Protocol
@@ -1500,6 +1502,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