@@ -372,26 +372,17 @@ def __convert_to_ast(self, other):
372
372
extra_names = {}
373
373
elts = []
374
374
375
- # For sets, we need to ensure consistent ordering
376
- if type (other ) is set :
377
- # For sets of types, sort by __name__
378
- if all (isinstance (x , type ) for x in other ):
379
- # Sort the elements by name
380
- sorted_elts = sorted (other , key = lambda x : x .__name__ )
381
- for elt in sorted_elts :
382
- new_elt , new_extra_names = self .__convert_to_ast (elt )
383
- if new_extra_names is not None :
384
- extra_names .update (new_extra_names )
385
- elts .append (new_elt )
386
- else :
387
- # For other sets, just use the original order
388
- for elt in other :
389
- new_elt , new_extra_names = self .__convert_to_ast (elt )
390
- if new_extra_names is not None :
391
- extra_names .update (new_extra_names )
392
- elts .append (new_elt )
375
+ # For sets of types, sort elements by name for consistent ordering.
376
+ if type (other ) is set and all (isinstance (x , type ) for x in other ):
377
+ # Sort the elements by name to ensure deterministic output.
378
+ sorted_elts = sorted (other , key = lambda x : x .__name__ )
379
+ for elt in sorted_elts :
380
+ new_elt , new_extra_names = self .__convert_to_ast (elt )
381
+ if new_extra_names is not None :
382
+ extra_names .update (new_extra_names )
383
+ elts .append (new_elt )
393
384
else :
394
- # For lists and tuples , preserve the original order
385
+ # For lists, tuples, and other sets , preserve the original order.
395
386
for elt in other :
396
387
new_elt , new_extra_names = self .__convert_to_ast (elt )
397
388
if new_extra_names is not None :
@@ -840,7 +831,7 @@ def _stringify_single(anno):
840
831
elif isinstance (anno , _Template ):
841
832
return ast .unparse (_template_to_ast (anno ))
842
833
elif isinstance (anno , set ) and all (isinstance (x , type ) for x in anno ):
843
- # Sort set elements by name to ensure consistent ordering
834
+ # Sort set elements by name to ensure consistent ordering.
844
835
sorted_elements = sorted (anno , key = lambda x : x .__name__ )
845
836
return "{" + ", " .join (x .__name__ for x in sorted_elements ) + "}"
846
837
else :
@@ -1053,7 +1044,7 @@ def annotations_to_string(annotations):
1053
1044
if isinstance (t , str ):
1054
1045
result [n ] = t
1055
1046
elif isinstance (t , set ) and all (isinstance (x , type ) for x in t ):
1056
- # Sort set elements by name to ensure consistent ordering
1047
+ # Sort set elements by name to ensure consistent ordering.
1057
1048
sorted_elements = sorted (t , key = lambda x : x .__name__ )
1058
1049
result [n ] = "{" + ", " .join (x .__name__ for x in sorted_elements ) + "}"
1059
1050
else :
0 commit comments