-
Notifications
You must be signed in to change notification settings - Fork 15
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
Add function to parse quantity #37
Conversation
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.
Hi, thanks for your PR. Can you add this function to the documentation together with an example? If you introduce the utils
sub-package you can create docs/utils.md
Thanks @gtsystem! Ready for re-review. |
|
||
|
||
@overload | ||
def equals_canonically(first: Optional[dict], second: Optional[dict]) -> bool: |
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.
I don't see sufficient benefits of supporting dicts. Users can always convert dicts to ResourceRequirements (ResourceRequirements.from_dict
). I'll suggest to drop this for now, so that we can have a simpler implementation.
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.
The dict here is not an alternative representation of
ResourceRequirements
, but of ResourceRequirements.limits
and ResourceRequirements.requests
, i.e. one level down.
The tests demonstrate this. With the overload we're able to do both:
equals_canonically({"cpu": "0.6"}, {"cpu": "600m"})
and
equals_canonically(
ResourceRequirements(limits={"cpu": "0.6"}),
ResourceRequirements(limits={"cpu": "600m"})
)
If feasible, we could change ResourceRequirements to use TypedDict
+class ResourceSpecDict(TypedDict, total=False):
+ cpu: Optional[str]
+ memory: Optional[str]
@dataclass
class ResourceRequirements(DataclassDictMixIn):
- limits: 'dict' = None
- requests: 'dict' = None
+ limits: ResourceSpecDict = None
+ requests: ResourceSpecDict = None
In which case the overload would become
@overload
def equals_canonically(first: Optional[ResourceSpecDict], second: Optional[ResourceSpecDict]) -> bool:
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.
Got it, I was confused by the function description "Compare two resource requirements for numerical equality." I assumed that the only acceptable type is ResourceRequirements
or the equivalent dict. Maybe we can clarify further?
No need to introduce the TypedDict as anyway all the keys are optional and we can have further keys.
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.
Fixed in next commit.
TypeError: Subscripted generics cannot be used with class and instance checks
Approved and merged. We have another contribution in progress, once that is completed I will create a public release. Thanks again. |
This PR add a utility function for parsing K8s "quantity" from string.
The value is parsed into an absolute (unitless)
decimal.Decimal
instance, and not into the K8s "canonical representation". This way the code is simpler and values are readily comparable.References:
Fixes #36.