Skip to content

Fix usage of dict[str, Any] annotations. #6

@pederhan

Description

@pederhan

dict[str, Any] annotations are potentially harmful as we forego type checking of data used as inputs to the various HTTP client methods.

For example:

def patch(self, fields: dict[str, Any], validate: bool = True) -> Self:
"""Patch the object with the given values.
Notes:
-----
1. Depending on the endpoint, the server may not return the patched object.
2. Patching with None may not clear the field if it isn't nullable (which few fields
are). Odds are you want to pass an empty string instead.
:param fields: The values to patch.
:param validate: Whether to validate the patched object.
:returns: The object refetched from the server.
"""
from mreg_api.client import MregClient # noqa: PLC0415
MregClient().patch(self.endpoint().with_id(self.id_for_endpoint()), **fields)

Here, we may pass any data to MregClient.patch, which may result in an unhandled runtime exception when passing it unserializable data. We need to use Json, JsonMapping and QueryParams wherever we can in order to facilitate better type checking. Ideally we ban Any.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions