betterproto2-compiler 0.5.1__py3-none-any.whl → 0.6.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -80,7 +80,7 @@ import dateutil.parser
80
80
 
81
81
  from ...message_pool import default_message_pool
82
82
 
83
- betterproto2.check_compiler_version("0.5.0")
83
+ betterproto2.check_compiler_version("0.6.0")
84
84
 
85
85
 
86
86
  class FieldCardinality(betterproto2.Enum):
@@ -17,7 +17,7 @@ import betterproto2
17
17
 
18
18
  from ....message_pool import default_message_pool
19
19
 
20
- betterproto2.check_compiler_version("0.5.0")
20
+ betterproto2.check_compiler_version("0.6.0")
21
21
 
22
22
 
23
23
  class CodeGeneratorResponseFeature(betterproto2.Enum):
@@ -33,17 +33,27 @@ def outputfile_compiler(output_file: OutputTemplate) -> str:
33
33
  loader=jinja2.FileSystemLoader(templates_folder),
34
34
  undefined=jinja2.StrictUndefined,
35
35
  )
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
+
37
46
  body_template = env.get_template("template.py.j2")
38
47
  header_template = env.get_template("header.py.j2")
39
48
 
49
+ # Load the body first do know the symbols defined in the file
40
50
  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
42
52
 
43
53
  try:
44
- # Sort imports, delete unused ones
54
+ # Sort imports, delete unused ones, sort __all__
45
55
  code = subprocess.check_output(
46
- ["ruff", "check", "--select", "I,F401,TCH005", "--fix", "--silent", "-"],
56
+ ["ruff", "check", "--select", "I,F401,TC005,RUF022", "--fix", "--silent", "-"],
47
57
  input=code,
48
58
  encoding="utf-8",
49
59
  )
@@ -353,8 +353,7 @@ class FieldCompiler(ProtoContentBase):
353
353
 
354
354
  @property
355
355
  def field_type(self) -> FieldType:
356
- # TODO it should be possible to remove constructor
357
- return FieldType(self.proto_obj.type)
356
+ return self.proto_obj.type
358
357
 
359
358
  @property
360
359
  def packed(self) -> bool:
@@ -411,12 +410,46 @@ class FieldCompiler(ProtoContentBase):
411
410
  def unwrapped_py_type(self) -> str:
412
411
  return self._py_type(wrap=False)
413
412
 
413
+ @property
414
+ def annotations(self) -> list[str]:
415
+ """List of the Pydantic annotation to add to the field."""
416
+ assert self.output_file.settings.pydantic_dataclasses
417
+
418
+ annotations = []
419
+
420
+ if self.proto_obj.type in (FieldType.TYPE_INT32, FieldType.TYPE_SFIXED32, FieldType.TYPE_SINT32):
421
+ annotations.append("pydantic.Field(ge=-2**31, le=2**31 - 1)")
422
+
423
+ elif self.proto_obj.type in (FieldType.TYPE_UINT32, FieldType.TYPE_FIXED32):
424
+ annotations.append("pydantic.Field(ge=0, le=2**32 - 1)")
425
+
426
+ elif self.proto_obj.type in (FieldType.TYPE_INT64, FieldType.TYPE_SFIXED64, FieldType.TYPE_SINT64):
427
+ annotations.append("pydantic.Field(ge=-2**63, le=2**63 - 1)")
428
+
429
+ elif self.proto_obj.type in (FieldType.TYPE_UINT64, FieldType.TYPE_FIXED64):
430
+ annotations.append("pydantic.Field(ge=0, le=2**64 - 1)")
431
+
432
+ elif self.proto_obj.type == FieldType.TYPE_FLOAT:
433
+ annotations.append("pydantic.AfterValidator(betterproto2.validators.validate_float32)")
434
+
435
+ elif self.proto_obj.type == FieldType.TYPE_STRING:
436
+ annotations.append("pydantic.AfterValidator(betterproto2.validators.validate_string)")
437
+
438
+ return annotations
439
+
414
440
  @property
415
441
  def annotation(self) -> str:
416
442
  py_type = self.py_type
417
443
 
418
444
  if self.use_builtins:
419
445
  py_type = f"builtins.{py_type}"
446
+
447
+ # Add the pydantic annotation if needed
448
+ if self.output_file.settings.pydantic_dataclasses:
449
+ annotations = self.annotations
450
+ if annotations:
451
+ py_type = f"typing.Annotated[{py_type}, {', '.join(annotations)}]"
452
+
420
453
  if self.repeated:
