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

Mypy: improve type hints #74

Closed
Merinorus opened this issue Feb 10, 2023 · 2 comments · May be fixed by Merinorus/pydantic-i18n#1
Closed

Mypy: improve type hints #74

Merinorus opened this issue Feb 10, 2023 · 2 comments · May be fixed by Merinorus/pydantic-i18n#1
Labels
bug Something isn't working

Comments

@Merinorus
Copy link

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
@Merinorus Merinorus added the bug Something isn't working label Feb 10, 2023
Merinorus added a commit to Merinorus/pydantic-i18n that referenced this issue Feb 10, 2023
@dukkee
Copy link
Member

dukkee commented Feb 11, 2023

Hi @Merinorus Thanks a lot for your time and fix. Of course, I will review it soon and this fix will be in the next release

Merinorus added a commit to Merinorus/pydantic-i18n that referenced this issue Feb 12, 2023
@dukkee
Copy link
Member

dukkee commented Mar 4, 2023

Hi @Merinorus This issue was fixed in #88
It'll be released soon

@dukkee dukkee closed this as completed Mar 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants