mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 21:51:50 +00:00 
			
		
		
		
	bpo-33967: Fix singledispatch raised IndexError when no args (GH-8184)
This commit is contained in:
		
							parent
							
								
									7762e4d387
								
							
						
					
					
						commit
						445f1b35ce
					
				
					 3 changed files with 14 additions and 0 deletions
				
			
		|  | @ -817,8 +817,13 @@ def register(cls, func=None): | ||||||
|         return func |         return func | ||||||
| 
 | 
 | ||||||
|     def wrapper(*args, **kw): |     def wrapper(*args, **kw): | ||||||
|  |         if not args: | ||||||
|  |             raise TypeError(f'{funcname} requires at least ' | ||||||
|  |                             '1 positional argument') | ||||||
|  | 
 | ||||||
|         return dispatch(args[0].__class__)(*args, **kw) |         return dispatch(args[0].__class__)(*args, **kw) | ||||||
| 
 | 
 | ||||||
|  |     funcname = getattr(func, '__name__', 'singledispatch function') | ||||||
|     registry[object] = func |     registry[object] = func | ||||||
|     wrapper.register = register |     wrapper.register = register | ||||||
|     wrapper.dispatch = dispatch |     wrapper.dispatch = dispatch | ||||||
|  |  | ||||||
|  | @ -2305,6 +2305,13 @@ def _(arg: typing.Iterable[str]): | ||||||
|         )) |         )) | ||||||
|         self.assertTrue(str(exc.exception).endswith(msg_suffix)) |         self.assertTrue(str(exc.exception).endswith(msg_suffix)) | ||||||
| 
 | 
 | ||||||
|  |     def test_invalid_positional_argument(self): | ||||||
|  |         @functools.singledispatch | ||||||
|  |         def f(*args): | ||||||
|  |             pass | ||||||
|  |         msg = 'f requires at least 1 positional argument' | ||||||
|  |         with self.assertRaisesRegexp(TypeError, msg): | ||||||
|  |             f() | ||||||
| 
 | 
 | ||||||
| if __name__ == '__main__': | if __name__ == '__main__': | ||||||
|     unittest.main() |     unittest.main() | ||||||
|  |  | ||||||
|  | @ -0,0 +1,2 @@ | ||||||
|  | functools.singledispatch now raises TypeError instead of IndexError when no | ||||||
|  | positional arguments are passed. | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Dong-hee Na
						Dong-hee Na