summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_dataclasses.py
diff options
context:
space:
mode:
authorAlex Waygood <Alex.Waygood@Gmail.com>2023-03-23 15:13:05 (GMT)
committerGitHub <noreply@github.com>2023-03-23 15:13:05 (GMT)
commit4531fd0cea0d2d8a0c5b8109f92ff6be886f5692 (patch)
treeeb640f3457959839dfde3c371b07b2d20e6bb7e4 /Lib/test/test_dataclasses.py
parent100da7c31aeb3888962bf33c8cc3594272964815 (diff)
downloadcpython-4531fd0cea0d2d8a0c5b8109f92ff6be886f5692.zip
cpython-4531fd0cea0d2d8a0c5b8109f92ff6be886f5692.tar.gz
cpython-4531fd0cea0d2d8a0c5b8109f92ff6be886f5692.tar.bz2
[3.10] gh-102947: Improve traceback when calling `fields()` on a non-dataclass (#102948) (#102954)
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 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