Skip to content

Mypy: improve type hints #74

Closed
Merinorus/pydantic-i18n
#1
@Merinorus

Description

@Merinorus

Describe the bug

Hello!

The translate function returns a Dict[str, Any].
When using the result of a translation, I want to read the loc attribute.
Eg: error.get("loc")

But it leads to a Mypy error: Value of type "Optional[Any]" is not indexable.

To correct this, I use the ErrorDict type included in the pydantic lib, to improve type checking. If we look at the pydantic source code, we get:

Loc = Tuple[Union[int, str], ...]

class _ErrorDictRequired(TypedDict):
    loc: Loc
    msg: str
    type: str

class ErrorDict(_ErrorDictRequired, total=False):
    ctx: Dict[str, Any]

which is an equivalent of:

class ErrorDict(TypedDict, total=False):
    loc: Loc
    msg: str
    type: str
    ctx: Dict[str, Any]

Proposal

Instead of returning a Dict[str, Any], the translate function should return a pydantic ErrorDict.

Environment

  • OS: Linux Ubuntu 22.04
  • pydantic-i18n version: 0.3.0
  • Python version: 3.10

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions