Skip to content

Commit 8338e9b

Browse files
committed
docs: Document limitation on forward references in base classes
Issue-mkdocstrings#586: mkdocstrings/mkdocstrings#586
1 parent c16c09d commit 8338e9b

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

docs/guide/users/recommendations/python-code.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,3 +371,44 @@ a.__b._c == __a.b.c
371371
```
372372

373373
When the qualified name of the object's parent module and the currently inspected module match like above, the object is inspected in-place (added as a member of the currently inspected module) instead of created as an alias.
374+
375+
## Avoid forward references in base classes
376+
377+
Python's type system will let you use forward references in generic types when they are used as base classes. For example:
378+
379+
=== "Before Python 3.12"
380+
381+
```python
382+
from typing import TypeVar, Generic
383+
384+
T = TypeVar('T')
385+
386+
387+
class Foo(Generic[T]):
388+
...
389+
390+
391+
class FooBar(Foo['Bar']):
392+
...
393+
394+
395+
class Bar:
396+
...
397+
```
398+
399+
=== "Python 3.12+"
400+
401+
```python
402+
class Foo[T]:
403+
...
404+
405+
406+
class FooBar(Foo['Bar']):
407+
...
408+
409+
410+
class Bar:
411+
...
412+
```
413+
414+
While Griffe will load this code without error, the `'Bar'` forward reference won't be resolved to the actual `Bar` class. As a consequence, downstream tools like documentation renderers won't be able to output a link to the `Bar` class. We therefore recommend to avoid using forward references in base classes, if possible.

0 commit comments

Comments
 (0)