Skip to content

Commit 6f22ad8

Browse files
committed
docs: Add solution to forward-ref in generic base
Issue-mkdocstrings#586: mkdocstrings/mkdocstrings#586
1 parent 88fb6b6 commit 6f22ad8

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,3 +412,18 @@ Python's type system will let you use forward references in generic types when t
412412
```
413413

414414
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.
415+
416+
Instead, you can try to declare or import the `Bar` class earlier, or make `FooBar` generic again but with a default type:
417+
418+
```python
419+
class Foo[T]:
420+
...
421+
422+
423+
class FooBar[T=Bar](Foo[T]):
424+
...
425+
426+
427+
class Bar:
428+
...
429+
```

0 commit comments

Comments
 (0)