421
454
  return f"list[{py_type}]"
422
455
  if self.optional:
@@ -6,15 +6,8 @@
6
6
  # This file has been @generated
7
7
 
8
8
  __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 }}",
18
11
  {%- endfor -%}
19
12
  )
20
13
 
@@ -28,6 +21,7 @@ import typing
28
21
  from typing import TYPE_CHECKING
29
22
 
30
23
  {% if output_file.settings.pydantic_dataclasses %}
24
+ import pydantic
31
25
  from pydantic.dataclasses import dataclass
32
26
  from pydantic import model_validator
33
27
  {%- else -%}
@@ -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 %}):
2
2
  {% block service_docstring scoped %}
3
3
  {% if service.comment %}
4
4
  """
@@ -1,5 +1,5 @@
1
1
  {% 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):
3
3
  {% if enum.comment %}
4
4
  """
5
5
  {{ enum.comment | indent(4) }}
@@ -31,7 +31,7 @@ class {{ enum.py_name }}(betterproto2.Enum):
31
31
  {% else %}
32
32
  @dataclass(eq=False, repr=False)
33
33
  {% endif %}
34
- class {{ message.py_name }}(betterproto2.Message):
34
+ class {{ message.py_name | add_to_all }}(betterproto2.Message):
35
35
  {% if message.comment or message.oneofs %}
36
36
  """
37
37
  {{ message.comment | indent(4) }}
@@ -104,7 +104,7 @@ default_message_pool.register_message("{{ output_file.package }}", "{{ message.p
104
104
 
105
105
  {% if output_file.settings.server_generation == "async" %}
106
106
  {% 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):
108
108
  {% if service.comment %}
109
109
  """
110
110
  {{ service.comment | indent(4) }}
@@ -0,0 +1,15 @@
1
+ Metadata-Version: 2.4
2
+ Name: betterproto2_compiler
3
+ Version: 0.6.0
4
+ Summary: Compiler for betterproto2
5
+ Project-URL: Documentation, https://betterproto.github.io/python-betterproto2/
6
+ Project-URL: Repository, https://github.com/betterproto/python-betterproto2
7
+ Author-email: Adrien Vannson <adrien.vannson@protonmail.com>, "Daniel G. Taylor" <danielgtaylor@gmail.com>
8
+ License-Expression: MIT
9
+ Keywords: compiler,gRPC,protobuf
10
+ Requires-Python: <4.0,>=3.10
11
+ Requires-Dist: betterproto2[grpclib]<0.7,>=0.6.0
12
+ Requires-Dist: jinja2>=3.0.3
13
+ Requires-Dist: ruff~=0.9.3
14
+ Requires-Dist: strenum<0.5,>=0.4.15; python_version == '3.10'
15
+ Requires-Dist: typing-extensions<5,>=4.7.1
@@ -1,5 +1,7 @@
1
1
  betterproto2_compiler/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
2
  betterproto2_compiler/casing.py,sha256=HSXLXAOqZzEnu-tC1SZjpW0LIjzdPqUNJEwy1BHzfgg,3056
3
+ betterproto2_compiler/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
+ betterproto2_compiler/settings.py,sha256=ggLU8qYTTACwdNN94cSznQchu-fJSJxZz041FXRwq7Y,1976
3
5
  betterproto2_compiler/compile/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
6
  betterproto2_compiler/compile/importing.py,sha256=cJZ1BqjSHaDwXlfseH7f73pMnBZTiqLyOQfQfyganVM,6256
5
7
  betterproto2_compiler/compile/naming.py,sha256=zf0VOmNojzyv33upOGelGxjZTEDE8JULEEED5_3inHg,562
@@ -9,27 +11,24 @@ betterproto2_compiler/known_types/duration.py,sha256=M-qsFeiHsw5Z_AoSata1ZUjfkho
9
11
  betterproto2_compiler/known_types/google_values.py,sha256=7JoPXVs6cVg_ihIWlWIDElSSuW0BymRnPHerz1bFuH4,6688
10
12
  betterproto2_compiler/known_types/timestamp.py,sha256=y1sNWG2Q0FWv6nIte1UTifFVCsryp7T8foXZqp4qhQQ,3409
11
13
  betterproto2_compiler/lib/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
12
- betterproto2_compiler/lib/google/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
13
- betterproto2_compiler/lib/google/protobuf/__init__.py,sha256=yC7k_A1XA9-CPzhIaJfWJSIBpqyaLx7IVVuvXrqD-iQ,102114
14
- betterproto2_compiler/lib/google/protobuf/compiler/__init__.py,sha256=IriT5naeEkcxA-R2EpzOGBMLVGgVO6CXqvrR8HVaR28,9600
15
14
  betterproto2_compiler/lib/message_pool.py,sha256=4-cRhhiM6bmfpUJZ8qxc8LEyqHBHpLCcotjbyZxl7JM,71
15
+ betterproto2_compiler/lib/google/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
16
+ betterproto2_compiler/lib/google/protobuf/__init__.py,sha256=UWGp10_l07eCd2AW1qVVJHrfWf7DiQHQqWoBLOxbDfI,102114
17
+ betterproto2_compiler/lib/google/protobuf/compiler/__init__.py,sha256=NSY2v-qU_CIqGHtXv02fXs1D6K_bPWJtr5Gf9byPFpA,9600
16
18
  betterproto2_compiler/plugin/__init__.py,sha256=L3pW0b4CvkM5x53x_sYt1kYiSFPO0_vaeH6EQPq9FAM,43
17
19
  betterproto2_compiler/plugin/__main__.py,sha256=vBQ82334kX06ImDbFlPFgiBRiLIinwNk3z8Khs6hd74,31
18
- betterproto2_compiler/plugin/compiler.py,sha256=3sPbCtdzjAGftVvoGe5dvxE8uTzJ78hABA-_f-1rEUo,2471
20
+ betterproto2_compiler/plugin/compiler.py,sha256=9jZcNlwxWLUQlZyCLKG33P2xCoJgqaIQHIgcZM40JGY,2730
19
21
  betterproto2_compiler/plugin/main.py,sha256=b1jDEdG1Iau-4cPq89uSjU0SHwC278SxqwiuFwIF8fA,1288
20
- betterproto2_compiler/plugin/models.py,sha256=rzCKsdoV-0QCwmRN82x2QZ6029W9EAAJCFbLGMkVoz8,22215
22
+ betterproto2_compiler/plugin/models.py,sha256=2JZWVgzemhgoyZdeArLEh7fBdxBLpqQOvUXlx8cRD1c,23638
21
23
  betterproto2_compiler/plugin/module_validation.py,sha256=JnP8dSN83eJJVDP_UPJsHzq7E7Md3lah0PnKXDbFW5Q,4808
22
24
  betterproto2_compiler/plugin/parser.py,sha256=XJd7n78E6Gnv04L1faGyTCOV3v0sv1eD5-AenNhPDMQ,10197
23
25
  betterproto2_compiler/plugin/plugin.bat,sha256=lfLT1WguAXqyerLLsRL6BfHA0RqUE6QG79v-1BYVSpI,48
24
- betterproto2_compiler/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
25
- betterproto2_compiler/settings.py,sha256=ggLU8qYTTACwdNN94cSznQchu-fJSJxZz041FXRwq7Y,1976
26
- betterproto2_compiler/templates/header.py.j2,sha256=nTUJ-BioeTTCrEr2ZbxPPUl6iBqHOxXr_NAVOGa8jYg,1622
27
- betterproto2_compiler/templates/service_stub.py.j2,sha256=r0AefgNbDCh-iDgFNV7aNx8fNe5kQY-8TNew-T_tUXc,929
26
+ betterproto2_compiler/templates/header.py.j2,sha256=9W34FKHVKItXQzIpiWQQbjBLP1PzGdJOdnef1IlJGa4,1311
27
+ betterproto2_compiler/templates/service_stub.py.j2,sha256=2fhbty6uw57EyxOskGcNlZjIjGELMKWY--pvq5ZEjFw,967
28
28
  betterproto2_compiler/templates/service_stub_async.py.j2,sha256=JNOAa8FPhzYS5D0zi0DPESVEwAjkdFsVQZ008Qi4JmE,2968
29
29
  betterproto2_compiler/templates/service_stub_sync.py.j2,sha256=V7HJIQJEgivX8VEBt7Ju6cXG5FeeCY9QyYMy1kicElM,2642
