mirror of
https://github.com/python/cpython.git
synced 2025-07-19 01:05:26 +00:00
gh-102947: Improve traceback when calling fields()
on a non-dataclass (GH-102948)
(cherry picked from commit baf4eb083c
)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
parent
f79cfb6625
commit
6ffeeb2199
3 changed files with 15 additions and 1 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue