Skip to content

Commit

Permalink
docs: Add solution to forward-ref in generic base
Browse files Browse the repository at this point in the history
Issue-mkdocstrings#586: mkdocstrings/mkdocstrings#586
  • Loading branch information
pawamoy committed Nov 25, 2024
1 parent 88fb6b6 commit 6f22ad8
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions docs/guide/users/recommendations/python-code.md
Original file line number Diff line number Diff line change
Expand Up @@ -412,3 +412,18 @@ Python's type system will let you use forward references in generic types when t
```

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.

Instead, you can try to declare or import the `Bar` class earlier, or make `FooBar` generic again but with a default type:

```python
class Foo[T]:
...


class FooBar[T=Bar](Foo[T]):
...


class Bar:
...
```

0 comments on commit 6f22ad8

Please sign in to comment.