lk-geimfari / mimesis

Mimesis is a robust data generator for Python that can produce a wide range of fake data in multiple languages.
https://mimesis.name
MIT License
4.38k stars 326 forks source link

Type hint the providers in the `Generic` class #1416

Closed guacs closed 7 months ago

guacs commented 11 months ago

Feature request

Would it be possible to type hint the providers in the Generic class?

Thesis

class Generic(BaseProvider):
    """Class which contain all providers at one."""

    address: Address
    # similarly type hint the other providers

    ...

Reasoning

This allows for autocomplete in editors/IDEs, and static type checking by type checkers.

lk-geimfari commented 7 months ago

I'm totally agree with this request. It would be much better to have annotated Generic.

@sobolevn I know that you have solid expertise with mypy and typing in general. What would you recommend to annotate lazy attributes?

I thought to solve this problem by adding a stub file for generic.py like this:

import typing as t

from mimesis.locales import Locale
from mimesis.providers import Address, Datetime, Finance, Food, Person, Text
from mimesis.providers.base import BaseProvider
from mimesis.types import Seed

__all__ = ["Generic"]

class Generic(BaseProvider):
    locale: Locale
    # These attributes are lazy set in __init__ and __getattr_.
    person: Person
    text: Text
    address: Address
    finance: Finance
    datetime: Datetime
    food: Food
    # ...
    # ...

    def __init__(self, locale: Locale = ..., seed: Seed = ...) -> None: ...

    class Meta:
        name: t.Final[str]

    def __getattr__(self, attrname: str) -> t.Any: ...

    def __dir__(self) -> list[str]: ...

    def reseed(self, seed: Seed = ...) -> None: ...

    def add_provider(self, cls: t.Type[BaseProvider], **kwargs: t.Any) -> None: ...

    def add_providers(self, *providers: t.Type[BaseProvider]) -> None: ...

    def __iadd__(self, other: t.Type[BaseProvider]) -> Generic: ...

But I'm not quite sure about this idea. Any advice or recommendation would be greatly appreciated.

sobolevn commented 7 months ago

seems fine :)

lk-geimfari commented 7 months ago

Fixed in 12.1.0