30
- betterproto2_compiler/templates/template.py.j2,sha256=kP1TMTK7BCv6sEFTGXPZs_jg39lC_IdvKTFWPHm8BgU,5415
31
- betterproto2_compiler-0.5.1.dist-info/LICENSE.md,sha256=Pgl2pReU-2yw2miGeQ55UFlyzqAZ_EpYVyZ2nWjwRv4,1121
32
- betterproto2_compiler-0.5.1.dist-info/METADATA,sha256=7wZ7cc4n5R4S9lnl2jBYY89Hib9WynJySv0zFk_3zjM,1225
33
- betterproto2_compiler-0.5.1.dist-info/WHEEL,sha256=fGIA9gx4Qxk2KDKeNJCbOEwSrmLtjWCwzBz351GyrPQ,88
34
- betterproto2_compiler-0.5.1.dist-info/entry_points.txt,sha256=re3Qg8lLljbVobeeKH2f1FVQZ114wfZkGv3zCZTD8Ok,84
35
- betterproto2_compiler-0.5.1.dist-info/RECORD,,
30
+ betterproto2_compiler/templates/template.py.j2,sha256=0Cwq8b3cW6mnMjo6vGO03fz4SoGM7GXvVh6TzGB4Nk4,5461
31
+ betterproto2_compiler-0.6.0.dist-info/METADATA,sha256=DSjcX8CLq6uxzwDY_DQYH6jnhgJc42CN-tSyuhn51sY,658
32
+ betterproto2_compiler-0.6.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
33
+ betterproto2_compiler-0.6.0.dist-info/entry_points.txt,sha256=MXDaz7YfiaWx8KiSzArjUPLt6eTlMRbqzE4jCjXozuI,85
34
+ betterproto2_compiler-0.6.0.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: poetry-core 2.1.2
2
+ Generator: hatchling 1.27.0
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ protoc-gen-python_betterproto2 = betterproto2_compiler.plugin:main
@@ -1,22 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2023 Daniel G. Taylor
4
- Copyright (c) 2024 The betterproto contributors
5
-
6
- Permission is hereby granted, free of charge, to any person obtaining a copy
7
- of this software and associated documentation files (the "Software"), to deal
8
- in the Software without restriction, including without limitation the rights
9
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
- copies of the Software, and to permit persons to whom the Software is
11
- furnished to do so, subject to the following conditions:
12
-
13
- The above copyright notice and this permission notice shall be included in all
14
- copies or substantial portions of the Software.
15
-
16
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
- SOFTWARE.
@@ -1,36 +0,0 @@
1
- Metadata-Version: 2.3
2
- Name: betterproto2_compiler
3
- Version: 0.5.1
4
- Summary: Compiler for betterproto2
5
- License: MIT
6
- Keywords: protobuf,gRPC,compiler
7
- Author: Adrien Vannson
8
- Author-email: adrien.vannson@protonmail.com
9
- Requires-Python: >=3.10,<4.0
10
- Classifier: License :: OSI Approved :: MIT License
11
- Classifier: Programming Language :: Python :: 3
12
- Classifier: Programming Language :: Python :: 3.10
13
- Classifier: Programming Language :: Python :: 3.11
14
- Classifier: Programming Language :: Python :: 3.12
15
- Classifier: Programming Language :: Python :: 3.13
16
- Requires-Dist: betterproto2[grpclib] (>=0.5.1,<0.6.0)
17
- Requires-Dist: jinja2 (>=3.0.3)
18
- Requires-Dist: ruff (>=0.9.3,<0.10.0)
19
- Requires-Dist: strenum (>=0.4.15,<0.5.0) ; python_version == "3.10"
20
- Requires-Dist: typing-extensions (>=4.7.1,<5.0.0)
21
- Project-URL: Documentation, https://betterproto.github.io/python-betterproto2-compiler/
22
- Project-URL: Repository, https://github.com/betterproto/python-betterproto2-compiler
23
- Description-Content-Type: text/markdown
24
-
25
- # Betterproto2 compiler
26
-
27
- ![](https://github.com/betterproto/python-betterproto2-compiler/actions/workflows/ci.yml/badge.svg)
28
-
29
-
30
-
31
- ## License
32
-
33
- Copyright © 2019 Daniel G. Taylor
34
-
35
- Copyright © 2024 The betterproto contributors
36
-
@@ -1,3 +0,0 @@
1
- [console_scripts]
2
- protoc-gen-python_betterproto2=betterproto2_compiler.plugin:main
3
-