Skip to content

BUG -- PydanticKnownError takes no keyword arguments #10604

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

Open
1 task done
it176131 opened this issue Oct 11, 2024 · 2 comments · May be fixed by pydantic/pydantic-core#1699
Open
1 task done

BUG -- PydanticKnownError takes no keyword arguments #10604

it176131 opened this issue Oct 11, 2024 · 2 comments · May be fixed by pydantic/pydantic-core#1699
Labels
bug V2 Bug related to Pydantic V2
Milestone

Comments

@it176131
Copy link

it176131 commented Oct 11, 2024

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

I'm hoping to incorporate some custom error handling using the :class:PydanticKnownError. I tried to use the example from the docs, but this raises a TypeError rather than the expected PydanticKnownError.

A potentially quick solution would be to mark the arguments as positional-only with / and updating the docs to reflect this, but this appears to be the opposite of most pydantic classes.

Example Code

# from the example docs -> https://docs.pydantic.dev/latest/api/pydantic_core/#pydantic_core.PydanticKnownError
from pydantic_core import PydanticKnownError

def custom_validator(v) -> None:
    if v <= 10:
        raise PydanticKnownError(error_type='greater_than', context={'gt': 10})
    return v

custom_validator(10)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[4], line 1
----> 1 custom_validator(10)

Cell In[3], line 5, in custom_validator(v)
      3 def custom_validator(v) -> None:
      4     if v <= 10:
----> 5         raise PydanticKnownError(error_type='greater_than', context={'gt': 10})
      6     return v

TypeError: pydantic_core._pydantic_core.PydanticKnownError() takes no keyword arguments

Python, Pydantic & OS Version

pydantic version: 2.9.2
        pydantic-core version: 2.23.4
          pydantic-core build: profile=release pgo=false
                 install path: C:\Users\A2644752\ADO\OCE-Regulatory-2023\env\Lib\site-packages\pydantic
               python version: 3.12.6 (tags/v3.12.6:a4a2d2b, Sep  6 2024, 20:11:23) [MSC v.1940 64 bit (AMD64)]
                     platform: Windows-10-10.0.19045-SP0
             related packages: fastapi-0.111.0 mypy-1.9.0 typing_extensions-4.10.0
                       commit: unknown
@it176131 it176131 added bug V2 Bug related to Pydantic V2 pending Is unconfirmed labels Oct 11, 2024
@it176131 it176131 changed the title PydanticKnownError takes no keyword arguments BUG -- PydanticKnownError takes no keyword arguments Oct 11, 2024
@sydney-runkle
Copy link
Contributor

@it176131,

Thanks for reporting this - odd, PydanticCustomError definitely should accept kwargs.

@sydney-runkle sydney-runkle removed the pending Is unconfirmed label Oct 11, 2024
@howsunjow
Copy link
Contributor

The bug can be replicated for PydanticCustomError .

from pydantic_core import PydanticCustomError

def custom_validator(v) -> None:
    if v <= 10:
        raise PydanticCustomError(error_type='custom_value_error', message_template='Value must be greater than {value}', context={'value': 10, 'extra_context': 'extra_data'})
    return v

custom_validator(10)

The example in the documentation though only uses positional arguments.

A quick investigation seems to suggest that no class that extends PyValueError in pydantic core can take keyword arguments regardless of signature. I'm not sure whether this is a bug in or a feature of PyO3.

@Viicos Viicos added this to the v2.12 milestone Mar 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V2 Bug related to Pydantic V2
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants