Skip to content

Commit 66757dc

Browse files
authored
Modernize patterns following protobuf-py conventions (#495)
1 parent 9179f7f commit 66757dc

27 files changed

Lines changed: 1262 additions & 603 deletions

protovalidate/__init__.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,28 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from protovalidate import validator
15+
"""The semantic validation library for Protobuf in Python."""
1616

17-
Validator = validator.Validator
18-
CompilationError = validator.CompilationError
19-
ValidationError = validator.ValidationError
20-
Violations = validator.Violations
17+
from __future__ import annotations
18+
19+
from protovalidate._validator import (
20+
CompilationError,
21+
ValidationError,
22+
Validator,
23+
Violation,
24+
Violations,
25+
)
2126

2227
_default_validator = Validator()
2328
validate = _default_validator.validate
2429
collect_violations = _default_validator.collect_violations
2530

26-
__all__ = ["CompilationError", "ValidationError", "Validator", "Violations", "collect_violations", "validate"]
31+
__all__ = [
32+
"CompilationError",
33+
"ValidationError",
34+
"Validator",
35+
"Violation",
36+
"Violations",
37+
"collect_violations",
38+
"validate",
39+
]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
"""Which CEL backend is available."""
1616

17+
from __future__ import annotations
18+
1719

1820
def _detect() -> bool:
1921
try:

protovalidate/internal/cel_field_presence.py renamed to protovalidate/_cel_field_presence.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,22 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from __future__ import annotations
16+
1517
import threading
18+
from typing import TYPE_CHECKING
1619

1720
import celpy
1821
import celpy.celtypes
1922

23+
if TYPE_CHECKING:
24+
from lark import Tree
25+
2026
_has_state = threading.local()
2127

2228

2329
def in_has() -> bool:
24-
"""
25-
Returns true if inside of CEL interpreter `has` macro.
30+
"""Returns true if inside of CEL interpreter `has` macro.
2631
2732
This enables working around an issue in cel-python where it is not possible
2833
to implement protobuf semantics around the `has` macro.
@@ -35,12 +40,11 @@ def in_has() -> bool:
3540
class InterpretedRunner(celpy.InterpretedRunner):
3641
def evaluate(self, context: celpy.Context) -> celpy.celtypes.Value:
3742
class Evaluator(celpy.Evaluator):
38-
def macro_has_eval(self, exprlist) -> celpy.celtypes.BoolType:
43+
def macro_has_eval(self, exprlist: Tree) -> celpy.celtypes.BoolType:
3944
_has_state.in_has = True
4045
result = super().macro_has_eval(exprlist)
4146
_has_state.in_has = False
4247
return result
4348

4449
e = Evaluator(ast=self.ast, activation=self.new_activation())
45-
value = e.evaluate(context)
46-
return value
50+
return e.evaluate(context)

protovalidate/internal/celexpr/__init__.py renamed to protovalidate/_celexpr/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
"""The cel-expr-python (cel-cpp) validation engine."""
1616

17+
from __future__ import annotations
18+
1719
from .bridge import GoogleBridge
1820
from .extra_func import make_extension
1921
from .rules import RuleFactory
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
from __future__ import annotations
15+
1416
from google.protobuf import descriptor as google_descriptor
1517

1618
# protobuf 7+ removed FieldDescriptor.label / LABEL_REPEATED in favour of is_repeated.
Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,32 @@
1616

1717
from __future__ import annotations
1818

19-
import typing
19+
from typing import TYPE_CHECKING
2020

21-
from google.protobuf import descriptor_pb2 as google_descriptor_pb2
22-
from google.protobuf import descriptor_pool as google_descriptor_pool
23-
from google.protobuf import message as google_message
24-
from google.protobuf import message_factory as google_message_factory
25-
from protobuf import DescFile, DescMessage
21+
from google.protobuf import (
22+
descriptor_pb2 as google_descriptor_pb2,
23+
descriptor_pool as google_descriptor_pool,
24+
message as google_message,
25+
message_factory as google_message_factory,
26+
)
2627

27-
if typing.TYPE_CHECKING:
28+
if TYPE_CHECKING:
2829
import protobuf
30+
from protobuf import DescFile, DescMessage
2931

3032

3133
class GoogleBridge:
32-
"""Lazily mirrors protobuf-py descriptors into google's pool and bridges
33-
protobuf-py message values to google dynamic messages."""
34+
"""Bridges from protobuf-py messages to google.protobuf for use with cel-expr-python."""
3435

3536
def __init__(self) -> None:
36-
self._pool: google_descriptor_pool.DescriptorPool = google_descriptor_pool.Default()
37+
self._pool: google_descriptor_pool.DescriptorPool = (
38+
google_descriptor_pool.Default()
39+
)
3740
self._mirrored: set[str] = set()
3841
self._classes: dict[str, type[google_message.Message]] = {}
3942

4043
def _mirror_file(self, desc_file: DescFile) -> None:
41-
"""Registers a protobuf-py DescFile (and its transitive deps) into the
42-
google pool, dependencies first, skipping files already present."""
44+
"""Registers a protobuf-py DescFile and deps into the google pool."""
4345
if desc_file.name in self._mirrored:
4446
return
4547
self._mirrored.add(desc_file.name)
@@ -48,7 +50,9 @@ def _mirror_file(self, desc_file: DescFile) -> None:
4850
try:
4951
self._pool.FindFileByName(desc_file.name)
5052
except KeyError:
51-
proto = google_descriptor_pb2.FileDescriptorProto.FromString(desc_file.proto.to_binary())
53+
proto = google_descriptor_pb2.FileDescriptorProto.FromString(
54+
desc_file.proto.to_binary()
55+
)
5256
self._pool.Add(proto)
5357

5458
def google_class(self, desc: DescMessage) -> type[google_message.Message]:

0 commit comments

Comments
 (0)