Skip to content
This repository was archived by the owner on Mar 26, 2026. It is now read-only.

Commit 5929070

Browse files
authored
cleanup: sort output in generated __init__.py files better (#846)
1 parent 2fa7940 commit 5929070

6 files changed

Lines changed: 19 additions & 39 deletions

File tree

gapic/ads-templates/%namespace/%name/%version/%sub/__init__.py.j2

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,21 @@
88
them again.
99
-#}
1010
__all__ = (
11-
{% filter sort_lines %}
12-
{% for subpackage in api.subpackages.keys() %}
11+
{% for subpackage in api.subpackages|dictsort %}
1312
'{{ subpackage }}',
1413
{% endfor %}
15-
{% for service in api.services.values()
14+
{% for service in api.services.values()|sort(attribute='client_name')
1615
if service.meta.address.subpackage == api.subpackage_view %}
1716
'{{ service.client_name }}',
1817
{% endfor %}
19-
{% for proto in api.protos.values()
18+
{% for proto in api.protos.values()|sort(attribute='name')
2019
if proto.meta.address.subpackage == api.subpackage_view %}
2120
{% for message in proto.messages.values() %}
2221
'{{ message.name }}',
2322
{% endfor %}
24-
{% for enum in proto.enums.values() %}
23+
{% for enum in proto.enums.values()|sort(attribute='name') %}
2524
'{{ enum.name }}',
2625
{% endfor %}
2726
{% endfor %}
28-
{% endfilter %}
2927
)
3028
{% endblock %}

gapic/ads-templates/%namespace/%name/%version/__init__.py.j2

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ def __dir__():
5050
return globals().get('__all__') or __getattr__('__all__')
5151
{% else %} {# do not use lazy import #}
5252
{# Import subpackages. -#}
53-
{% filter sort_lines %}
54-
{% for subpackage in api.subpackages.keys() %}
53+
{% for subpackage in api.subpackages|dictsort %}
5554
from {% if api.naming.module_namespace %}{{ api.naming.module_namespace|join('.') }}.{% endif %}
5655
{{ api.naming.versioned_module_name }} import {{ subpackage }}
5756
{% endfor %}
@@ -83,15 +82,13 @@ from {% if api.naming.module_namespace %}{{ api.naming.module_namespace|join('.'
8382
from {% if api.naming.module_namespace %}{{ api.naming.module_namespace|join('.') }}.{% endif %}
8483
{{ api.naming.versioned_module_name }}.types.{{ proto.module_name }} import {{ enum.name }}
8584
{% endfor %}{% endfor %}
86-
{% endfilter %}
8785
{# Define __all__.
8886
This requires the full set of imported names, so we iterate over
8987
them again.
9088
-#}
9189
__all__ = (
9290
{% filter indent %}
93-
{% filter sort_lines %}
94-
{% for subpackage in api.subpackages.keys() %}
91+
{% for subpackage in api.subpackages|dictsort %}
9592
'{{ subpackage }}',
9693
{% endfor %}
9794
{% for service in api.services.values()|sort(attribute='name')
@@ -108,7 +105,6 @@ __all__ = (
108105
'{{ enum.name }}',
109106
{% endfor %}{% endfor %}
110107
{% endfilter %}
111-
{% endfilter %}
112108
)
113109
{% endif %} {# lazy import #}
114110
{% endblock %}

gapic/ads-templates/%namespace/%name/__init__.py.j2

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ def __dir__():
4848
return globals().get('__all__') or __getattr__('__all__')
4949
{% else %} {# do not use lazy import #}
5050
{# Import subpackages. -#}
51-
{% filter sort_lines %}
52-
{% for subpackage in api.subpackages.keys() %}
51+
{% for subpackage in api.subpackages|dictsort %}
5352
from {% if api.naming.module_namespace %}{{ api.naming.module_namespace|join('.') }}.{% endif %}
5453
{{- api.naming.versioned_module_name }} import {{ subpackage }}
5554
{% endfor %}
@@ -81,14 +80,12 @@ from {% if api.naming.module_namespace %}{{ api.naming.module_namespace|join('.'
8180
from {% if api.naming.module_namespace %}{{ api.naming.module_namespace|join('.') }}.{% endif %}
8281
{{- api.naming.versioned_module_name }}.types.{{ proto.module_name }} import {{ enum.name }}
8382
{% endfor %}{% endfor %}
84-
{% endfilter %}
8583
{# Define __all__.
8684
This requires the full set of imported names, so we iterate over
8785
them again.
8886
#}
8987
__all__ = (
9088
{% filter indent %}
91-
{% filter sort_lines %}
9289
{% for subpackage, _ in api.subpackages|dictsort %}
9390
'{{ subpackage }}',
9491
{% endfor %}
@@ -106,7 +103,6 @@ __all__ = (
106103
'{{ enum.name }}',
107104
{% endfor %}{% endfor %}
108105
{% endfilter %}
109-
{% endfilter %}
110106
)
111107
{% endif %} {# lazy import #}
112108
{% endblock %}

gapic/schema/metadata.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ class Address:
5050
collisions: FrozenSet[str] = dataclasses.field(default_factory=frozenset)
5151

5252
def __eq__(self, other) -> bool:
53-
return all([getattr(self, i) == getattr(other, i) for i
54-
in ('name', 'module', 'module_path', 'package', 'parent')])
53+
return all(getattr(self, i) == getattr(other, i)
54+
for i in ('name', 'module', 'module_path', 'package', 'parent'))
5555

5656
def __hash__(self):
5757
# Do NOT include collisions; they are not relevant.
@@ -188,7 +188,7 @@ def sphinx(self) -> str:
188188
if self.proto_package.startswith(self.api_naming.proto_package):
189189
return '.'.join(self.api_naming.module_namespace + (
190190
self.api_naming.versioned_module_name,
191-
) + self.subpackage + ('types',) + self.parent + (self.name, ))
191+
) + self.subpackage + ('types',) + self.parent + (self.name, ))
192192

193193
# Anything left is a standard _pb2 type
194194
return f'{self.proto_package}.{self.module}_pb2.{self.name}'

gapic/templates/%namespace/%name/__init__.py.j2

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
{% block content %}
33

44
{# Import subpackages. -#}
5-
{% filter sort_lines %}
6-
{% for subpackage in api.subpackages.keys() %}
5+
{% for subpackage in api.subpackages|dictsort %}
76
from {% if api.naming.module_namespace %}{{ api.naming.module_namespace|join('.') }}.{% endif %}
87
{{- api.naming.versioned_module_name }} import {{ subpackage }}
98
{% endfor %}
@@ -39,15 +38,13 @@ from {% if api.naming.module_namespace %}{{ api.naming.module_namespace|join('.'
3938
from {% if api.naming.module_namespace %}{{ api.naming.module_namespace|join('.') }}.{% endif %}
4039
{{- api.naming.versioned_module_name }}.types.{{ proto.module_name }} import {{ enum.name }}
4140
{% endfor %}{% endfor %}
42-
{% endfilter %}
4341
{# Define __all__.
4442
This requires the full set of imported names, so we iterate over
4543
them again.
4644
#}
4745

4846
__all__ = (
4947
{%- filter indent %}
50-
{% filter sort_lines %}
5148
{% for subpackage, _ in api.subpackages|dictsort %}
5249
'{{ subpackage }}',
5350
{% endfor %}
@@ -68,6 +65,5 @@ __all__ = (
6865
'{{ enum.name }}',
6966
{% endfor %}{% endfor %}
7067
{% endfilter %}
71-
{% endfilter %}
7268
)
7369
{% endblock %}

gapic/templates/%namespace/%name_%version/%sub/__init__.py.j2

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,52 +8,46 @@ from . import {{ subpackage }}
88
{% endfor %}
99

1010
{# Import services for this package. -#}
11-
{% filter sort_lines %}
1211
{% for service in api.services.values()|sort(attribute='name')
1312
if service.meta.address.subpackage == api.subpackage_view %}
1413
from .services.{{ service.name|snake_case }} import {{ service.client_name }}
1514
{% endfor %}
16-
{% endfilter %}
1715

1816
{# Import messages and enums from each proto.
1917
It is safe to import all of the messages into the same namespace here,
2018
because protocol buffers itself enforces selector uniqueness within
2119
a proto package.
2220
-#}
23-
{% filter sort_lines %}
24-
{% for proto in api.protos.values()
21+
{% for proto in api.protos.values()|sort(attribute='name')
2522
if proto.meta.address.subpackage == api.subpackage_view %}
26-
{% for message in proto.messages.values() %}
23+
{% for message in proto.messages.values()|sort(attribute='name') %}
2724
from .types.{{ proto.module_name }} import {{ message.name }}
2825
{% endfor %}
29-
{% for enum in proto.enums.values() %}
26+
{% for enum in proto.enums.values()|sort(attribute='name') %}
3027
from .types.{{ proto.module_name }} import {{ enum.name }}
3128
{% endfor %}
3229
{% endfor %}
33-
{% endfilter %}
3430

3531
{# Define __all__.
3632
This requires the full set of imported names, so we iterate over
3733
them again.
3834
-#}
3935
__all__ = (
40-
{% filter sort_lines %}
41-
{% for subpackage in api.subpackages.keys() %}
36+
{% for subpackage in api.subpackages|dictsort %}
4237
'{{ subpackage }}',
4338
{% endfor %}
44-
{% for service in api.services.values()
39+
{% for service in api.services.values()|sort(attribute='client_name')
4540
if service.meta.address.subpackage == api.subpackage_view %}
4641
'{{ service.client_name }}',
4742
{% endfor %}
48-
{% for proto in api.protos.values()
43+
{% for proto in api.protos.values()|sort(attribute='name')
4944
if proto.meta.address.subpackage == api.subpackage_view %}
50-
{% for message in proto.messages.values() %}
45+
{% for message in proto.messages.values()|sort(attribute='name') %}
5146
'{{ message.name }}',
5247
{% endfor %}
53-
{% for enum in proto.enums.values() %}
48+
{% for enum in proto.enums.values()|sort(attribute='name') %}
5449
'{{ enum.name }}',
5550
{% endfor %}
5651
{% endfor %}
57-
{% endfilter %}
5852
)
5953
{% endblock %}

0 commit comments

Comments
 (0)