|
2 | 2 |
|
3 | 3 | import re |
4 | 4 | import sys |
| 5 | +from collections.abc import Mapping |
5 | 6 | from typing import TYPE_CHECKING, Any, Callable, Literal, TypedDict, TypeVar |
6 | 7 |
|
7 | 8 | from attrs import NOTHING, Attribute |
@@ -307,15 +308,18 @@ def make_dict_structure_fn( |
307 | 308 | globs["__c_feke"] = ForbiddenExtraKeysError |
308 | 309 |
|
309 | 310 | if _cattrs_detailed_validation: |
310 | | - # When running under detailed validation, be extra careful about copying |
311 | | - # so that the correct error is raised if the input isn't a dict. |
312 | | - lines.append(" try:") |
313 | | - lines.append(" res = o.copy()") |
314 | | - lines.append(" except Exception as exc:") |
| 311 | + # When running under detailed validation, be extra careful about the |
| 312 | + # input type so that the correct error is raised if the input isn't a dict. |
| 313 | + internal_arg_parts["__c_mapping"] = Mapping |
| 314 | + lines.append(" if not isinstance(o, __c_mapping):") |
| 315 | + te = "TypeError(f'expected a mapping, not {o.__class__.__name__}')" |
315 | 316 | lines.append( |
316 | | - f" raise __c_cve('While structuring ' + {cl.__name__!r}, [exc], __cl)" |
| 317 | + f" raise __c_cve('While structuring ' + {cl.__name__!r}, [{te}], __cl)" |
317 | 318 | ) |
318 | 319 |
|
| 320 | + lines.append(" res = o.copy()") |
| 321 | + |
| 322 | + if _cattrs_detailed_validation: |
319 | 323 | lines.append(" errors = []") |
320 | 324 | internal_arg_parts["__c_cve"] = ClassValidationError |
321 | 325 | internal_arg_parts["__c_avn"] = AttributeValidationNote |
@@ -389,7 +393,6 @@ def make_dict_structure_fn( |
389 | 393 | f" if errors: raise __c_cve('While structuring ' + {cl.__name__!r}, errors, __cl)" |
390 | 394 | ) |
391 | 395 | else: |
392 | | - lines.append(" res = o.copy()") |
393 | 396 | non_required = [] |
394 | 397 |
|
395 | 398 | # The first loop deals with required args. |
|
0 commit comments