Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ conformance: $(BIN)/protovalidate-conformance generate install ## Run conformanc
lint: install $(BIN)/buf ## Lint code
buf format -d --exit-code
uv run -- ruff format --check --diff protovalidate test
uv run -- mypy protovalidate
uv run -- ty check protovalidate
uv run -- ruff check protovalidate test
uv run -- tombi format --check
uv run -- tombi lint
Expand Down
2 changes: 1 addition & 1 deletion protovalidate/internal/cel_field_presence.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def in_has() -> bool:


class InterpretedRunner(celpy.InterpretedRunner):
def evaluate(self, context) -> celpy.celtypes.Value:
def evaluate(self, context: celpy.Context) -> celpy.celtypes.Value:
class Evaluator(celpy.Evaluator):
def macro_has_eval(self, exprlist) -> celpy.celtypes.BoolType:
_has_state.in_has = True
Expand Down
45 changes: 24 additions & 21 deletions protovalidate/internal/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import celpy
from celpy import celtypes
from google.protobuf import ( # type: ignore[attr-defined]
from google.protobuf import (
any_pb2,
descriptor,
duration_pb2,
Expand All @@ -33,15 +33,16 @@
from protovalidate.internal.cel_field_presence import InterpretedRunner, in_has

# protobuf 7+ removed FieldDescriptor.label / LABEL_REPEATED in favour of is_repeated.
if hasattr(descriptor.FieldDescriptor, "is_repeated"):
_FieldDescriptorClass = descriptor.FieldDescriptor
if hasattr(_FieldDescriptorClass, "is_repeated"):

def _is_repeated(field: descriptor.FieldDescriptor) -> bool:
return field.is_repeated # type: ignore[attr-defined]
return field.is_repeated

else:

def _is_repeated(field: descriptor.FieldDescriptor) -> bool:
return field.label == descriptor.FieldDescriptor.LABEL_REPEATED # type: ignore[attr-defined]
return field.label == descriptor.FieldDescriptor.LABEL_REPEATED


class CompilationError(Exception):
Expand Down Expand Up @@ -93,14 +94,14 @@ def __init__(self, msg: message.Message):
continue
self[field.name] = field_to_cel(self.msg, field)

def __getitem__(self, name):
field = self.desc.fields_by_name[name]
if field.has_presence and not self.msg.HasField(name):
def __getitem__(self, key):
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ty wants names to match up in overrides; done several places here.

field = self.desc.fields_by_name[key]
if field.has_presence and not self.msg.HasField(key):
if in_has():
raise KeyError()
else:
return _zero_value(field)
return super().__getitem__(name)
return super().__getitem__(key)


def _msg_to_cel(msg: message.Message) -> celtypes.Value:
Expand Down Expand Up @@ -153,14 +154,14 @@ def _get_type_ctor(fd: typing.Any) -> typing.Callable[..., celtypes.Value] | Non

def _proto_message_has_field(msg: message.Message, field: descriptor.FieldDescriptor) -> typing.Any:
if field.is_extension:
return msg.HasExtension(field) # type: ignore
return msg.HasExtension(field) # ty: ignore[invalid-argument-type]
else:
return msg.HasField(field.name)


def _proto_message_get_field(msg: message.Message, field: descriptor.FieldDescriptor) -> typing.Any:
if field.is_extension:
return msg.Extensions[field] # type: ignore
return msg.Extensions[field] # ty: ignore[invalid-argument-type]
else:
return getattr(msg, field.name)

Expand Down Expand Up @@ -322,7 +323,7 @@ def sub_context(self) -> "RuleContext":
class Rules:
"""The rules associated with a single 'rules' message."""

def validate(self, ctx: RuleContext, _: message.Message):
def validate(self, ctx: RuleContext, message: message.Message): # noqa: ARG002
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused, but ty wants the name to match up. I think we could make this behave just fine with a larger refactor to make one of these an ABC, but probably not worth pursuing here?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree it seems like it should be an ABC but can be separate

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"""Validate the message against the rules in this rule."""
ctx.add(Violation(rule_id="unimplemented", message="Unimplemented"))

Expand Down Expand Up @@ -440,8 +441,8 @@ def __init__(self, fields: list[descriptor.FieldDescriptor], *, required: bool):
self._fields = fields
self._required = required

def validate(self, ctx: RuleContext, msg: message.Message):
num_set_fields = sum(1 for field in self._fields if not _is_empty_field(msg, field))
def validate(self, ctx: RuleContext, message: message.Message):
num_set_fields = sum(1 for field in self._fields if not _is_empty_field(message, field))
if num_set_fields > 1:
ctx.add(
Violation(
Expand Down Expand Up @@ -586,8 +587,8 @@ def __init__(
# For each set field in the message, look for the private rule
# extension.
for list_field, _ in rules.ListFields():
if validate_pb2.predefined in list_field.GetOptions().Extensions: # type: ignore
for cel in list_field.GetOptions().Extensions[validate_pb2.predefined].cel: # type: ignore
if validate_pb2.predefined in list_field.GetOptions().Extensions: # ty: ignore[unsupported-operator]
for cel in list_field.GetOptions().Extensions[validate_pb2.predefined].cel: # ty: ignore[invalid-argument-type]
self.add_rule(
env,
funcs,
Expand Down Expand Up @@ -642,11 +643,13 @@ def validate(self, ctx: RuleContext, message: message.Message):
sub_ctx.add_field_path_element(element)
ctx.add_errors(sub_ctx)

def validate_item(self, ctx: RuleContext, val: typing.Any, *, for_key: bool = False):
self._validate_value(ctx, val, for_key=for_key)
self._validate_cel(ctx, this_value=val, this_cel=_scalar_field_value_to_cel(val, self._field), for_key=for_key)
def validate_item(self, ctx: RuleContext, value: typing.Any, *, for_key: bool = False):
Copy link
Copy Markdown
Contributor Author

@stefanvanburen stefanvanburen May 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

val -> value (hard to spot at first)

self._validate_value(ctx, value, for_key=for_key)
self._validate_cel(
ctx, this_value=value, this_cel=_scalar_field_value_to_cel(value, self._field), for_key=for_key
)

def _validate_value(self, ctx: RuleContext, val: typing.Any, *, for_key: bool = False):
def _validate_value(self, ctx: RuleContext, value: typing.Any, *, for_key: bool = False):
pass


Expand Down Expand Up @@ -1079,8 +1082,8 @@ def _new_rules(self, desc: descriptor.Descriptor) -> list[Rules]:
result: list[Rules] = []
rule: Rules | None = None
all_msg_oneof_fields = set()
if desc.GetOptions().HasExtension(validate_pb2.message): # type: ignore
message_level = desc.GetOptions().Extensions[validate_pb2.message] # type: ignore
if desc.GetOptions().HasExtension(validate_pb2.message): # ty: ignore[invalid-argument-type]
message_level = desc.GetOptions().Extensions[validate_pb2.message] # ty: ignore[invalid-argument-type]
for oneof in message_level.oneof:
all_msg_oneof_fields.update(oneof.fields)
if rule := self._new_message_rule(message_level, desc):
Expand Down
4 changes: 2 additions & 2 deletions protovalidate/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ def collect_violations(
break
for violation in ctx.violations:
if violation.proto.HasField("field"):
violation.proto.field.elements.reverse() # type: ignore
violation.proto.field.elements.reverse()
if violation.proto.HasField("rule"):
violation.proto.rule.elements.reverse() # type: ignore
violation.proto.rule.elements.reverse()
return ctx.violations


Expand Down
5 changes: 1 addition & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ Source = "https://github.com/bufbuild/protovalidate-python"
[dependency-groups]
dev = [
"google-re2-stubs==0.1.1",
"mypy==1.20.2",
"pytest==9.0.3",
"ruff==0.15.12",
"tombi==0.10.5",
"ty==0.0.34",
"types-protobuf==6.32.1.20260221",
]

Expand All @@ -59,9 +59,6 @@ build-backend = "hatchling.build"
source = "vcs"
raw-options = { fallback_version = "0.0.0" }

[tool.mypy]
mypy_path = "gen"

[tool.pytest]
# Turn all warnings into errors,
# except DeprecationWarnings (which we knowingly tolerate due to using old `protobuf` APIs).
Expand Down
Loading
Loading