boardpack / pydantic-i18n

pydantic-i18n is an extension to support an i18n for the pydantic error messages.
https://pydantic-i18n.boardpack.org
MIT License
82 stars 12 forks source link

Can not translate the errors, errors method does not exist #284

Closed sehHeiden closed 6 days ago

sehHeiden commented 3 weeks ago

I want to translate the error. Just the method to get the pydantic errors does not exist.

I tried:

    loader = JsonLoader(PROJECT_PATH / "translations")
    tr = PydanticI18n(loader)

    with pytest.raises(ValidationError) as e:
        Configuration(**config)
    translated_errors = tr.translate(e.errors(), locale="de_DE")

Returns the error: AttributeError: 'ExceptionInfo' object has no attribute 'errors'

Environment

sehHeiden commented 3 weeks ago

In Pytest:

e.value.errors() is needed!

dukkee commented 3 weeks ago

Hello @sehHeiden So what is your issue now?

Yes, you need to use the e.value instead of just e to access the exception object and then you can use the errors method.

sehHeiden commented 3 weeks ago

Well, I found the problem myself. That's why a wrote a second statement. Would be nice, when I would have found the remark in documentation, though.

dukkee commented 6 days ago

Hey @sehHeiden It's related more to the pytest than to the pydantic-i18n I think. Here is the description of this value attribute usage: https://docs.pytest.org/en/7.1.x/how-to/assert.html#assertions-about-expected-exceptions