Skip to content

Add more precise typing #449

@bidoubiwa

Description

@bidoubiwa

See this thread for context:

To enjoy the advantages of types, we should consider adding more complex types to this repo.

As per the thread

@brunoocasali asking if it is breaking.
answer from @sanders41

@brunoocasali it depends on how you look at it. Really Dict[str, int] has been wrong since tasks were introduced and I am thinking @alallema is first to notice and made the correction here. So really it was breaking in a previous release and a bug fix here.

The reason this was easy to miss is a dictionary is returned, but mypy has no way to know what the dictionary will be with the current implementation. Since all it knows you you said you are returning a dictionary and one is being returned it assumes you have the rest correct. The point @bidoubiwa brings up would prevent this from happening because at that point mypy would know exactly what to expect in the return values.

Implementation:

from @sanders41

There is a TypedDict, but it is first available in Python 3.8. You can do it all the way back to Python 3.6 by using Pydantic classes instead of dictionaries, but that would be a big change.

Pydantic is how I do it so if you do decide to go this direction feel free to grab my models and use them here https://github.com/sanders41/meilisearch-python-async/tree/main/meilisearch_python_async/models

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions