sphinx
sphinx copied to clipboard
Fix autodoc for `typing.TypedDict` inheritance
Subject: enable listing inherited members of TypedDict subclasses
Feature or Bugfix
- Bugfix
Purpose
- fix inherited members not showing up for subclasses of
TypedDict
Detail
- Workarounds the problem of MRO not including parent classes for
TypedDict(there does not seem to be a clear reason for that as for this BDFL comment and subsequent comments - probably someone wanted to make the TypedDict as lightweight as possible, but it seems it was never documented) - Uses convenient fact that classes directly inheriting from
typing.TypedDictinclude__orig_bases__from PEP560. - unfortunately does not work with
typing_extensions.TypedDictwhich does not implement__orig_bases__as of now
Relates
- fixes #9290
I think __annotations__ are properly inherited for TypedDicts, but I don't remember when it was introduced (namely whether it's already available in 3.9 or not). Could you check for which python version the current code base actually works and for which versions a patch is needed?
I think
__annotations__are properly inherited for TypedDicts
@picnixz thank you for looking at my PR! Yes, annotations are properly inherited, but this is not the problem. In fact, this patch builds upon the fact that __annotations__ are properly inherited. Back in 2022 I tested this patch against Python 3.11.0rc1. I just rebased and tested by commenting out the patch - the test included in this PR still fails on 3.11 and it also fails on 3.12. I can manually test on older versions if you would like me to, but I am convinced the situation is the same.
thank you for looking at my PR! Yes, annotations are properly inherited, but this is not the problem
Ah my bad!
if you would like me to, but I am convinced the situation is the same.
No it's fine. However, when you mentioned the fact that we need an explicit TypedDict, would it be possible to use the 3.10 helper is_typeddict for 3.10 and later (possibly backporting it for 3.9 or using typing_extensions for this one (while the dependency is not explicit, it's included in mypy and I think most systems already have it (and we already assumed that it was installed actually even though it is not an explicit dependency))) and use a separate logic for TypedDicts? (your patch does not seem to be TypedDict specific so I prefer it to be only executed for TypedDict so that we don't have bad surprises if the MRO is poorly written).
would it be possible to use the 3.10 helper
is_typeddictfor 3.10 and later (possibly backporting it for 3.9 or usingtyping_extensions
This was done https://github.com/sphinx-doc/sphinx/pull/10806/commits/3ebd1abec4130982cced02c46b0062d34ea5deb1. I added a simple fallback for Python 3.9 rather than using typing_extensions, but if that does not seem desirably to you let me know and I can import from typing_extensions instead.
After looking at https://github.com/python/typing_extensions/blob/main/src/typing_extensions.py, I think it's better to import it from typing_extensions to be sure that it's robust.
That one is more complicated because typing_extensions re-implements TypedDict logic for Python < 3.8, but the patch from this PR would not work for that fallback implementation anyways.
After looking at python/typing_extensions@
main/src/typing_extensions.py, I think it's better to import it from typing_extensions to be sure that it's robust.That one is more complicated because
typing_extensionsre-implementsTypedDictlogic for Python < 3.8, but the patch from this PR would not work for that fallback implementation anyways.
Oh, then your implementation is fine I guess.