-
Notifications
You must be signed in to change notification settings - Fork 9
Switch from mypy to ty #465
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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, | ||
|
|
@@ -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): | ||
|
|
@@ -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): | ||
| 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: | ||
|
|
@@ -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) | ||
|
|
||
|
|
@@ -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 | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unused, but There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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")) | ||
|
|
||
|
|
@@ -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( | ||
|
|
@@ -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, | ||
|
|
@@ -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): | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| 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 | ||
|
|
||
|
|
||
|
|
@@ -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): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tywants names to match up in overrides; done several places here.