mirror of
				https://github.com/python/cpython.git
				synced 2025-11-03 23:21:29 +00:00 
			
		
		
		
	gh-102947: Improve traceback when calling fields() on a non-dataclass (#102948)
				
					
				
			This commit is contained in:
		
							parent
							
								
									08254be6c5
								
							
						
					
					
						commit
						baf4eb083c
					
				
					 3 changed files with 15 additions and 1 deletions
				
			
		| 
						 | 
					@ -1248,7 +1248,7 @@ def fields(class_or_instance):
 | 
				
			||||||
    try:
 | 
					    try:
 | 
				
			||||||
        fields = getattr(class_or_instance, _FIELDS)
 | 
					        fields = getattr(class_or_instance, _FIELDS)
 | 
				
			||||||
    except AttributeError:
 | 
					    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
 | 
					    # Exclude pseudo-fields.  Note that fields is sorted by insertion
 | 
				
			||||||
    # order, so the order of the tuple is as the fields were defined.
 | 
					    # order, so the order of the tuple is as the fields were defined.
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -5,11 +5,13 @@
 | 
				
			||||||
from dataclasses import *
 | 
					from dataclasses import *
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import abc
 | 
					import abc
 | 
				
			||||||
 | 
					import io
 | 
				
			||||||
import pickle
 | 
					import pickle
 | 
				
			||||||
import inspect
 | 
					import inspect
 | 
				
			||||||
import builtins
 | 
					import builtins
 | 
				
			||||||
import types
 | 
					import types
 | 
				
			||||||
import weakref
 | 
					import weakref
 | 
				
			||||||
 | 
					import traceback
 | 
				
			||||||
import unittest
 | 
					import unittest
 | 
				
			||||||
from unittest.mock import Mock
 | 
					from unittest.mock import Mock
 | 
				
			||||||
from typing import ClassVar, Any, List, Union, Tuple, Dict, Generic, TypeVar, Optional, Protocol, DefaultDict
 | 
					from typing import ClassVar, Any, List, Union, Tuple, Dict, Generic, TypeVar, Optional, Protocol, DefaultDict
 | 
				
			||||||
| 
						 | 
					@ -1526,6 +1528,16 @@ class C: pass
 | 
				
			||||||
        with self.assertRaisesRegex(TypeError, 'dataclass type or instance'):
 | 
					        with self.assertRaisesRegex(TypeError, 'dataclass type or instance'):
 | 
				
			||||||
            fields(C())
 | 
					            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):
 | 
					    def test_helper_asdict(self):
 | 
				
			||||||
        # Basic tests for asdict(), it should return a new dictionary.
 | 
					        # Basic tests for asdict(), it should return a new dictionary.
 | 
				
			||||||
        @dataclass
 | 
					        @dataclass
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,2 @@
 | 
				
			||||||
 | 
					Improve traceback when :func:`dataclasses.fields` is called on a
 | 
				
			||||||
 | 
					non-dataclass. Patch by Alex Waygood
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue