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

Accept arbitrary types for memos & results, clarify other kwarg types #747

Merged
merged 3 commits into from May 1, 2021

Conversation

nolar
Copy link
Owner

@nolar nolar commented May 1, 2021

Memos are usually of type kopf.Memo but can be configured to any custom type (in embedded operators mostly). As such, the framework must not rely on any behaviour of memo instances, so it uses object as the type which has no methods or properties or fields.

However, for the user-facing signatures of handler callbacks, it is declared as Any for the same purpose: to accept any specialised custom type. Users are encouraged to put their own classes as annotations of the memo kwarg; kopf.Memo works as well.

The original declaration of handler callbacks with memo: AnyMemo was problematic as it was forcing the operator developers to accept memo exactly as this type only: Union[kopf.Memo, object], not just kopf.Memo alone — which was against the intended usage scenario with arbitrary custom types or the default type.

For even stricter control, to not mess with any other value by accident (as object would accept anything), it is redeclared as a new type "AnyMemo". For the user-facing embedded operator functions, it is defined as literally object to prevent exposing the declared new type AnyMemo. The type-casting of pure object to redeclared AnyMemo=object is done at the earliest possible point internally, hidden from the users.


Similarly, fixes the differences of the logger kwarg (now, there is kopf.Logger type), and of the handler results (were expected as never-exported Optional[Result], will be expected as Optional[object] and type-casted where needed).

fixes #746

Memos are usually of type `kopf.Memo` but can be configured to any custom type (in embedded operators mostly). As such, the framework must not rely on any behaviour of memo instances, so it uses `object` as the type which has no methods or properties or fields.

However, for the user-facing signatures of handler callbacks, it is declared as `Any` for the same purpose: to accept any specialised custom type. Users are encouraged to put their own classes as annotations of the `memo` kwarg; `kopf.Memo` works as well.

The original declaration of handler callbacks with `memo: AnyMemo` was problematic as it was forcing the operator developers to accept `memo` exactly as this type only: `Union[kopf.Memo, object]`, not just `kopf.Memo` alone — which was against the intended usage scenario with arbitrary custom types or the default type.

For even stricter control, to not mess with any other value by accident (as `object` would accept anything), it is redeclared as a _new type_ "AnyMemo". For the user-facing embedded operator functions, it is defined as literally `object` to prevent exposing the declared new type `AnyMemo`. The type-casting of pure `object` to redeclared `AnyMemo=object` is done at the earliest possible point internally, hidden from the users.

Signed-off-by: Sergey Vasilyev <nolar@nolar.info>
@nolar nolar added the bug Something isn't working label May 1, 2021
nolar added 2 commits May 1, 2021 13:53
Signed-off-by: Sergey Vasilyev <nolar@nolar.info>
Previously, `Result(object)` was expected from the handlers despite `Any` was essentially meant for the public protocols. `Result` is only needed for internal type-checking — to avoid using any other value instead of the results by accident. Now, `Optional[object]` is specified for the protocols as seen by the users, and it is casted to `Optional[Result]` where needed.

There is no need to declare the protocol results as `Any` because any actual type or structure becomes a special case of `Optional[object]` anyway. The latter form allows us to be strict for not using any methods/fields/properties and to make no implicit assumptions on the result type.

Signed-off-by: Sergey Vasilyev <nolar@nolar.info>
@nolar nolar changed the title Accept custom memo types properly Accept arbitrary types for memos & results, clarify other kwarg types May 1, 2021
@nolar nolar merged commit 9c931fe into main May 1, 2021
@nolar nolar deleted the proper-memo-typing-2 branch May 1, 2021 14:26
@nolar nolar added the refactoring Code cleanup without new features added label May 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working refactoring Code cleanup without new features added
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant