Use type identifiers instead of string type hints

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:

code-review-doctorbotsuggested changes just now
helpers.py
1
+
def foo_bar() -> "FooBarClass":
Suggested changes
-
def foo_bar() -> "FooBarClass":
+
def foo_bar() -> FooBarClass:
Expand 4 lines ...
Commit suggestion

Use type identifiers instead of using string type hints.

Read more
2
+
    return FooBarClass()
3
+
	
4
+
class FooBarClass:
5
+
    pass
Update helpers.py
We're your Python code review copilot. Get code improvements right in your pull request with our GitHub code review bot.

Configuring this check

Code Review Doctor will run this check by default. No configuration is needed but the check can be turned on/off using check code use-type-identifiers in your pyproject.toml file.

Read more about configuring Code Review Doctor.