-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Add py:type directive and role for documenting type aliases #11989
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
Conversation
|
I don't mind the use of |
|
I wasn't planning on implementing it in autodoc myself because my use case doesn't require it. But I can implement it if preferred. Either way I figured it belonged in a separate pull request? |
|
Yes it would. I think we can delay the autodoc implementation for now since I want to hear from other maintainers the direction we'll take for improving autodoc. |
@picnixz, if you have time, it would be great if you could open a discussion, similarish to #12152 😄; Obviously from I have some thoughts on this I could add (e.g. https://github.com/sphinx-extensions2/sphinx-autodoc2?tab=readme-ov-file#design-and-comparison-to-sphinx-autoapi), but would be good to baseline the discussion first |
I'll try to find time for that tomorrow. And actually, tomorrow I'll do more a review thing rather than coding (#12219 is still a draft but it could be reviewed I think? I tried to have as much coverage as possible since this is something you want to use in tests). I'll also review this PR since I said that I would do it last month... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some comments (and sorry for the dealy, and thank your for your contribution)
| .. rst:directive:: .. py:type:: name | ||
| Describes a :ref:`type alias <python:type-aliases>`. A description of | ||
| the type alias, such as the docstring can be, placed in the body of | ||
| the directive. | ||
|
|
||
| .. versionadded:: 7.3 | ||
|
|
||
| .. rubric:: options | ||
|
|
||
| .. rst:directive:option:: type: the type that the alias represents | ||
| :type: text | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| .. rst:directive:: .. py:type:: name | |
| Describes a :ref:`type alias <python:type-aliases>`. A description of | |
| the type alias, such as the docstring can be, placed in the body of | |
| the directive. | |
| .. versionadded:: 7.3 | |
| .. rubric:: options | |
| .. rst:directive:option:: type: the type that the alias represents | |
| :type: text | |
| .. rst:directive:: .. py:type:: name | |
| Describe a :ref:`type alias <python:type-aliases>`. | |
| This directive supports an optional description body, e.g.:: | |
| .. code-block:: rst | |
| .. py:type:: UInt64 | |
| Represent a 64-bit positive integer. | |
| .. rubric:: options | |
| .. rst:directive:option:: canonical | |
| :type: text | |
| The canonical type represented by this alias, e.g.:: | |
| .. code-block:: rst | |
| .. py:type:: StrPattern | |
| :canonical: str | re.Pattern[str] | |
| Represent a regular expression or a compiled pattern. | |
| .. versionadded:: 7.3 |
I suggested canonical because type is a bit weird in the sense it's not the type of the type but rather the underlying type. In addition, I am wondering whether the aliased type should be the fully-qualified name (in which case we wouldn't have any issues with references I think and we could have "clickable" items) or if it's simply a text (I'll probably comment below).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would the type aliases be considered as clickable items in base classes / type annotations, etc and so? (i.e., do they behave similarly to a class?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, they would be.
042ed21 to
9579c8a
Compare
|
Thanks @AWhetter! A |
Feature or Bugfix
Purpose
This pull request adds a
py:typedirective for documenting type aliases (https://typing.readthedocs.io/en/latest/spec/aliases.html#type-aliases), and apy:typerole for linking to them.Detail
I've chosen to make a new directive for documenting type aliases, rather than add a new option to the
dataorattributedirectives, because not all of the options on thedataandattributedirectives apply to a type alias (eg.type). In addition, type aliases seem to be classified differently from variable assignments by Python. Python documentation treats type aliases almost like first class citizens.typein their code, but they're documenting it using the workaround of documenting it as an assignment (eg... py:data:: Url\n :type: TypeAlias\n :value: str), the user will likely want to update their documentation to use this new directive. I'm assuming that the number of users usingtypein their code is quite low, and therefore this trade off seemed worth it.I've chosen to name the directive and role "type" because that's also the keyword used to define a type alias in Python. I considered using "typealias", "alias", or some thing similar, but it seemed safer to choose the same word used in Python syntax because CPython may change the usage of the
typekeyword in the future to define more than just type aliases.:type:field, which potentially makes understanding rST syntax more confusing for users who are learning rST for the first time. But this trade-off seemed worth it.I've chosen to have users specify the type being aliased in a
:value:option, rather than in the signature (eg... py:type:: MyAlias = int), because this is consistent with how thedataandattributedirectives work. So I think it makes the syntax more intuitive for users.I've chosen to make specifying a value of the alias optional because there could be cases where a user wants to declare the alias, but not make the type that is aliased public. For example:
In the above example, a user may want to allow users to pass this type between parts of the API, without letting the user do anything else with instance of
MyAlias, by not documenting whatMyAliasis aliased from. So the user would document this as follows:Users can currently do something similar with classes, where they might define a class but not document any of the attributes on it.
I've chosen to name the option "value" because this is consistent with the
dataandattributedirectives. In addition, this is the terminology used intyping.TypeAliasTypeand in the ast node.Relates