Skip to content

Problem with pydanticΒ #63

Description

@Athesto

I'm using this code πŸ—’οΈ

#!/usr/bin/env python3
from typing import Optional
from pydantic import BaseModel, Field

import pretty_errors

pretty_errors.configure(
    separator_character = '*',
    display_link        = True,
    exception_above     = True,
    exception_below     = False,
    line_color          = pretty_errors.RED + '> ' + pretty_errors.default_config.line_color,
    code_color          = '  ' + pretty_errors.default_config.line_color,
    display_locals      = True,
)


class MyEvent(BaseModel):
    function_name: str = Field('my_function', frozen=True)
    limit: int = 10


myEvent = MyEvent()
myEvent2 = MyEvent(function_name="hello world")
print(myEvent.model_dump())
print(myEvent2.model_dump())
myEvent.function_name = "altered_function"
print(myEvent.model_dump())

The pretty-errors show this πŸ’…πŸ½

{'function_name': 'my_function', 'limit': 10}
{'function_name': 'hello world', 'limit': 10}

*****************************************************************************************************************************
pydantic_core._pydantic_core.ValidationError:
MyEvent
[<builtins.PyLineError object at 0x7f21af7489c0>]
python
False

test-pydantic.py 27 <module>
"/home/gustavo/tmp/sandbox-python/./test-pydantic.py", line 27
> myEvent.function_name = "altered_function"

main.py 910 __setattr__
"/home/gustavo/tmp/sandbox-python/venv/lib/python3.11/site-packages/pydantic/main.py", line 910
> self._check_frozen(name, value)

main.py 985 _check_frozen
"/home/gustavo/tmp/sandbox-python/venv/lib/python3.11/site-packages/pydantic/main.py", line 985
> raise pydantic_core.ValidationError.from_exception_data(self.__class__.__name__, [error])

self: function_name='my_function' limit=10
name: function_name
error: {'type': 'frozen_field', 'loc': ('function_name',), 'input': 'altered_function'}

and the normal output is this πŸ’»

{'function_name': 'my_function', 'limit': 10}
{'function_name': 'hello world', 'limit': 10}
Traceback (most recent call last):
  File "/home/gustavo/tmp/sandbox-python/./test-pydantic.py", line 17, in <module>
    myEvent.function_name = "altered_function"
    ^^^^^^^^^^^^^^^^^^^^^
  File "/home/gustavo/tmp/sandbox-python/venv/lib/python3.11/site-packages/pydantic/main.py", line 910, in __setattr__
    self._check_frozen(name, value)
  File "/home/gustavo/tmp/sandbox-python/venv/lib/python3.11/site-packages/pydantic/main.py", line 985, in _check_frozen
    raise pydantic_core.ValidationError.from_exception_data(self.__class__.__name__, [error])
pydantic_core._pydantic_core.ValidationError: 1 validation error for MyEvent
function_name
  Field is frozen [type=frozen_field, input_value='altered_function', input_type=str]
    For further information visit https://errors.pydantic.dev/2.10/v/frozen_field

The problem is Pretty-Erros shows an object in the Exception ❌

pydantic_core._pydantic_core.ValidationError:
MyEvent
[<builtins.PyLineError object at 0x7f21af7489c0>]
python
False

And the normal output shows the correct message βœ…

pydantic_core._pydantic_core.ValidationError: 1 validation error for MyEvent
function_name
  Field is frozen [type=frozen_field, input_value='altered_function', input_type=str]
    For further information visit https://errors.pydantic.dev/2.10/v/frozen_field

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions