|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -import linecache |
4 | 3 | import re |
5 | 4 | from typing import TYPE_CHECKING, Any, Callable, Iterable, Mapping, Tuple, TypeVar |
6 | 5 |
|
@@ -212,22 +211,17 @@ def make_dict_unstructure_fn( |
212 | 211 | + [" return res"] |
213 | 212 | ) |
214 | 213 | script = "\n".join(total_lines) |
215 | | - |
216 | 214 | fname = generate_unique_filename( |
217 | | - cl, "unstructure", reserve=_cattrs_use_linecache |
| 215 | + cl, "unstructure", lines=total_lines if _cattrs_use_linecache else [] |
218 | 216 | ) |
219 | 217 |
|
220 | 218 | eval(compile(script, fname, "exec"), globs) |
221 | | - |
222 | | - fn = globs[fn_name] |
223 | | - if _cattrs_use_linecache: |
224 | | - linecache.cache[fname] = len(script), None, total_lines, fname |
225 | 219 | finally: |
226 | 220 | working_set.remove(cl) |
227 | 221 | if not working_set: |
228 | 222 | del already_generating.working_set |
229 | 223 |
|
230 | | - return fn |
| 224 | + return globs[fn_name] |
231 | 225 |
|
232 | 226 |
|
233 | 227 | DictStructureFn = Callable[[Mapping[str, Any], Any], T] |
@@ -628,11 +622,12 @@ def make_dict_structure_fn( |
628 | 622 | *pi_lines, |
629 | 623 | ] |
630 | 624 |
|
631 | | - fname = generate_unique_filename(cl, "structure", reserve=_cattrs_use_linecache) |
632 | 625 | script = "\n".join(total_lines) |
| 626 | + fname = generate_unique_filename( |
| 627 | + cl, "structure", lines=total_lines if _cattrs_use_linecache else [] |
| 628 | + ) |
| 629 | + |
633 | 630 | eval(compile(script, fname, "exec"), globs) |
634 | | - if _cattrs_use_linecache: |
635 | | - linecache.cache[fname] = len(script), None, total_lines, fname |
636 | 631 |
|
637 | 632 | return globs[fn_name] |
638 | 633 |
|
@@ -743,9 +738,11 @@ def make_mapping_unstructure_fn( |
743 | 738 | if kh == identity: |
744 | 739 | kh = None |
745 | 740 |
|
746 | | - val_handler = converter._unstructure_func.dispatch(val_arg) |
747 | | - if val_handler == identity: |
748 | | - val_handler = None |
| 741 | + if val_arg is not Any: |
| 742 | + # TODO: Remove this once we have more consistent Any handling in place. |
| 743 | + val_handler = converter._unstructure_func.dispatch(val_arg) |
| 744 | + if val_handler == identity: |
| 745 | + val_handler = None |
749 | 746 |
|
750 | 747 | globs = { |
751 | 748 | "__cattr_mapping_cl": unstructure_to or cl, |
|
0 commit comments