Skip to content

BUG -- PydanticKnownError takes no keyword arguments #10604

Closed
pydantic/pydantic-core
#1699
@it176131

Description

@it176131

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug V2Bug related to Pydantic V2

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions