harborapi icon indicating copy to clipboard operation
harborapi copied to clipboard

Add `MaybeModel[T]` type for POST/PUT/PATCH endpoint methods

Open pederhan opened this issue 3 years ago • 0 comments

Rationale

It is currently possible to pass dicts to methods that create or update resources. This is not explained anywhere, and is basically an implementation detail.

Being able to pass in dicts to these methods is beneficial if the API spec changes in a backwards-incompatible way in the future, as users will be able to pass in custom dicts conforming to the new spec until an official harborapi update is available, after which they can resume using the models.

Implementation

In order to document this (currently implicit) behavior, we should add a new type called MaybeModel[T] that is an alias for Union[T, dict[str, Any]], where T is bound to BaseModel, and replace current types on POST/PUT/PATCH methods with this type.

from typing import TypeVar, Union, Any
from pydantic import BaseModel

T = TypeVar('T', bound=BaseModel)

MaybeModel = Union[T, dict[str, Any]]

pederhan avatar Dec 20 '22 14:12 pederhan