mirror of
				https://github.com/python/cpython.git
				synced 2025-10-30 21:21:22 +00:00 
			
		
		
		
	bpo-39728: Enum: fix duplicate ValueError (GH-22277)
				
					
				
			fix default `_missing_` to return `None` instead of raising a `ValueError` Co-authored-by: Andrey Darascheka <andrei.daraschenka@leverx.com>
This commit is contained in:
		
							parent
							
								
									83f6dcd207
								
							
						
					
					
						commit
						c95ad7a91f
					
				
					 4 changed files with 21 additions and 2 deletions
				
			
		|  | @ -629,7 +629,7 @@ def _generate_next_value_(name, start, count, last_values): | ||||||
| 
 | 
 | ||||||
|     @classmethod |     @classmethod | ||||||
|     def _missing_(cls, value): |     def _missing_(cls, value): | ||||||
|         raise ValueError("%r is not a valid %s" % (value, cls.__qualname__)) |         return None | ||||||
| 
 | 
 | ||||||
|     def __repr__(self): |     def __repr__(self): | ||||||
|         return "<%s.%s: %r>" % ( |         return "<%s.%s: %r>" % ( | ||||||
|  |  | ||||||
|  | @ -1845,6 +1845,18 @@ class Dupes(Enum): | ||||||
|             third = auto() |             third = auto() | ||||||
|         self.assertEqual([Dupes.first, Dupes.second, Dupes.third], list(Dupes)) |         self.assertEqual([Dupes.first, Dupes.second, Dupes.third], list(Dupes)) | ||||||
| 
 | 
 | ||||||
|  |     def test_default_missing(self): | ||||||
|  |         class Color(Enum): | ||||||
|  |             RED = 1 | ||||||
|  |             GREEN = 2 | ||||||
|  |             BLUE = 3 | ||||||
|  |         try: | ||||||
|  |             Color(7) | ||||||
|  |         except ValueError as exc: | ||||||
|  |             self.assertTrue(exc.__context__ is None) | ||||||
|  |         else: | ||||||
|  |             raise Exception('Exception not raised.') | ||||||
|  | 
 | ||||||
|     def test_missing(self): |     def test_missing(self): | ||||||
|         class Color(Enum): |         class Color(Enum): | ||||||
|             red = 1 |             red = 1 | ||||||
|  | @ -1863,7 +1875,12 @@ def _missing_(cls, item): | ||||||
|                     # trigger not found |                     # trigger not found | ||||||
|                     return None |                     return None | ||||||
|         self.assertIs(Color('three'), Color.blue) |         self.assertIs(Color('three'), Color.blue) | ||||||
|         self.assertRaises(ValueError, Color, 7) |         try: | ||||||
|  |             Color(7) | ||||||
|  |         except ValueError as exc: | ||||||
|  |             self.assertTrue(exc.__context__ is None) | ||||||
|  |         else: | ||||||
|  |             raise Exception('Exception not raised.') | ||||||
|         try: |         try: | ||||||
|             Color('bad return') |             Color('bad return') | ||||||
|         except TypeError as exc: |         except TypeError as exc: | ||||||
|  |  | ||||||
|  | @ -433,6 +433,7 @@ Marcos Donolo | ||||||
| Dima Dorfman | Dima Dorfman | ||||||
| Yves Dorfsman | Yves Dorfsman | ||||||
| Michael Dorman | Michael Dorman | ||||||
|  | Andrey Doroschenko | ||||||
| Steve Dower | Steve Dower | ||||||
| Allen Downey | Allen Downey | ||||||
| Cesar Douady | Cesar Douady | ||||||
|  |  | ||||||
|  | @ -0,0 +1 @@ | ||||||
|  | fix default `_missing_` so a duplicate `ValueError` is not set as the `__context__` of the original `ValueError` | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Ethan Furman
						Ethan Furman