Skip to content

Commit 1e2fcad

Browse files
committed
docs: Add type-based solution to forward-ref in generic base
1 parent 6f22ad8 commit 1e2fcad

1 file changed

Lines changed: 30 additions & 9 deletions

File tree

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

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -413,17 +413,38 @@ Python's type system will let you use forward references in generic types when t
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.
415415

416-
Instead, you can try to declare or import the `Bar` class earlier, or make `FooBar` generic again but with a default type:
416+
Instead, you can try one of the following approach:
417417

418-
```python
419-
class Foo[T]:
420-
...
418+
- declare or import the `Bar` class earlier
419+
- declare a proper type:
421420

421+
```python
422+
class Foo[T]:
423+
...
422424

423-
class FooBar[T=Bar](Foo[T]):
424-
...
425425

426+
type TBar = Bar
426427

427-
class Bar:
428-
...
429-
```
428+
429+
class FooBar(Foo[TBar]):
430+
...
431+
432+
433+
class Bar:
434+
...
435+
```
436+
437+
- make `FooBar` generic again but with a default type:
438+
439+
```python
440+
class Foo[T]:
441+
...
442+
443+
444+
class FooBar[T=Bar](Foo[T]):
445+
...
446+
447+
448+
class Bar:
449+
...
450+
```

0 commit comments

Comments
 (0)