mirror of
				https://github.com/python/cpython.git
				synced 2025-10-30 21:21:22 +00:00 
			
		
		
		
	bpo-45662: Fix the repr of InitVar with a type alias to the built-in class (GH-29291)
For example, InitVar[list[int]].
This commit is contained in:
		
							parent
							
								
									60c320c38e
								
							
						
					
					
						commit
						1fd4de5bdd
					
				
					 3 changed files with 7 additions and 1 deletions
				
			
		|  | @ -229,7 +229,7 @@ def __init__(self, type): | ||||||
|         self.type = type |         self.type = type | ||||||
| 
 | 
 | ||||||
|     def __repr__(self): |     def __repr__(self): | ||||||
|         if isinstance(self.type, type): |         if isinstance(self.type, type) and not isinstance(self.type, GenericAlias): | ||||||
|             type_name = self.type.__name__ |             type_name = self.type.__name__ | ||||||
|         else: |         else: | ||||||
|             # typing objects, e.g. List[int] |             # typing objects, e.g. List[int] | ||||||
|  |  | ||||||
|  | @ -1126,6 +1126,10 @@ def test_init_var_preserve_type(self): | ||||||
|         self.assertEqual(repr(InitVar[int]), 'dataclasses.InitVar[int]') |         self.assertEqual(repr(InitVar[int]), 'dataclasses.InitVar[int]') | ||||||
|         self.assertEqual(repr(InitVar[List[int]]), |         self.assertEqual(repr(InitVar[List[int]]), | ||||||
|                          'dataclasses.InitVar[typing.List[int]]') |                          'dataclasses.InitVar[typing.List[int]]') | ||||||
|  |         self.assertEqual(repr(InitVar[list[int]]), | ||||||
|  |                          'dataclasses.InitVar[list[int]]') | ||||||
|  |         self.assertEqual(repr(InitVar[int|str]), | ||||||
|  |                          'dataclasses.InitVar[int | str]') | ||||||
| 
 | 
 | ||||||
|     def test_init_var_inheritance(self): |     def test_init_var_inheritance(self): | ||||||
|         # Note that this deliberately tests that a dataclass need not |         # Note that this deliberately tests that a dataclass need not | ||||||
|  |  | ||||||
|  | @ -0,0 +1,2 @@ | ||||||
|  | Fix the repr of :data:`dataclasses.InitVar` with a type alias to the | ||||||
|  | built-in class, e.g. ``InitVar[list[int]]``. | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Serhiy Storchaka
						Serhiy Storchaka