-
Notifications
You must be signed in to change notification settings - Fork 16
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
✨ Add support for doc()
#49
Conversation
Codecov Report
❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more. @@ Coverage Diff @@
## main #49 +/- ##
==========================================
+ Coverage 94.50% 94.76% +0.26%
==========================================
Files 2 2
Lines 200 210 +10
Branches 29 30 +1
==========================================
+ Hits 189 199 +10
Misses 9 9
Partials 2 2
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
I think there's some discussion to be had around the proposal in general, but I don't see any reason not to include it in annotated-types for now. As far as this project is concerned, could you clarify what implementers are supposed to do if someone does something like: # Is doc() here meaningless or valid? There is no parameter (no name, etc.)
Foo = Annotated[str, doc(...)]
# What about nesting it? does it get "pulled out" to the outermost parameter?
def foo(b: int | list[Annotated[float, doc(...)]]) -> None: ...
# How should we handle duplications? Unlike constraints, there can only be 1 docstring generally
def foo(b: Annotated[str, doc("abc"), doc("xyz")] -> None: ... |
Good questions! Actually, let me add notes about them to my doc and copy them here as well. |
Great, I'm keen 👍. LGTM. |
Should we support this in pydantic to fill JSON Schema |
Good points @adriangb! (I would love your feedback on the whole document too if you had the chance). I added a whole new section to the doc answering @adriangb's comments. In short, implementers would not be required to support any of the edge cases to be considered conformant. Nevertheless, I added them all (plus a couple more) with the optional behavior an implementer could adopt for each use case, in particular, what would it mean and what would it refer to. I'm copying it here just in case: Additional ScenariosThe main scenarios that this proposal intends to cover are described above, and for implementers to be conformant to this specification, they only need to support those scenarios described above. Here are some additional edge case scenarios with their respective considerations, but implementers are not required to support them. Type AliasWhen creating a type alias, like: Username = Annotated[str, doc("The name of a user in the system")] ...the documentation would be considered to be carried by the parameter annotated with So, in a function like: def hi(to: Username) -> None: ... ...it would be equivalent to: def hi(to: Annotated[str, doc("The name of a user in the system")]) -> None: ... Nevertheless, implementers would not be required to support type aliases outside of the final type annotation to be conformant with this specification, as it could require more complex derefrenecing logic. Annotating Type ParametersWhen annotating type parameters, as in: def hi(to: list[Annotated[str, doc("The name of a user in a list")]]) -> None: ... ...the documentation in There are currently no practical use cases for documenting type parameters, so implementers are not required to support this scenario to be considered conformant, but it's included for completeness. Annotating UnionsIf used in one of the parameters of a union, as in: def hi(to: str | Annotated[list[str, doc("List of user names")]]) -> None: ... ...again, the documentation in In particular, the documentation would not refer to a single string passed as a parameter, only to a list. There are currently no practical use cases for documenting unions, so implementers are not required to support this scenario to be considered conformant, but it's included for completeness. Multiple
|
I think so! I had also thought of that. It would also make sense for Typer... the only thing I have that (for now) is not based on Pydantic. 😅 |
I’d recommend the opposite: pick the right most one. This allows overriding because: Foo = Annotated[int, 1]
assert Annotated[Foo, 2] == Annotated[int, 1 , 2] |
I think so. We still need to do pydantic/pydantic#5884 this would play well with that. |
You're right! I just updated my doc to recommend that, and also the comment here. |
✨ Add support for
doc()
, for the doc proposal to add annotations inAnnotated
for parameters and others: https://github.com/tiangolo/fastapi/blob/typing-doc/typing_doc.mdIf this ends up here, I would update the doc to make this the alternate form (instead of the local stub code).
This would ideally end up in
typing_extensions
, a PEP, and ultimatelytyping
. But having it here would make it much easier for early adopters like FastAPI, Typer, SQLModel, Asyncer... possibly Pydantic, Strawberry, and maybe others.