Skip to content

Commit dc8c3ad

Browse files
viccie30pawamoy
andauthored
feat: Support PEP 695 generics
PR-221: #221 Co-authored-by: Timothée Mazzucotelli <dev@pawamoy.fr>
1 parent 53507b5 commit dc8c3ad

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1344
-48
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
- name: Setup Python
3333
uses: actions/setup-python@v5
3434
with:
35-
python-version: "3.12"
35+
python-version: "3.13"
3636

3737
- name: Setup uv
3838
uses: astral-sh/setup-uv@v5

docs/snippets/package/generics.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""Some module showing generics.
2+
3+
Type Aliases:
4+
SomeType: Some type alias.
5+
"""
6+
7+
type SomeType[Z] = int | list[Z]
8+
"""Some type alias.
9+
10+
Type parameters:
11+
Z: Some type parameter.
12+
"""
13+
14+
15+
class MagicBag[T: (str, bytes) = str](list[T]):
16+
"""A magic bag of items.
17+
18+
Type parameters:
19+
T: Some type.
20+
"""
21+
22+
def __init__[U: (int, bool)](self, *args: T, flag1: U | None = None, flag2: U | None = None) -> None:
23+
"""Initialize bag.
24+
25+
Type parameters:
26+
U: Some flag type.
27+
28+
Parameters:
29+
flag1: Some flag.
30+
flag2: Some flag.
31+
"""
32+
super().__init__(args)
33+
self.flag1 = flag1
34+
self.flag2 = flag2
35+
36+
def mutate[K](self, item: T, into: K) -> K:
37+
"""Shake the bag to mutate an item into something else (and eject it).
38+
39+
Type parameters:
40+
K: Some other type.
41+
42+
Parameters:
43+
item: The item to mutate.
44+
into: Mutate the item into something like this.
45+
46+
Returns:
47+
The mutated item.
48+
"""
49+
...

docs/usage/configuration/docstrings.md

Lines changed: 113 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ class ClassWithoutDocstring:
623623
- **:octicons-package-24: Type [`bool`][] :material-equal: `True`{ title="default value" }**
624624
<!-- - **:octicons-project-template-24: Template :material-null:** (N/A) -->
625625

626-
Whether to render the "Attributes" sections of docstrings.
626+
Whether to render the "Attributes" section of docstrings.
627627

628628
```yaml title="in mkdocs.yml (global configuration)"
629629
plugins:
@@ -676,7 +676,7 @@ class Class:
676676
- **:octicons-package-24: Type [`bool`][] :material-equal: `True`{ title="default value" }**
677677
<!-- - **:octicons-project-template-24: Template :material-null:** (N/A) -->
678678

679-
Whether to render the "Functions" or "Methods" sections of docstrings.
679+
Whether to render the "Functions" or "Methods" section of docstrings.
680680

681681
```yaml title="in mkdocs.yml (global configuration)"
682682
plugins:
@@ -751,7 +751,7 @@ class Class:
751751
- **:octicons-package-24: Type [`bool`][] :material-equal: `True`{ title="default value" }**
752752
<!-- - **:octicons-project-template-24: Template :material-null:** (N/A) -->
753753

754-
Whether to render the "Classes" sections of docstrings.
754+
Whether to render the "Classes" section of docstrings.
755755

756756
```yaml title="in mkdocs.yml (global configuration)"
757757
plugins:
@@ -804,13 +804,71 @@ class Class:
804804
////
805805
///
806806

807+
[](){#option-show_docstring_type_aliases}
808+
## `show_docstring_type_aliases`
809+
810+
- **:octicons-package-24: Type [`bool`][] :material-equal: `True`{ title="default value" }**
811+
812+
Whether to render the "Type Aliases" section of docstrings.
813+
814+
```yaml title="in mkdocs.yml (global configuration)"
815+
plugins:
816+
- mkdocstrings:
817+
handlers:
818+
python:
819+
options:
820+
show_docstring_type_aliases: true
821+
```
822+
823+
```md title="or in docs/some_page.md (local configuration)"
824+
::: path.to.module
825+
options:
826+
show_docstring_type_aliases: false
827+
```
828+
829+
```python
830+
"""Summary.
831+
832+
Type Aliases:
833+
TypeAlias: Some type alias.
834+
"""
835+
836+
837+
type TypeAlias = int
838+
"""Summary."""
839+
```
840+
841+
/// admonition | Preview
842+
type: preview
843+
844+
//// tab | With type_aliases
845+
<h2>module</h2>
846+
<p>Summary.</p>
847+
<p><b>Type Aliases:</b></p>
848+
849+
**Name** | **Description**
850+
------------ | ----------------
851+
`TypeAlias` | Some type alias.
852+
853+
<h3><code>TypeAlias</code></h3>
854+
<p>Summary.</p>
855+
////
856+
857+
//// tab | Without classes
858+
<h2>module</h2>
859+
<p>Summary.</p>
860+
<h3><code>TypeAlias</code></h3>
861+
<p>Summary.</p>
862+
////
863+
///
864+
807865
[](){#option-show_docstring_modules}
808866
## `show_docstring_modules`
809867

810868
- **:octicons-package-24: Type [`bool`][] :material-equal: `True`{ title="default value" }**
811869
<!-- - **:octicons-project-template-24: Template :material-null:** (N/A) -->
812870

813-
Whether to render the "Modules" sections of docstrings.
871+
Whether to render the "Modules" section of docstrings.
814872

815873
```yaml title="in mkdocs.yml (global configuration)"
816874
plugins:
@@ -1245,6 +1303,57 @@ def rand() -> int:
12451303
////
12461304
///
12471305

1306+
[](){#option-show_docstring_type_parameters}
1307+
## `show_docstring_type_parameters`
1308+
1309+
- **:octicons-package-24: Type [`bool`][] :material-equal: `True`{ title="default value" }**
1310+
<!-- - **:octicons-project-template-24: Template :material-null:** (N/A) -->
1311+
1312+
Whether to render the "Type Parameters" section of docstrings.
1313+
1314+
```yaml title="in mkdocs.yml (global configuration)"
1315+
plugins:
1316+
- mkdocstrings:
1317+
handlers:
1318+
python:
1319+
options:
1320+
show_docstring_type_parameters: true
1321+
```
1322+
1323+
```md title="or in docs/some_page.md (local configuration)"
1324+
::: path.to.module
1325+
options:
1326+
show_docstring_type_parameters: false
1327+
```
1328+
1329+
```python
1330+
class AClass[X: (int, str) = str]:
1331+
"""Represents something.
1332+
1333+
Type Parameters:
1334+
X: Something.
1335+
"""
1336+
```
1337+
1338+
/// admonition | Preview
1339+
type: preview
1340+
1341+
//// tab | With parameters
1342+
<h2><code>AClass</code></h2>
1343+
<p>Represents something.</p>
1344+
<p><b>Type Parameters:</b></p>
1345+
1346+
**Name** | **Bound or Constraints** | **Description** | **Default**
1347+
---------- | ------------------------ | --------------- | -----------
1348+
`whatever` | `(int, str)` | Something. | `str`
1349+
////
1350+
1351+
//// tab | Without parameters
1352+
<h2><code>AClass</code></h2>
1353+
<p>Represents something.</p>
1354+
////
1355+
///
1356+
12481357
[](){#option-show_docstring_warns}
12491358
## `show_docstring_warns`
12501359

docs/usage/configuration/headings.md

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,3 +682,131 @@ NOTE: **Use with/without `heading`.** If you use this option without specifying
682682
heading: "My fancy module"
683683
toc_label: "My fancy module"
684684
```
685+
686+
[](){#option-type_parameter_headings}
687+
## `type_parameter_headings`
688+
689+
- **:octicons-package-24: Type [`bool`][] :material-equal: `False`{ title="default value" }**
690+
691+
Whether to render headings for generic class, function/method and type alias
692+
type parameters.
693+
694+
With this option enabled, each type parameter of a generic object (including
695+
type parameters of `__init__` methods merged in their parent class with the
696+
[`merge_init_into_class`][] option) gets a permalink, an entry in the Table of
697+
Contents, and an entry in the generated objects inventory. The permalink and
698+
inventory entry allow cross-references from internal and external pages.
699+
700+
<!-- TODO: Update when https://github.com/mkdocstrings/griffe/issues/404 is solved.
701+
The identifier used in the permalink and inventory is of the following form:
702+
`path.to.function:type_param_name`. To manually cross-reference a parameter,
703+
you can therefore use this Markdown syntax:
704+
705+
```md
706+
- Class type parameter: [`Param`][package.module.Class[Param\]]
707+
- Method type parameter: [`Param`][package.module.Class.method[Param\]]
708+
- Function type parameter: [`Param`][package.module.function[Param\]]
709+
- Type alias type parameter: [`Param`][package.module.TypeAlias[Param\]]
710+
- Type variable tuple: [`*Args`][package.module.function[*Args\]]
711+
- Parameter specification: [`**Params`][package.module.function[**Params\]]
712+
```
713+
-->
714+
715+
Enabling this option along with [`signature_crossrefs`][] will automatically
716+
render cross-references to type parameters in class/function/method/type alias
717+
signatures.
718+
719+
```yaml title="in mkdocs.yml (global configuration)"
720+
plugins:
721+
- mkdocstrings:
722+
handlers:
723+
python:
724+
options:
725+
type_parameter_headings: false
726+
```
727+
728+
```md title="or in docs/some_page.md (local configuration)"
729+
::: path.to.module
730+
options:
731+
type_parameter_headings: true
732+
```
733+
734+
/// admonition | Preview: Cross-references
735+
type: preview
736+
737+
```md exec="on"
738+
::: package.generics
739+
options:
740+
show_root_heading: false
741+
heading_level: 3
742+
docstring_section_style: list
743+
show_bases: true
744+
summary: false
745+
separate_signature: true
746+
show_signature_type_parameters: true
747+
type_parameter_headings: true
748+
```
749+
750+
///
751+
752+
/// admonition | Preview: Type parameter sections
753+
type: preview
754+
755+
//// tab | Table style
756+
```md exec="on"
757+
::: package.generics.MagicBag
758+
options:
759+
members: false
760+
heading_level: 3
761+
show_root_heading: false
762+
show_root_toc_entry: false
763+
parameter_headings: true
764+
docstring_section_style: table
765+
show_docstring_description: false
766+
show_docstring_parameters: false
767+
show_docstring_returns: false
768+
```
769+
////
770+
771+
//// tab | List style
772+
```md exec="on"
773+
::: package.generics.MagicBag
774+
options:
775+
members: false
776+
heading_level: 3
777+
show_root_heading: false
778+
show_root_toc_entry: false
779+
parameter_headings: true
780+
docstring_section_style: list
781+
show_docstring_description: false
782+
show_docstring_parameters: false
783+
show_docstring_returns: false
784+
```
785+
////
786+
787+
//// tab | Spacy style
788+
```md exec="on"
789+
::: package.generics.MagicBag
790+
options:
791+
members: false
792+
heading_level: 3
793+
show_root_heading: false
794+
show_root_toc_entry: false
795+
parameter_headings: true
796+
docstring_section_style: spacy
797+
show_docstring_description: false
798+
show_docstring_parameters: false
799+
show_docstring_returns: false
800+
```
801+
////
802+
///
803+
804+
/// admonition | Preview: Table of contents (with symbol types)
805+
type: preview
806+
807+
<code class="doc-symbol doc-symbol-toc doc-symbol-function"></code> mutate<br>
808+
<code class="doc-symbol doc-symbol-toc doc-symbol-type_parameter" style="margin-left: 16px;"></code> U
809+
810+
To customize symbols, see [Customizing symbol types](../customization.md/#symbol-types).
811+
812+
///

docs/usage/configuration/members.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ package
585585
Whether to render summaries of modules, classes, functions (methods) and attributes.
586586

587587
This option accepts a boolean (`yes`, `true`, `no`, `false` in YAML)
588-
or a dictionary with one or more of the following keys: `attributes`, `functions`, `classes`, `modules`,
588+
or a dictionary with one or more of the following keys: `attributes`, `functions`, `classes`, `modules`, `type_aliases`,
589589
with booleans as values. Class methods summary is (de)activated with the `functions` key.
590590
By default, `summary` is false, and by extension all values are false.
591591

0 commit comments

Comments
 (0)