Skip to content

Commit 77346d2

Browse files
Switch from mypy to ty for type checking
1 parent dc97693 commit 77346d2

File tree

5 files changed

+49
-155
lines changed

5 files changed

+49
-155
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ conformance: $(BIN)/protovalidate-conformance generate install ## Run conformanc
7676
lint: install $(BIN)/buf ## Lint code
7777
buf format -d --exit-code
7878
uv run -- ruff format --check --diff protovalidate test
79-
uv run -- mypy protovalidate
79+
uv run -- ty check protovalidate
8080
uv run -- ruff check protovalidate test
8181
uv lock --check
8282

protovalidate/internal/cel_field_presence.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@
1313
# limitations under the License.
1414

1515
import threading
16+
from typing import TYPE_CHECKING
1617

1718
import celpy
1819

20+
if TYPE_CHECKING:
21+
import celpy.celtypes
22+
1923
_has_state = threading.local()
2024

2125

@@ -32,7 +36,7 @@ def in_has() -> bool:
3236

3337

3438
class InterpretedRunner(celpy.InterpretedRunner):
35-
def evaluate(self, context):
39+
def evaluate(self, context: celpy.Context) -> celpy.celtypes.Value:
3640
class Evaluator(celpy.Evaluator):
3741
def macro_has_eval(self, exprlist):
3842
_has_state.in_has = True

protovalidate/internal/rules.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,14 @@ def __init__(self, msg: message.Message):
7474
continue
7575
self[field.name] = field_to_cel(self.msg, field)
7676

77-
def __getitem__(self, name):
78-
field = self.desc.fields_by_name[name]
79-
if field.has_presence and not self.msg.HasField(name):
77+
def __getitem__(self, key):
78+
field = self.desc.fields_by_name[key]
79+
if field.has_presence and not self.msg.HasField(key):
8080
if in_has():
8181
raise KeyError()
8282
else:
8383
return _zero_value(field)
84-
return super().__getitem__(name)
84+
return super().__getitem__(key)
8585

8686

8787
def _msg_to_cel(msg: message.Message) -> celtypes.Value:
@@ -303,7 +303,7 @@ def sub_context(self) -> "RuleContext":
303303
class Rules:
304304
"""The rules associated with a single 'rules' message."""
305305

306-
def validate(self, ctx: RuleContext, _: message.Message):
306+
def validate(self, ctx: RuleContext, message: message.Message): # noqa: ARG002
307307
"""Validate the message against the rules in this rule."""
308308
ctx.add(Violation(rule_id="unimplemented", message="Unimplemented"))
309309

@@ -415,8 +415,8 @@ def __init__(self, fields: list[descriptor.FieldDescriptor], *, required: bool):
415415
self._fields = fields
416416
self._required = required
417417

418-
def validate(self, ctx: RuleContext, msg: message.Message):
419-
num_set_fields = sum(1 for field in self._fields if not _is_empty_field(msg, field))
418+
def validate(self, ctx: RuleContext, message: message.Message):
419+
num_set_fields = sum(1 for field in self._fields if not _is_empty_field(message, field))
420420
if num_set_fields > 1:
421421
ctx.add(
422422
Violation(
@@ -561,8 +561,8 @@ def __init__(
561561
# For each set field in the message, look for the private rule
562562
# extension.
563563
for list_field, _ in rules.ListFields():
564-
if validate_pb2.predefined in list_field.GetOptions().Extensions: # type: ignore
565-
for cel in list_field.GetOptions().Extensions[validate_pb2.predefined].cel: # type: ignore
564+
if validate_pb2.predefined in list_field.GetOptions().Extensions:
565+
for cel in list_field.GetOptions().Extensions[validate_pb2.predefined].cel:
566566
self.add_rule(
567567
env,
568568
funcs,
@@ -617,11 +617,13 @@ def validate(self, ctx: RuleContext, message: message.Message):
617617
sub_ctx.add_field_path_element(element)
618618
ctx.add_errors(sub_ctx)
619619

620-
def validate_item(self, ctx: RuleContext, val: typing.Any, *, for_key: bool = False):
621-
self._validate_value(ctx, val, for_key=for_key)
622-
self._validate_cel(ctx, this_value=val, this_cel=_scalar_field_value_to_cel(val, self._field), for_key=for_key)
620+
def validate_item(self, ctx: RuleContext, value: typing.Any, *, for_key: bool = False):
621+
self._validate_value(ctx, value, for_key=for_key)
622+
self._validate_cel(
623+
ctx, this_value=value, this_cel=_scalar_field_value_to_cel(value, self._field), for_key=for_key
624+
)
623625

624-
def _validate_value(self, ctx: RuleContext, val: typing.Any, *, for_key: bool = False):
626+
def _validate_value(self, ctx: RuleContext, value: typing.Any, *, for_key: bool = False):
625627
pass
626628

627629

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ Issues = "https://github.com/bufbuild/protovalidate-python/issues"
4343
[dependency-groups]
4444
dev = [
4545
"google-re2-stubs>=0.1.1",
46-
"mypy>=1.17.1",
4746
"pytest>=8.4.1",
4847
"ruff>=0.12.0",
48+
"ty>=0.0.12",
4949
"types-protobuf>=5.29.1.20250315",
5050
]
5151

0 commit comments

Comments
 (0)