Skip to content
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

Upgrade cel-python to 0.2.0 #252

Merged
merged 14 commits into from
Feb 25, 2025
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 Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ verify_ssl = true
name = "pypi"

[packages]
cel-python = "==0.1.5"
cel-python = "==0.2.*"
protobuf = "==5.*"

[dev-packages]
Expand Down
164 changes: 21 additions & 143 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions protovalidate/internal/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ def make_duration(msg: message.Message) -> celtypes.DurationType:


def make_timestamp(msg: message.Message) -> celtypes.TimestampType:
return make_duration(msg) + celtypes.TimestampType(1970, 1, 1)
return celtypes.TimestampType(1970, 1, 1) + make_duration(msg)


def unwrap(msg: message.Message) -> celtypes.Value:
return _field_to_cel(msg, msg.DESCRIPTOR.fields_by_name["value"])


_MSG_TYPE_URL_TO_CTOR = {
_MSG_TYPE_URL_TO_CTOR: dict[str, typing.Callable[..., celtypes.Value]] = {
"google.protobuf.Duration": make_duration,
"google.protobuf.Timestamp": make_timestamp,
"google.protobuf.StringValue": unwrap,
Expand Down Expand Up @@ -89,7 +89,7 @@ def _msg_to_cel(msg: message.Message) -> celtypes.Value:
return MessageType(msg)


_TYPE_TO_CTOR = {
_TYPE_TO_CTOR: dict[str, typing.Callable[..., celtypes.Value]] = {
descriptor.FieldDescriptor.TYPE_MESSAGE: _msg_to_cel,
descriptor.FieldDescriptor.TYPE_GROUP: _msg_to_cel,
descriptor.FieldDescriptor.TYPE_ENUM: celtypes.IntType,
Expand Down
14 changes: 8 additions & 6 deletions protovalidate/internal/extra_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,11 @@ def is_ip_prefix(val: celtypes.Value, *args) -> celpy.Result:
raise celpy.CELEvalError(msg)
try:
if version is None:
ip_network(val, strict=strict)
ip_network(val, strict=bool(strict))
elif version == 4:
IPv4Network(val, strict=strict)
IPv4Network(val, strict=bool(strict))
elif version == 6:
IPv6Network(val, strict=strict)
IPv6Network(val, strict=bool(strict))
else:
msg = "invalid argument, expected 4 or 6"
raise celpy.CELEvalError(msg)
Expand All @@ -164,7 +164,7 @@ def is_email(string: celtypes.Value) -> celpy.Result:


def is_uri(string: celtypes.Value) -> celpy.Result:
url = urlparse.urlparse(string)
url = urlparse.urlparse(str(string))
# urlparse correctly reads the scheme from URNs but parses everything
# after (except the query string) as the path.
if url.scheme == "urn":
Expand All @@ -182,7 +182,7 @@ def is_uri(string: celtypes.Value) -> celpy.Result:


def is_uri_ref(string: celtypes.Value) -> celpy.Result:
url = urlparse.urlparse(string)
url = urlparse.urlparse(str(string))
if not all([url.scheme, url.path]) and url.fragment:
return celtypes.BoolType(False)
return celtypes.BoolType(True)
Expand Down Expand Up @@ -238,7 +238,9 @@ def unique(val: celtypes.Value) -> celpy.Result:


def make_extra_funcs(locale: str) -> dict[str, celpy.CELFunction]:
string_fmt = string_format.StringFormat(locale)
# TODO(#257): Fix types and add tests for StringFormat.
# For now, ignoring the type.
string_fmt = string_format.StringFormat(locale) # type: ignore
return {
# Missing standard functions
"format": string_fmt.format,
Expand Down
36 changes: 0 additions & 36 deletions protovalidate/internal/field_path.py

This file was deleted.

7 changes: 5 additions & 2 deletions protovalidate/internal/string_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# type: ignore
# TODO(#257): Fully test and fix types in this file.

from decimal import Decimal

import celpy # type: ignore
Expand Down Expand Up @@ -44,9 +47,9 @@ def __init__(self, locale: str):

def format(self, fmt: celtypes.Value, args: celtypes.Value) -> celpy.Result:
if not isinstance(fmt, celtypes.StringType):
return celpy.native_to_cel(celpy.new_error("format() requires a string as the first argument"))
return celpy.CELEvalError("format() requires a string as the first argument")
if not isinstance(args, celtypes.ListType):
return celpy.native_to_cel(celpy.new_error("format() requires a list as the second argument"))
return celpy.CELEvalError("format() requires a list as the second argument")
Comment on lines -47 to +52
Copy link
Member Author

Choose a reason for hiding this comment

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

I didn't see a native_to_cel or new_error anywhere in cel-python — while I doubt we ever hit these cases (more of an assert?), figured it made sense to update them.

# printf style formatting
i = 0
j = 0
Expand Down
Loading