Use type identifiers instead of using string type hints.
Historically, string type annotations catered for the case where a function on line 5 returns an instance of a class that that is defined at line 7: how can type annotations on line 5 refer to a class that has not been defined yet?
Python 3.7 solved this problem by introducing postponing evaluation of type annotations until Python is ready to use them, removing the need to use string annotation styles.
Function annotations and variable annotations don't evaluate at function definition time. Instead, under the hood they are preserved in __annotations__
in string form.
This deferring allows forward references: type hints can contains names that have not been defined yet.
If our GitHub code review bot spots this issue in your pull request it gives this advice:
1 | + | def foo_bar() -> "FooBarClass": |
Use type identifiers instead of using string type hints.
Read more- | def foo_bar() -> "FooBarClass": | |||
+ | def foo_bar() -> FooBarClass: | |||
Expand 4 lines ... |
2 | + | return FooBarClass() | |
3 | + | ||
4 | + | class FooBarClass: | |
5 | + | pass |