Skip to content

Typing stubs not being analyzed with deferred type evaluation #14354

Description

@ktbarrett

Describe the bug

In typing stubs, all type evaluation is deferred, but Sphinx seems to load typing stubs as if they were regular Python modules and then chokes when there are forward references.

WARNING: autodoc: failed to import 'types' from module 'coconext'; the following exception was raised:
Traceback (most recent call last):
  File "/home/ktbarrett/dev/coconext/.venv/lib/python3.12/site-packages/sphinx/ext/autodoc/_dynamic/_importer.py", line 227, in _import_module
    spec.loader.exec_module(module)
  File "<frozen importlib._bootstrap_external>", line 995, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "/home/ktbarrett/dev/coconext/.venv/lib/python3.12/site-packages/coconext/types.pyi", line 17, in <module>
    class Logic:
  File "/home/ktbarrett/dev/coconext/.venv/lib/python3.12/site-packages/coconext/types.pyi", line 19, in Logic
    def __init__(self, arg: Logic) -> None: ...
                            ^^^^^
NameError: name 'Logic' is not defined

This was fixed by placing a from __future__ import annotations at the top of the offending file; however, this is redundant as typing stubs implicitly behave in this way.

The stub causing offense was generated by nanobind. I don't want to have to maintain some fixup script for this, so there's no current workaround for me.

How to Reproduce

git clone https://github.com/ktbarrett/coconext.git@438b84a9e02ffc69144ee04bc00ef35ce093dc9c
cd coconext
uv venv
source .venv/bin/activate
make docs

Environment Information

Python 3.12
Sphinx 9.1.0

Sphinx extensions

"sphinx.ext.autodoc"

Additional context

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions