diff --git a/src/attr/_compat.py b/src/attr/_compat.py index dc0cb02b6..d1f303276 100644 --- a/src/attr/_compat.py +++ b/src/attr/_compat.py @@ -211,15 +211,22 @@ def force_x_to_be_a_cell(): # pragma: no cover ) set_first_freevar_code = types.CodeType(*args) - def set_closure_cell(cell, value): - # Create a function using the set_first_freevar_code, - # whose first closure cell is `cell`. Calling it will - # change the value of that cell. - setter = types.FunctionType( - set_first_freevar_code, {}, "setter", (), (cell,) - ) - # And call it to set the cell. - setter(value) + if sys.version_info >= (3, 11): + + def set_closure_cell(cell, value): + cell.cell_contents = value + + else: + + def set_closure_cell(cell, value): + # Create a function using the set_first_freevar_code, + # whose first closure cell is `cell`. Calling it will + # change the value of that cell. + setter = types.FunctionType( + set_first_freevar_code, {}, "setter", (), (cell,) + ) + # And call it to set the cell. + setter(value) # Make sure it works on this interpreter: def make_func_with_cell():