@@ -372,26 +372,17 @@ def __convert_to_ast(self, other):
372372 extra_names = {}
373373 elts = []
374374
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 )
393384 else :
394- # For lists and tuples , preserve the original order
385+ # For lists, tuples, and other sets , preserve the original order.
395386 for elt in other :
396387 new_elt , new_extra_names = self .__convert_to_ast (elt )
397388 if new_extra_names is not None :
@@ -840,7 +831,7 @@ def _stringify_single(anno):
840831 elif isinstance (anno , _Template ):
841832 return ast .unparse (_template_to_ast (anno ))
842833 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.
844835 sorted_elements = sorted (anno , key = lambda x : x .__name__ )
845836 return "{" + ", " .join (x .__name__ for x in sorted_elements ) + "}"
846837 else :
@@ -1053,7 +1044,7 @@ def annotations_to_string(annotations):
10531044 if isinstance (t , str ):
10541045 result [n ] = t
10551046 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.
10571048 sorted_elements = sorted (t , key = lambda x : x .__name__ )
10581049 result [n ] = "{" + ", " .join (x .__name__ for x in sorted_elements ) + "}"
10591050 else :
0 commit comments