Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeVars imported from another module lose their docstring #13276

Open
coretl opened this issue Jan 29, 2025 · 1 comment · May be fixed by #13277
Open

TypeVars imported from another module lose their docstring #13276

coretl opened this issue Jan 29, 2025 · 1 comment · May be fixed by #13277
Labels

Comments

@coretl
Copy link
Contributor

coretl commented Jan 29, 2025

Describe the bug

If you create your TypeVars in a private module, then re-export them via a public module, they lose their type comments when documented via an automodule directive.

In the reproducer below, V appears in the output, while T does not.

I will submit a PR fixing this with the reproducer as a unit test.

How to Reproduce

mymodule/_private.py:

from typing import TypeVar

T = TypeVar("T", bound=int | str)
"""A reimported TypeVar"""

mymodule/__init__.py:

from ._private import T

from typing import TypeVar

V = TypeVar("V")
"""A locally defined TypeVar"""

__all__ = ["T", "V"]

index.rst:

.. automodule:: mymodule
    :members:

Environment Information

Platform:              linux; (Linux-4.18.0-513.24.1.el8_9.x86_64-x86_64-with-glibc2.36)
Python version:        3.11.11 (main, Dec  4 2024, 20:38:25) [GCC 12.2.0])
Python implementation: CPython
Sphinx version:        7.3.7
Docutils version:      0.21.2
Jinja2 version:        3.1.5
Pygments version:      2.19.1

Sphinx extensions

`["sphinx.ext.autodoc"]`

Additional context

No response

coretl added a commit to coretl/sphinx that referenced this issue Jan 29, 2025
coretl added a commit to coretl/sphinx that referenced this issue Jan 29, 2025
coretl added a commit to coretl/sphinx that referenced this issue Jan 29, 2025
@coretl
Copy link
Contributor Author

coretl commented Jan 29, 2025

Unfortunately I was not able to fix the entire bug, only half of it.

My workaround until this is fixed:
mymodule/_private.py:

from typing import TypeVar

T = TypeVar("T", bound=int | str)

mymodule/__init__.py:

from typing import TypeVar

from ._private import T

# Workaround just for sphinx...
T = T
"""A reimported TypeVar"""

V = TypeVar("V")
"""A locally defined TypeVar"""

__all__ = ["T", "V"]

conf.py

import mymodule
mymodule.T.__module__ = "mymodule.T"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant