1616from typing import Tuple
1717
1818
19- @dataclasses .dataclass (frozen = True , order = True )
19+ @dataclasses .dataclass (frozen = True )
2020class Import :
2121 package : Tuple [str , ...]
2222 module : str
2323 alias : str = ""
2424
25+ def __lt__ (self , other ) -> bool :
26+ return str (self ) < str (other )
27+
2528 def __eq__ (self , other ) -> bool :
2629 return self .package == other .package and self .module == other .module
2730
@@ -30,19 +33,19 @@ def __str__(self) -> str:
3033 # We do this for protobuf generated files (_pb2) and api_core
3134 # internals where type information might be missing or incomplete.
3235 needs_type_ignore = self .module .endswith ("_pb2" ) or "api_core" in self .package
33-
34- if needs_type_ignore :
36+ if not needs_type_ignore :
37+ # Standard import generation
38+ answer = f"import { self .module } "
39+ if self .package :
40+ answer = f"from { '.' .join (self .package )} { answer } "
41+ if self .alias :
42+ answer += f" as { self .alias } "
43+ return answer
44+ else :
3545 # Use 'import absolute.path as module' syntax to prevent Ruff/isort
3646 # from combining this with other imports. This ensures the
3747 # '# type: ignore' comment remains effective for this specific import.
3848 full_module = "." .join (self .package + (self .module ,))
3949 alias = self .alias or self .module
4050 return f"import { full_module } as { alias } # type: ignore"
4151
42- # Standard import generation
43- answer = f"import { self .module } "
44- if self .package :
45- answer = f"from { '.' .join (self .package )} { answer } "
46- if self .alias :
47- answer += f" as { self .alias } "
48- return answer
0 commit comments