@@ -23,20 +23,19 @@ def AugAssign(draw):
2323 op = draw (
2424 sampled_from (
2525 [
26- ast .Add (),
26+ ast .Add (), # Most common arithmetic
2727 ast .Sub (),
2828 ast .Mult (),
2929 ast .Div (),
30+ ast .Mod (), # Common operations
3031 ast .FloorDiv (),
31- ast .Mod (),
32- ast .Pow (),
33- ast .LShift (),
34- ast .RShift (),
32+ ast .Pow (), # Less common
33+ ast .BitAnd (), # Bitwise operations
3534 ast .BitOr (),
3635 ast .BitXor (),
37- ast .BitOr (),
38- ast .BitAnd (),
39- ast .MatMult ()
36+ ast .LShift (),
37+ ast .RShift (),
38+ ast .MatMult () # Least common (matrix mult)
4039 ]
4140 )
4241 )
@@ -51,7 +50,7 @@ def Print(draw):
5150
5251@composite
5352def Raise (draw ):
54- return ast .Raise (draw (none () | expression ()), cause = None )
53+ return ast .Raise (draw (one_of ( none (), expression () )), cause = None )
5554
5655
5756@composite
@@ -81,14 +80,16 @@ def Continue(draw) -> ast.Continue:
8180
8281@composite
8382def With (draw , statements ) -> ast .With :
84- items = draw (lists (expression (), min_size = 1 , max_size = 3 ))
83+ # Tuples cannot be context expressions - they parse as multiple withitems
84+ items = draw (lists (expression ().filter (lambda e : not isinstance (e , ast .Tuple )), min_size = 1 , max_size = 3 ))
8585 body = draw (lists (statements , min_size = 1 , max_size = 3 ))
8686 return ast .With ([ast .withitem (context_expr = i , optional_vars = None ) for i in items ], body )
8787
8888
8989@composite
9090def AsyncWith (draw , statements ) -> ast .AsyncWith :
91- items = draw (lists (expression (), min_size = 1 , max_size = 3 ))
91+ # Tuples cannot be context expressions - they parse as multiple withitems
92+ items = draw (lists (expression ().filter (lambda e : not isinstance (e , ast .Tuple )), min_size = 1 , max_size = 3 ))
9293 body = draw (lists (statements , min_size = 1 , max_size = 3 ))
9394 return ast .AsyncWith ([ast .withitem (context_expr = i , optional_vars = None ) for i in items ], body )
9495
@@ -102,7 +103,7 @@ def If(draw, statements) -> ast.If:
102103
103104@composite
104105def ExceptHandler (draw , statements ) -> ast .ExceptHandler :
105- t = draw (none () | Name ())
106+ t = draw (one_of ( none (), Name () ))
106107
107108 n = None
108109 if t is not None :
@@ -181,7 +182,7 @@ def Nonlocal(draw) -> ast.Nonlocal:
181182
182183@composite
183184def alias (draw ) -> ast .alias :
184- return ast .alias (name = draw (name ()), asname = draw (none () | name ()))
185+ return ast .alias (name = draw (name ()), asname = draw (one_of ( none (), name () )))
185186
186187
187188@composite
@@ -202,7 +203,7 @@ def ImportFrom(draw) -> ast.ImportFrom:
202203def TypeVar (draw ) -> ast .TypeVar :
203204 return ast .TypeVar (
204205 name = draw (name ()),
205- bound = draw (none () | expression ())
206+ bound = draw (one_of ( none (), expression () ))
206207 )
207208
208209
@@ -232,7 +233,7 @@ def FunctionDef(draw, statements) -> ast.FunctionDef:
232233 body = draw (lists (statements , min_size = 1 , max_size = 3 ))
233234 decorator_list = draw (lists (Name (), min_size = 0 , max_size = 2 ))
234235 type_params = draw (lists (one_of (TypeVar (), TypeVarTuple (), ParamSpec ()), min_size = 0 , max_size = 3 ))
235- returns = draw (none () | expression ())
236+ returns = draw (one_of ( none (), expression () ))
236237 return ast .FunctionDef (n , args , body , decorator_list , returns , type_params = type_params )
237238
238239
@@ -243,7 +244,7 @@ def AsyncFunctionDef(draw, statements) -> ast.AsyncFunctionDef:
243244 body = draw (lists (statements , min_size = 1 , max_size = 3 ))
244245 decorator_list = draw (lists (Name (), min_size = 0 , max_size = 2 ))
245246 type_params = draw (lists (one_of (TypeVar (), TypeVarTuple (), ParamSpec ()), min_size = 0 , max_size = 3 ))
246- returns = draw (none () | expression ())
247+ returns = draw (one_of ( none (), expression () ))
247248 return ast .AsyncFunctionDef (n , args , body , decorator_list , returns , type_params = type_params )
248249
249250
@@ -261,7 +262,14 @@ def ClassDef(draw, statements) -> ast.ClassDef:
261262 bases = draw (lists (expression (), min_size = 0 , max_size = 2 ))
262263 keywords = draw (lists (keyword (), min_size = 0 , max_size = 2 ))
263264
264- assume (len ({kw .arg for kw in keywords }) == len (keywords ))
265+ # Remove duplicate keyword names
266+ seen_args = set ()
267+ unique_keywords = []
268+ for kw in keywords :
269+ if kw .arg not in seen_args :
270+ seen_args .add (kw .arg )
271+ unique_keywords .append (kw )
272+ keywords = unique_keywords
265273
266274 body = draw (lists (statements , min_size = 1 , max_size = 3 ))
267275 decorator_list = draw (lists (Name (), min_size = 0 , max_size = 2 ))
@@ -277,39 +285,38 @@ def ClassDef(draw, statements) -> ast.ClassDef:
277285
278286if hasattr (ast , 'Print' ):
279287 simple_statements = one_of (
280- Pass (),
281- Break (),
288+ Pass (), # Simplest - no operation
289+ Break (), # Simple control flow
282290 Continue (),
283- Global (),
291+ Global (), # Simple declarations
284292 Nonlocal (),
285- Expr (),
286- Assert (),
287- Print (),
288- Raise (),
289- # Delete() |
290- Assign (),
291- AnnAssign (),
293+ Expr (), # Expression statement
294+ Assign (), # Simple assignments
292295 AugAssign (),
293- Import (),
296+ AnnAssign (), # Type annotations
297+ Print (), # Python 2 print statement
298+ Assert (), # More complex statements
299+ Raise (),
300+ Import (), # Import statements
294301 ImportFrom ()
302+ # Delete() - commented out
295303 )
296304else :
297305 simple_statements = one_of (
298- Pass (),
299- Break (),
306+ Pass (), # Simplest - no operation
307+ Break (), # Simple control flow
300308 Continue (),
301- Global (),
309+ Global (), # Simple declarations
302310 Nonlocal (),
303- Expr (),
304- Assert (),
305- Raise (),
306- # Delete() |
307- Assign (),
308- AnnAssign (),
311+ Expr (), # Expression statement
312+ Assign (), # Simple assignments
309313 AugAssign (),
310- Import (),
314+ AnnAssign (), # Type annotations
315+ Assert (), # More complex statements
316+ Raise (),
317+ Import (), # Import statements
311318 ImportFrom (),
312- TypeAlias ()
319+ TypeAlias () # Most complex
313320 )
314321
315322
@@ -318,22 +325,22 @@ def suite() -> SearchStrategy:
318325 simple_statements ,
319326 lambda statements :
320327 one_of (
321- With (statements ),
328+ If (statements ), # Simple conditional
329+ While (statements ), # Simple loop
330+ For (statements ), # Loop with iteration
331+ With (statements ), # Context manager
332+ FunctionDef (statements ), # Function definition
333+ AsyncFor (statements ), # Async variants
322334 AsyncWith (statements ),
323- If (statements ),
324- For (statements ),
325- AsyncFor (statements ),
326- While (statements ),
327- FunctionDef (statements ),
328335 AsyncFunctionDef (statements ),
329- ClassDef (statements ),
330- Try (statements )
336+ Try (statements ), # Complex exception handling
337+ ClassDef (statements ) # Most complex
331338 ),
332339 max_leaves = 100
333340 )
334341
335342
336343@composite
337344def Module (draw ) -> ast .Module :
338- b = draw (lists (suite (), min_size = 1 , max_size = 10 ))
345+ b = draw (lists (suite (), min_size = 1 , max_size = 3 ))
339346 return ast .Module (body = b , type_ignores = [])
0 commit comments