Skip to content

Bug with tracebacks on IPython when detailed_validations=True #590

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

Closed
edthrn opened this issue Oct 13, 2024 · 3 comments
Closed

Bug with tracebacks on IPython when detailed_validations=True #590

edthrn opened this issue Oct 13, 2024 · 3 comments

Comments

@edthrn
Copy link

edthrn commented Oct 13, 2024

  • cattrs version: 24.1.2
  • Python version: 3.11
  • Operating System: Linux

Description

import attrs, cattrs

@attrs.define
class Test:
    a: int
    b: str

cattrs.structure({"a": "hello", "b": "world"}, Test)

The expected traceback is the following:

  + Exception Group Traceback (most recent call last):
  |   File "/tmp/test_cattrs.py", line 8, in <module>
  |     cattrs.structure({"a": "hello", "b": "world"}, Test)
  |   File "<redacted>/lib/python3.11/site-packages/cattrs/converters.py", line 558, in structure
  |     return self._structure_func.dispatch(cl)(obj, cl)
  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |   File "<cattrs generated structure __main__.Test>", line 14, in structure_Test
  | cattrs.errors.ClassValidationError: While structuring Test (1 sub-exception)
  +-+---------------- 1 ----------------
    | Traceback (most recent call last):
    |   File "<cattrs generated structure __main__.Test>", line 5, in structure_Test
    | ValueError: invalid literal for int() with base 10: 'hello'
    | Structuring class Test @ attribute a
    +------------------------------------

⚠️ HOWEVER, if you execute this Python code within IPython or a Jupyter Notebook, the helpful traceback is lost, and we get another error that don't relate to the actual validation problem:

image
The following code is expected to raise an error:

Edit

When turning off detailed_validation, the error message is the expected one:
image

@edthrn edthrn changed the title Traceback are obfuscated for custom validation on IPython Bug with tracebacks on IPython when detailed_validations=True Oct 13, 2024
@Tinche
Copy link
Member

Tinche commented Oct 13, 2024

Hm, works for me:

ipython
Python 3.11.1 (main, Dec 16 2022, 13:37:35) [Clang 14.0.0 (clang-1400.0.29.202)]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.28.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import attrs, cattrs
   ...:
   ...: @attrs.define
   ...: class Test:
   ...:     a: int
   ...:     b: str
   ...:
   ...: cattrs.structure({"a": "hello", "b": "world"}, Test)
  + Exception Group Traceback (most recent call last):
  |   File "/Users/tintvrtkovic/pg/cattrs/.venv/lib/python3.11/site-packages/IPython/core/interactiveshell.py", line 3577, in run_code
  |     exec(code_obj, self.user_global_ns, self.user_ns)
  |   File "<ipython-input-1-10a21ea11d1c>", line 8, in <module>
  |     cattrs.structure({"a": "hello", "b": "world"}, Test)
  |   File "/Users/tintvrtkovic/pg/cattrs/src/cattrs/converters.py", line 565, in structure
  |     return self._structure_func.dispatch(cl)(obj, cl)
  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |   File "<cattrs generated structure __main__.Test>", line 14, in structure_Test
  |     if errors: raise __c_cve('While structuring ' + 'Test', errors, __cl)
  |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  | cattrs.errors.ClassValidationError: While structuring Test (1 sub-exception)
  +-+---------------- 1 ----------------
    | Traceback (most recent call last):
    |   File "<cattrs generated structure __main__.Test>", line 5, in structure_Test
    |     res['a'] = __c_structure_a(o['a'])
    |                 ^^^^^^^^^^^^^^^^^^^^^^^
    | ValueError: invalid literal for int() with base 10: 'hello'
    | Structuring class Test @ attribute a
    +------------------------------------

In any case, it's a question of ipython displaying exception groups properly, so if it didn't work you might try their issue tracker instead. I see support for this has been added in ipython/ipython@87828d3 though, so you might need to upgrade?

@Tinche Tinche closed this as not planned Won't fix, can't repro, duplicate, stale Oct 13, 2024
@edthrn
Copy link
Author

edthrn commented Oct 14, 2024

Apologies, I wasn't aware that Exception Group was a "new" Python mechanism (first time I encounter them), and thought it was a nice feature brought by the lib.

@Tinche
Copy link
Member

Tinche commented Oct 14, 2024

No need to apologize! 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants