Skip to content

Commit d9ac523

Browse files
committed
@pcrespov review: add some more string comparisons
1 parent 991973c commit d9ac523

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

packages/aws-library/src/aws_library/ec2/_models.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import contextlib
12
import datetime
23
import re
34
import tempfile
@@ -17,6 +18,8 @@
1718
StrictFloat,
1819
StrictInt,
1920
StringConstraints,
21+
TypeAdapter,
22+
ValidationError,
2023
field_validator,
2124
)
2225
from pydantic.config import JsonDict
@@ -82,8 +85,15 @@ def __gt__(self, other: "Resources") -> bool:
8285
return False
8386
else:
8487
# remaining options is a is str and b is str or mixed types
88+
# NOTE: we cannot compare strings unless they are equal or some kind of boolean (e.g. "true", "false", "yes", "no", "1", "0")
8589
assert isinstance(a, str) # nosec
8690
assert isinstance(b, int | float | str) # nosec
91+
# let's try to get a boolean out of the values to compare them
92+
with contextlib.suppress(ValidationError):
93+
a_as_boolean = TypeAdapter(bool).validate_python(a)
94+
b_as_boolean = TypeAdapter(bool).validate_python(b)
95+
if not a_as_boolean and b_as_boolean:
96+
return False
8797

8898
# here we have either everything greater or equal or non-comparable strings
8999

packages/aws-library/tests/test_ec2_models.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@
8383
Resources(cpus=0.1, ram=ByteSize(1), generic_resources={"SSE": "yes"}),
8484
True,
8585
),
86+
(
87+
Resources(cpus=0.1, ram=ByteSize(1), generic_resources={"SSE": "yes"}),
88+
Resources(cpus=0.1, ram=ByteSize(1), generic_resources={"SSE": "no"}),
89+
True,
90+
),
91+
(
92+
Resources(cpus=0.1, ram=ByteSize(1), generic_resources={"SSE": "no"}),
93+
Resources(cpus=0.1, ram=ByteSize(1), generic_resources={"SSE": "yes"}),
94+
False,
95+
),
8696
(
8797
Resources(cpus=0.1, ram=ByteSize(1)),
8898
Resources(cpus=0.1, ram=ByteSize(1), generic_resources={"SSE": "yes"}),
@@ -179,6 +189,11 @@ def test_resources_ge_operator(
179189
Resources(cpus=0.1, ram=ByteSize(1), generic_resources={"SSE": "no"}),
180190
True,
181191
),
192+
(
193+
Resources(cpus=0.1, ram=ByteSize(1), generic_resources={"SSE": "no"}),
194+
Resources(cpus=0.1, ram=ByteSize(1), generic_resources={"SSE": "yes"}),
195+
False,
196+
),
182197
],
183198
)
184199
def test_resources_gt_operator(a: Resources, b: Resources, a_greater_than_b: bool):

0 commit comments

Comments
 (0)