# PEP 846: Docstrings for Type Aliases

**URL:** <https://discuss.python.org/t/pep-846-docstrings-for-type-aliases/109116>\
**Category:** PEPs\
**Created:** [September 18, 2026, 5:46pm UTC](https://discuss.python.org/t/pep-846-docstrings-for-type-aliases/109116 "2026-09-18T17:46:00Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![bswck](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/bswck/32/34363_2.png) [@bswck](https://discuss.python.org/u/bswck)\
**Post date:** [September 18, 2026, 5:46pm UTC](https://discuss.python.org/t/pep-846-docstrings-for-type-aliases/109116/1 "2026-09-18T17:46:00Z")

</div>

I am pleased to present PEP 846, a well-received idea to recognize docstrings of type aliases in Python.

What I like the most about it is that it looks like an easy win for the language.

It’s focused, it has support in most relevant tools, and it’s a well-established practice among many Python programmers.

#### Abstract

This PEP proposes preserving a string literal immediately following a [`type`](https://docs.python.org/3/reference/simple_stmts.html#type "(in Python v3.14)") statement as the resulting type alias object’s ` __doc__ ` attribute, exposing that documentation through [`ast`](https://docs.python.org/3/library/ast.html#module-ast "(in Python v3.14)"), and displaying it through [`pydoc`](https://docs.python.org/3/library/pydoc.html#module-pydoc "(in Python v3.14)") and [`help()`](https://docs.python.org/3/builtins/functions.html#help "(in Python v3.14)"). It follows the placement already supported by source-based documentation tools. The parser stores the docstring in a new optional `doc` field on [`ast.TypeAlias`](https://docs.python.org/3/library/ast.html#ast.TypeAlias "(in Python v3.14)") instead of creating a separate [`ast.Expr`](https://docs.python.org/3/library/ast.html#ast.Expr "(in Python v3.14)") node for it. [`ast.get_docstring()`](https://docs.python.org/3/library/ast.html#ast.get_docstring "(in Python v3.14)") retrieves alias docstrings from this field.

#### Full PEP

> **[PEP 846 – Docstrings for Type Aliases | peps.python.org](https://peps.python.org/pep-0846/)**
>
> This PEP proposes preserving a string literal immediately following a type statement as the resulting type alias object’s \_\_doc\_\_ attribute, exposing that documentation through ast, and displaying it through pydoc and help(). It follows the placement...

#### Previous idea discussion

[https://discuss.python.org/t/docstrings-for-type-aliases](https://discuss.python.org/t/docstrings-for-type-aliases) ( **please see this for mentions of a more general “variable docstrings” problem and why this PEP does not address it!** )

#### Play with this

> **[GitHub - johnslavik/cpython at gh-156925](https://github.com/johnslavik/cpython/tree/gh-156925)**
>
> gh-156925

Run with [`every-python`](https://github.com/savannahostrowski/every-python):

```bash
uvx every-python run --repo johnslavik/cpython gh-156925 -- python

```

---

<div class="post-metadata">

**Author:** ![Dutcho](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/dutcho/32/7074_2.png) [@Dutcho](https://discuss.python.org/u/Dutcho)\
**Post date:** [September 18, 2026, 7:48pm UTC](https://discuss.python.org/t/pep-846-docstrings-for-type-aliases/109116/2 "2026-09-18T19:48:24Z")

</div>

Nice.

I see you chose to put _doctest_ support out of scope.  
Please document in _rejected ideas_ including motivation. And perhaps in _how to teach_, that this is an exception to the rule that _docstring_s can have _doctest_s.  
(Or -my preference- make it in scope ☺)

---

<div class="post-metadata">

**Author:** ![mjmikulski](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mjmikulski/32/37517_2.png) [@mjmikulski](https://discuss.python.org/u/mjmikulski)\
**Post date:** [September 18, 2026, 9:15pm UTC](https://discuss.python.org/t/pep-846-docstrings-for-type-aliases/109116/3 "2026-09-18T21:15:17Z")

</div>

Docstrings for type aliases are useful and worth supporting 💪

PEP 846 reminded me of an old idea: class-like syntax for union type aliases. With that syntax, instead of attaching the following string to a `type` statement, a type alias could optionally have a class-like block with the familiar docstring placement:

```python
type Timeout:
    """Maximum wait in seconds."""
    float
    None

```

with multiple expressions in the block forming a union.

I posted the idea separately here to avoid derailing this PEP discussion:

> [@Class-like syntax for union type aliases and natural docstrings](https://discuss.python.org/t/class-like-syntax-for-union-type-aliases-and-natural-docstrings/109103):
>
> In many type aliases, a union is the outermost operation. The recent PEP 846 (docstrings for type aliases) reminded me of an old idea of mine. That PEP proposes to attach a string statement to the preceding type statement, because the current type statement (unlike class) has no natural place for a docstring. I propose the following class-like syntax that takes the union of all its members. For example, instead of: type Timeout = int | float | None """Maximum wait in seconds; None waits indef…

---

<div class="post-metadata">

**Author:** ![jorenham](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jorenham/32/27806_2.png) [@jorenham](https://discuss.python.org/u/jorenham)\
**Post date:** [September 18, 2026, 10:50pm UTC](https://discuss.python.org/t/pep-846-docstrings-for-type-aliases/109116/4 "2026-09-18T22:50:14Z")

</div>

Thanks, this seems very useful, and I’d love to be able to apply this to the `numpy.typing` type aliases 🙂 .

When reading the “Rejected Ideas” section I had a bit of a déjà vu, which I then realized was the rejected [PEP 727](https://peps.python.org/pep-0727/) from a while back. It proposed a mechanism for documenting type annotations, and therefore also type aliases, using a new dedicated `typing.Doc` class as `typing.Annotated` metadata.  
And since PEP 846 already talks about `Annotated` as a rejected idea, I thought it might also be a good idea to mention PEP 727.

---

<div class="post-metadata">

**Author:** ![pawamoy](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/pawamoy/32/5811_2.png) [@pawamoy](https://discuss.python.org/u/pawamoy)\
**Post date:** [September 19, 2026, 9:08am UTC](https://discuss.python.org/t/pep-846-docstrings-for-type-aliases/109116/5 "2026-09-19T09:08:45Z")

</div>

Congrats @bswck!

> Tools that find alias docstrings by looking at the following statement, or that rely on the alias node’s end position, need adjusting when parsing with Python 3.16.

Yep, confirmed:

```python
# pkg.py
type T = int | None
"""Hello."""

```

(assuming Griffe is installed in respective envs)

```console
% python -c 'import griffe; print(griffe.load("pkg")["T"].docstring)'
<griffe._internal.models.Docstring object at 0x7f71caae4ad0>

```

```console
% every-python run gh-156925 --repo johnslavik/cpython -- python -c 'import griffe; print(griffe.load("pkg")["T"].docstring)'
None

```

Added to the backlog 🙂

---

<div class="post-metadata">

**Author:** ![bswck](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/bswck/32/34363_2.png) [@bswck](https://discuss.python.org/u/bswck)\
**Post date:** [September 22, 2026, 1:48pm UTC](https://discuss.python.org/t/pep-846-docstrings-for-type-aliases/109116/6 "2026-09-22T13:48:39Z")

</div>

> [@Dutcho](#):
>
> I see you chose to put _doctest_ support out of scope.  
> Please document in _rejected ideas_ including motivation. And perhaps in _how to teach_, that this is an exception to the rule that _docstring_s can have _doctest_s.  
> (Or -my preference- make it in scope ☺)

I’ve included it in the scope. The update of the PEP should land soon.

---

<div class="post-metadata">

**Author:** ![beauxq](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/beauxq/32/15042_2.png) [@beauxq](https://discuss.python.org/u/beauxq)\
**Post date:** [September 22, 2026, 9:14pm UTC](https://discuss.python.org/t/pep-846-docstrings-for-type-aliases/109116/7 "2026-09-22T21:14:54Z")

</div>

+0 on the change.

> This PEP has no known security implications.

There are tools that automatically look for code in docstrings and run it as tests.

I’m imagining some freak accident where someone puts some code that should never be run in a string in a place where they don’t expect it to be run because they weren’t aware of this change…  
Maybe that string with the bad code has been there for decades and it was never run until this change was made…

I don’t know if it’s worth talking about.

---

<div class="post-metadata">

**Author:** ![brettcannon](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/brettcannon/32/34895_2.png) [@brettcannon](https://discuss.python.org/u/brettcannon)\
**Post date:** [October 26, 2026, 10:48pm UTC](https://discuss.python.org/t/pep-846-docstrings-for-type-aliases/109116/8 "2026-10-26T22:48:05Z")

</div>

> [@beauxq](#):
>
> I don’t know if it’s worth talking about.

No because that is not specific to this PEP.

---

<div class="post-metadata">

**Author:** ![carljm](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/carljm/32/959_2.png) [@carljm](https://discuss.python.org/u/carljm)\
**Post date:** [October 30, 2026, 9:07pm UTC](https://discuss.python.org/t/pep-846-docstrings-for-type-aliases/109116/9 "2026-10-30T21:07:17Z")

</div>

Thanks for this PEP! I think this is useful.

I’m curious about the rationale for not allowing `type A = int; "foo"` to be considered a docstring. Today this is equivalent to the two-line version with no semicolon, and code formatters (at least Ruff and Black) will rewrite the former to the latter. After this PEP, that rewrite will become unsafe; the semicolon will become meaningful. Maybe this is fine, but it means this PEP will require an update in formatters; that should probably be mentioned in the PEP.

Similarly, it seems strange to me that in the prototype implementation, a trailing semicolon disables the next line from being a docstring:

```py
type A = int;
"not a docstring, because of the trailing semicolon above"

```

I would prefer to define this simply in terms of the next statement in the same block, without reference to formatting details like semicolons. That would imply that both `type A = int; "foo"` and the previous example would define a docstring.

Relatedly, I think there are risks involved with the choice to handle this in the parser/grammar rather than in the AST, and I don’t think the current PEP text adequately motivates this choice.

The prototype implementation can change the semantics of code at runtime by wrongly considering subsequent code to be inside a block:

```py
if False: type A = int
"docstring"; print("runs")

```

Today this code prints `runs`; the print is rightly considered to be outside the `if False` block. The prototype implementation prints nothing: the `type` statement eats the newline, leading the print to be wrongly considered part of the block.

It can also allow odd-looking code that is currently invalid to be accepted:

```py
if False: type A = int
"outside"
else: print(42)

```

In general, I think it is awkward to make a newline part of the grammar of a statement in a whitespace-delimited language. I think it would be better to return to the original implementation, which performed this transformation in `_PyAST_Preprocess`. The PEP (rightly IMO) rejects keeping the docstring in the AST twice, but that doesn’t rule out moving the `Constant` node into a field of the type-alias node and removing it from the outer statement list. (I realize this would mean adding an extra flag to `_PyAST_Preprocess` so it could avoid doing this transformation on user-supplied AST in `compile()`, as mentioned in the PEP – but I think the consequences of moving it into the parser are worse than this.)

---

<div class="post-metadata">

**Author:** ![guido](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/guido/32/21_2.png) [@guido](https://discuss.python.org/u/guido)\
**Post date:** [November 6, 2026, 12:49am UTC](https://discuss.python.org/t/pep-846-docstrings-for-type-aliases/109116/10 "2026-11-06T00:49:06Z")

</div>

> [@carljm](#):
>
> I think it is awkward to make a newline part of the grammar of a statement in a whitespace-delimited language

Oh! Thanks for catching this. Clearly my suggestion was naive.

I still think it’s better to make this case explicit in the grammar; the original idea of a separate pass searching through the AST feels unsystematic. A fix could be to allow only “short type aliases” (i.e., without docstring) in `simple_stmts` but allow short or long type aliases in `statements`. Could allow a type alias followed by a semicolon and a docstring on the same line? Probably that opens a bigger can of worms.

Honestly, any grammar-based solution that doesn’t get fooled by your example is fine with me.

---

<div class="post-metadata">

**Author:** ![carljm](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/carljm/32/959_2.png) [@carljm](https://discuss.python.org/u/carljm)\
**Post date:** [November 6, 2026, 8:19pm UTC](https://discuss.python.org/t/pep-846-docstrings-for-type-aliases/109116/12 "2026-11-06T20:19:11Z")

</div>

> [@guido](#):
>
> The original idea of a separate pass searching through the AST feels unsystematic.

Can you say more about this objection? I’m not sure that I really understand it. To me it seems that doing this on the AST allows us to define the behavior much more simply and trivially take advantage of “normal” handling of newlines, semicolons, and blocks, which will allow us to easily handle all these cases correctly and match user intuition without requiring a lot of fiddly special-casing in the parser. It’s not really a separate pass because we already have `_PyAST_Preprocess`. It’s also more parallel to how docstrings on classes and functions are handled today: they are recognized in the AST, not in the parser.

---

<div class="post-metadata">

**Author:** ![guido](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/guido/32/21_2.png) [@guido](https://discuss.python.org/u/guido)\
**Post date:** [November 7, 2026, 12:53am UTC](https://discuss.python.org/t/pep-846-docstrings-for-type-aliases/109116/13 "2026-11-07T00:53:18Z")

</div>

I was going to argue that anything that _can_ reasonably be expressed in the grammar _should_ be implemented that way. (Where I draw the line at things like checking that `return` only occurs inside functions, and similarly for `yield` and `await` inside generators / async functions.)

But realizing that docstrings in general are already handled the way you are proposing makes me withdraw my objection.

Go ahead and do it the old way.

We should at least mention in the PEP (if it’s not already specified) whether the following example is type alias with or without a docstring:

```py
type Id = int; "Identifier (int under the hood)"

```
