Skip to content

Commit 84027a8

Browse files
committed
fix namelambda on Python 3.10
1 parent 8b5e487 commit 84027a8

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

unpythonic/misc.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,9 @@ def rename(f):
107107
# https://docs.python.org/3/library/types.html#types.CodeType
108108
# https://docs.python.org/3/library/inspect.html#types-and-members
109109
if version_info >= (3, 8, 0): # Python 3.8+: positional-only parameters
110-
f.__code__ = CodeType(co.co_argcount, co.co_posonlyargcount, co.co_kwonlyargcount,
111-
co.co_nlocals, co.co_stacksize, co.co_flags,
112-
co.co_code, co.co_consts, co.co_names,
113-
co.co_varnames, co.co_filename,
114-
name,
115-
co.co_firstlineno, co.co_lnotab, co.co_freevars,
116-
co.co_cellvars)
110+
# In Python 3.8+, `CodeType` has the convenient `replace()` method to functionally update it.
111+
# In Python 3.10, we must actually use it to avoid losing the line number info.
112+
f.__code__ = f.__code__.replace(co_name=name)
117113
else:
118114
f.__code__ = CodeType(co.co_argcount, co.co_kwonlyargcount,
119115
co.co_nlocals, co.co_stacksize, co.co_flags,

0 commit comments

Comments
 (0)