unioslo / harborapi

Python async client for the Harbor REST API v2.0.
https://unioslo.github.io/harborapi/
MIT License
28 stars 5 forks source link

Safer model construction in raw mode #24

Closed pederhan closed 1 year ago

pederhan commented 1 year ago

This pull request changes the way HarborAsyncClient.construct_model() works by adding a new is_list parameter, which internally iteratively calls a new HarborAsyncClient._construct_model() method, which performs the actual model construction. Furthermore, all methods that previously called construct_model() within a list comprehension have been changed to call it once, but now with the new is_list parameter.

Rationale

The benefit of this is that when the new raw mode is enabled, we completely evade all logic that deals with assumptions about the shape of the data. When construct_model() is called in raw mode, it immediately returns whatever it received.

https://github.com/pederhan/harborapi/blob/3edfd4a9790498cd6c8b47babe15761f1837330d/harborapi/client.py#L290-L313

Currently, raw mode completely circumvents all type checks, and in the future we could potentially make it type safe by adding another overload with a generic type for the data argument of construct_model().

pederhan commented 1 year ago

Not updating changelog, since 0.7.1 hasn't been released yet, and it would be nonsensical to have "added raw" AND "made raw safer" in the same version changelog.