mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 13:41:24 +00:00 
			
		
		
		
	[3.9] bpo-40334: Produce better error messages on invalid targets (GH-20106) (GH-20973)
* bpo-40334: Produce better error messages on invalid targets (GH-20106)
The following error messages get produced:
- `cannot delete ...` for invalid `del` targets
- `... is an illegal 'for' target` for invalid targets in for
  statements
- `... is an illegal 'with' target` for invalid targets in
  with statements
Additionally, a few `cut`s were added in various places before the
invocation of the `invalid_*` rule, in order to speed things
up.
Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
(cherry picked from commit 01ece63d42)
			
			
This commit is contained in:
		
							parent
							
								
									b1e7361134
								
							
						
					
					
						commit
						a5442b26f4
					
				
					 6 changed files with 2649 additions and 2258 deletions
				
			
		|  | @ -160,6 +160,61 @@ | |||
| Traceback (most recent call last): | ||||
| SyntaxError: 'list' is an illegal expression for augmented assignment | ||||
| 
 | ||||
| Invalid targets in `for` loops and `with` statements should also | ||||
| produce a specialized error message | ||||
| 
 | ||||
| >>> for a() in b: pass | ||||
| Traceback (most recent call last): | ||||
| SyntaxError: cannot assign to function call | ||||
| 
 | ||||
| >>> for (a, b()) in b: pass | ||||
| Traceback (most recent call last): | ||||
| SyntaxError: cannot assign to function call | ||||
| 
 | ||||
| >>> for [a, b()] in b: pass | ||||
| Traceback (most recent call last): | ||||
| SyntaxError: cannot assign to function call | ||||
| 
 | ||||
| >>> for (*a, b, c+1) in b: pass | ||||
| Traceback (most recent call last): | ||||
| SyntaxError: cannot assign to operator | ||||
| 
 | ||||
| >>> for (x, *(y, z.d())) in b: pass | ||||
| Traceback (most recent call last): | ||||
| SyntaxError: cannot assign to function call | ||||
| 
 | ||||
| >>> for a, b() in c: pass | ||||
| Traceback (most recent call last): | ||||
| SyntaxError: cannot assign to function call | ||||
| 
 | ||||
| >>> for i < (): pass | ||||
| Traceback (most recent call last): | ||||
| SyntaxError: invalid syntax | ||||
| 
 | ||||
| >>> with a as b(): pass | ||||
| Traceback (most recent call last): | ||||
| SyntaxError: cannot assign to function call | ||||
| 
 | ||||
| >>> with a as (b, c()): pass | ||||
| Traceback (most recent call last): | ||||
| SyntaxError: cannot assign to function call | ||||
| 
 | ||||
| >>> with a as [b, c()]: pass | ||||
| Traceback (most recent call last): | ||||
| SyntaxError: cannot assign to function call | ||||
| 
 | ||||
| >>> with a as (*b, c, d+1): pass | ||||
| Traceback (most recent call last): | ||||
| SyntaxError: cannot assign to operator | ||||
| 
 | ||||
| >>> with a as (x, *(y, z.d())): pass | ||||
| Traceback (most recent call last): | ||||
| SyntaxError: cannot assign to function call | ||||
| 
 | ||||
| >>> with a as b, c as d(): pass | ||||
| Traceback (most recent call last): | ||||
| SyntaxError: cannot assign to function call | ||||
| 
 | ||||
| >>> p = p = | ||||
| Traceback (most recent call last): | ||||
| SyntaxError: invalid syntax | ||||
|  | @ -736,7 +791,7 @@ def test_assign_del(self): | |||
|         self._check_error("del (1, 2)", "delete literal") | ||||
|         self._check_error("del None", "delete None") | ||||
|         self._check_error("del *x", "delete starred") | ||||
|         self._check_error("del (*x)", "delete starred") | ||||
|         self._check_error("del (*x)", "use starred expression") | ||||
|         self._check_error("del (*x,)", "delete starred") | ||||
|         self._check_error("del [*x,]", "delete starred") | ||||
|         self._check_error("del f()", "delete function call") | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Lysandros Nikolaou
						Lysandros Nikolaou