#4067·Nuitka

Python 3.14: annotationlib.call_annotate_function(FORWARDREF) fails for a compiled class __annotate__ that closes over a local

Author: KRRT7Created Sep 29, 2026Updated Sep 29, 2026

Bug Description

On Python 3.14, annotationlib.call_annotate_function(cls.__annotate__, Format.FORWARDREF) fails for a compiled class whose annotations refer to a local of the enclosing function. It works under CPython.

In FORWARDREF format, annotationlib rebuilds the annotate function as types.FunctionType(annotate.__code__, globals, closure=...), pairing annotate.__code__.co_freevars with annotate.__closure__ (_build_closure). The compiled __annotate__ doesn't satisfy that:

  • 4.2.2: co_freevars is empty while __closure__ holds a cell → ValueError: zip() argument 2 is longer than argument 1
  • develop (4.3rc4): the closure holds compiled_cell objects → TypeError: arg 5 (closure) expected cell, found compiled_cell

This is the path msgspec takes to read a Struct's fields on 3.14 (msgspec._utils.call_annotate_forwardref), so any msgspec.Struct defined inside a function fails in a compiled program. That is how we hit it: a Nuitka-built CLI failed on its first use of such a struct.

Possibly related: #3868 (classes without a working __annotate__ on 3.14+), #3815 (compiled_cell passed as a closure to FunctionType).

️ Environment

1. Nuitka Version, Python Version, OS, and Platform

4.2.2
Commercial: None
Python: 3.14.7 (main, Sep 24 2026, 17:58:18) [Clang 22.1.3 ]
Flavor: Python Build Standalone
GIL: yes
OS: Linux
Arch: x86_64
Distribution: Ubuntu (based on Debian) 26.04.1
Version C compiler: gcc (gcc 15).

Also reproduced with develop: 4.3rc4.

2. How Nuitka and Python were Installed

Python: uv-managed CPython 3.14.7 (python-build-standalone). Nuitka: uv pip install nuitka into a fresh venv; develop from https://github.com/Nuitka/Nuitka/archive/develop.zip.

3. Relevant PyPI Packages and Versions

None needed for the reproduction (standard library only). Downstream it shows with msgspec 0.22.0.

Reproduction

repro.py:

python
import annotationlib
import sys


def make():
    from typing import Any

    class C:
        x: Any = None

    return C


C = make()
print(sys.version.split()[0], "compiled" if "__compiled__" in globals() else "interpreted")
print(annotationlib.call_annotate_function(C.__annotate__, annotationlib.Format.FORWARDREF))
bash
python repro.py
python -m nuitka --mode=accelerated --remove-output repro.py
./repro.bin

Expected (CPython)

3.14.7 interpreted
{'x': typing.Any}

Actual, Nuitka 4.2.2

3.14.7 compiled
Traceback (most recent call last):
  File "repro.py", line 20, in <module>
    print(annotationlib.call_annotate_function(C.__annotate__, annotationlib.Format.FORWARDREF))
  File ".../lib/python3.14/annotationlib.py", line 810, in call_annotate_function
    closure, cell_dict = _build_closure(
  File ".../lib/python3.14/annotationlib.py", line 879, in _build_closure
    for name, cell in zip(annotate.__code__.co_freevars, annotate.__closure__, strict=True):
ValueError: zip() argument 2 is longer than argument 1

Actual, Nuitka develop (4.3rc4)

3.14.7 compiled
  ...
    kwdefaults=annotate.__kwdefaults__,
    )
TypeError: arg 5 (closure) expected cell, found compiled_cell

Note: annotationlib.get_annotations(C, format=Format.FORWARDREF) on the same class works when compiled; the failure is specific to call_annotate_function, which rebuilds the function from its code and closure.