Skip to content
This repository was archived by the owner on Jun 9, 2025. It is now read-only.

Commit 5c61b5e

Browse files
Fix __all__ definition (#77)
* Fix __all__ definition * Remove comment
1 parent 5664b71 commit 5c61b5e

File tree

4 files changed

+18
-15
lines changed

4 files changed

+18
-15
lines changed

src/betterproto2_compiler/plugin/compiler.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,22 @@ def outputfile_compiler(output_file: OutputTemplate) -> str:
3333
loader=jinja2.FileSystemLoader(templates_folder),
3434
undefined=jinja2.StrictUndefined,
3535
)
36-
# Load the body first so we have a compleate list of imports needed.
36+
37+
# List of the symbols that should appear in the `__all__` variable of the file
38+
all: list[str] = []
39+
40+
def add_to_all(name: str) -> str:
41+
all.append(name)
42+
return name
43+
44+
env.filters["add_to_all"] = add_to_all
45+
3746
body_template = env.get_template("template.py.j2")
3847
header_template = env.get_template("header.py.j2")
3948

49+
# Load the body first do know the symbols defined in the file
4050
code = body_template.render(output_file=output_file)
41-
code = header_template.render(output_file=output_file, version=version) + "\n" + code
51+
code = header_template.render(output_file=output_file, version=version, all=all) + "\n" + code
4252

4353
try:
4454
# Sort imports, delete unused ones

src/betterproto2_compiler/templates/header.py.j2

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,8 @@
66
# This file has been @generated
77

88
__all__ = (
9-
{% for _, enum in output_file.enums|dictsort(by="key") %}
10-
"{{ enum.py_name }}",
11-
{%- endfor -%}
12-
{% for _, message in output_file.messages|dictsort(by="key") %}
13-
"{{ message.py_name }}",
14-
{%- endfor -%}
15-
{% for _, service in output_file.services|dictsort(by="key") %}
16-
"{{ service.py_name }}Stub",
17-
"{{ service.py_name }}Base",
9+
{%- for name in all -%}
10+
"{{ name }}",
1811
{%- endfor -%}
1912
)
2013

src/betterproto2_compiler/templates/service_stub.py.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class {% block class_name %}{% endblock %}({% block inherit_from %}{% endblock %}):
1+
class {% filter add_to_all %}{% block class_name %}{% endblock %}{% endfilter %}({% block inherit_from %}{% endblock %}):
22
{% block service_docstring scoped %}
33
{% if service.comment %}
44
"""

src/betterproto2_compiler/templates/template.py.j2

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{% for _, enum in output_file.enums|dictsort(by="key") %}
2-
class {{ enum.py_name }}(betterproto2.Enum):
2+
class {{ enum.py_name | add_to_all }}(betterproto2.Enum):
33
{% if enum.comment %}
44
"""
55
{{ enum.comment | indent(4) }}
@@ -31,7 +31,7 @@ class {{ enum.py_name }}(betterproto2.Enum):
3131
{% else %}
3232
@dataclass(eq=False, repr=False)
3333
{% endif %}
34-
class {{ message.py_name }}(betterproto2.Message):
34+
class {{ message.py_name | add_to_all }}(betterproto2.Message):
3535
{% if message.comment or message.oneofs %}
3636
"""
3737
{{ message.comment | indent(4) }}
@@ -104,7 +104,7 @@ default_message_pool.register_message("{{ output_file.package }}", "{{ message.p
104104

105105
{% if output_file.settings.server_generation == "async" %}
106106
{% for _, service in output_file.services|dictsort(by="key") %}
107-
class {{ service.py_name }}Base(ServiceBase):
107+
class {{ (service.py_name + "Base") | add_to_all }}(ServiceBase):
108108
{% if service.comment %}
109109
"""
110110
{{ service.comment | indent(4) }}

0 commit comments

Comments
 (0)