konradhalas / dacite

Simple creation of data classes from dictionaries.
MIT License
1.76k stars 106 forks source link

Suppress context in ForwardReferenceError and MissingValueError #265

Open m-aciek opened 3 weeks ago

m-aciek commented 3 weeks ago

It simplifies the traceback presented to the user, not showing the library internals.

Traceback previously/now for MissingValueError:

Previously: ```python Traceback (most recent call last): File "/Users/maciej.olko/projects/dacite/dacite/core.py", line 72, in from_dict value = get_default_value_for_field(field, field_type) ~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^ File "/Users/maciej.olko/projects/dacite/dacite/dataclasses.py", line 21, in get_default_value_for_field raise DefaultValueNotFoundError() dacite.dataclasses.DefaultValueNotFoundError During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/Users/maciej.olko/Library/Application Support/JetBrains/PyCharm2024.2/scratches/scratch_167.py", line 11, in from_dict(X, {"s": "test"}) ~~~~~~~~~^^^^^^^^^^^^^^^^^^ File "/Users/maciej.olko/projects/dacite/dacite/core.py", line 76, in from_dict raise MissingValueError(field.name) dacite.exceptions.MissingValueError: missing value for field "i" ``` Now: ```python Traceback (most recent call last): File "/Users/maciej.olko/Library/Application Support/JetBrains/PyCharm2024.2/scratches/scratch_167.py", line 11, in from_dict(X, {"s": "test"}) ~~~~~~~~~^^^^^^^^^^^^^^^^^^ File "/Users/maciej.olko/projects/dacite/dacite/core.py", line 76, in from_dict raise MissingValueError(field.name) from None dacite.exceptions.MissingValueError: missing value for field "i" ```

Previously/now for ForwardReferenceError:

Previously: ```python Traceback (most recent call last): File "/Users/maciej.olko/projects/dacite/dacite/core.py", line 51, in from_dict … # couldn't really trigger this error NameError(…) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/Users/maciej.olko/Library/Application Support/JetBrains/PyCharm2024.2/scratches/scratch_167.py", line 11, in from_dict(X, {"s": "test"}) ~~~~~~~~~^^^^^^^^^^^^^^^^^^ File "/Users/maciej.olko/projects/dacite/dacite/core.py", line 53, in from_dict raise ForwardReferenceError(str(error)) dacite.exceptions.ForwardReferenceError: can not resolve forward reference: ``` Now: ```python Traceback (most recent call last): File "/Users/maciej.olko/Library/Application Support/JetBrains/PyCharm2024.2/scratches/scratch_167.py", line 11, in from_dict(X, {"s": "test"}) ~~~~~~~~~^^^^^^^^^^^^^^^^^^ File "/Users/maciej.olko/projects/dacite/dacite/core.py", line 53, in from_dict raise ForwardReferenceError(str(error)) from None dacite.exceptions.ForwardReferenceError: can not resolve forward reference: ```
m-aciek commented 3 weeks ago

The linting change tested locally with:

% uvx --python 3.9 --exclude-newer=2019-12-17 pylint dacite/core.py
   Built wrapt==1.11.2
   Built lazy-object-proxy==1.4.3
Installed 7 packages in 7ms

------------------------------------
Your code has been rated at 10.00/10