I ask for your guidance about how to proceed.
As I previously mentioned, my recommendation is to abandon the use of Annotated
for documentation and build on the existing docstring mechanisms that are supported by existing tools.
Here’s another idea that addresses most (but perhaps not all) of the issues you’re trying to solve for FastAPI. You could stick with one of the three standard function docstring formats (all of which are already supported by existing tools) and then write a simple CI script that validates that the docstrings are consistent with the parameters in your function signatures. This is similar to what I suggested but without the added work of writing a spec or publishing a library. You could do this entirely within your own CI/build system with relatively little work.
I’m not sure who would be willing to put the effort to try to bring consensus among the several current conventions…
I think you may be overestimating the work involved in documenting, standardizing, and parsing one of the existing formats, but I understand if you’re not interested in championing such an effort.
For what it’s worth, I suspect that you would receive quite a bit of community help if you were to pursue this approach.
I added the features I’m interested in to the PEP…
Yes, I saw those, but I didn’t think they were compelling.
- Editing & Rendering: These are already supported by all popular IDEs and language servers for existing docstring formats. The proposed PEP doesn’t add anything here other than a redundant mechanism.
- Deduplication and elimination of inconsistencies: As I suggested in my alternative proposal, there are other (arguably better) ways to address this through tooling, and this would benefit the thousands of library maintainers and millions of developers who have already invested in existing docstring formats.
- Reuse of documentation using type aliases: As I’ve said, the latest draft of the PEP is very problematic and inconsistent in how it’s treating type aliases. If you were to fix this (what I think is a) significant design flaw, the proposal would no longer address this problem.
- Access to documentation at runtime: This is also addressed by my alternative proposal.
- No microsyntax to learn for newcomers: The proposed PEP requires newcomers to learn yet another way to specify documentation — one that differs from existing conventions. The microsyntax rules are not onerous to learn, and AI-powered copilots already know how to generate them. As mentioned in my alternative proposal, tooling could further simplify this through automation and validation.
- Parameter documentation for
ParamSpec
: This is already handled today with traditional function docstrings. The proposed PEP adds no new value here.
I presume that only a subset of these are problems that you personally face as the maintainer of FastAPI. For example, the “microsyntax” issue is presumably not a problem for you. It would be useful to understand which of these problems represent pain points for you personally. That might help us find a solution that meets your needs. I’m guessing that you’re primarily focused on “deduplication and elimination of inconsistencies”?
I see that Pyright now supports some non-standard formats (e.g. strings under variable names)
Pyright has long treated string literals that appear immediately after an attribute or type alias declaration as a docstring for that symbol. Other IDEs do this as well.
Attribute docstrings are discussed in PEP 257. It says String literals occurring immediately after a simple assignment at the top level of a module, class, or __init__
method are called “attribute docstrings”. PEP 257 was written prior to the introduction of type aliases, but I think it’s reasonable for tools to treat type aliases consistently in this regard.
It’s important to understand that attribute and type alias docstrings rely only on constructs that are built in to the language. They have no dependency on third-party libraries or their (non-standardized) behaviors. Pyright has no knowledge of any third-party libraries or their behaviors beyond the type information provided by them. That’s a hard-and-fast rule that I’m unwilling to violate — for the same reason that it would be completely inappropriate for the TypeScript compiler to have intrinsic knowledge of certain third-party libraries.
Third-party libraries do not go through the same level of design scrutiny that stdlib does, they are not guaranteed to retain consistent semantics over time, they are not version-locked with Python releases, and they do not have the same backward compatibility guarantees as stdlib. If you want pyright to add support for a feature that requires knowledge of specific classes, these classes must be part of the stdlib, and their intended usage and semantics must be documented in the official Python documentation.
It’s possible that you could convince the pylance team to add support for an annotated-doc
library at the language server level. They’ve done this in a (very small) number of cases where there is demand voiced by many pylance users over a sustained time period. However, adding support at the pylance layer is less robust than something implemented at the type checker layer. For example, it wouldn’t be possible for pylance to retain parameter comments for decorated methods that use ParamSpec
because that requires the type checker to track the documentation across type transforms. I can’t speak for the pylance team, but given the process they use for prioritizing features, I think it’s unlikely that you could convince them to do this as an experimental feature. You could reach out to them and gauge their interest.
I appreciate that you are trying to address a real problem that you face in maintaining FastAPI. Unfortunately, I don’t think your proposal is aligned with what is best for the Python community.
Implementing your current proposal in FastAPI with the hopes that you can force tools vendors to support another non-standardized documentation mechanism is not the approach I would use. You might succeed in the long run, but you will create additional fragmentation and potentially generate ill will in the process.
I want to make it clear that the statements above are my own opinions. I’m not speaking for the recently-formed Typing Council, of which I’m a member. There may be other viewpoints among the other TC members. We haven’t had any discussions on this topic yet because it hasn’t been submitted to us for consideration.