NoStringTypeAnnotationRule

Enforce the use of type identifier instead of using string type hints for simplicity and better syntax highlighting. Starting in Python 3.7, from __future__ import annotations can postpone evaluation of type annotations PEP 563 and thus forward references no longer need to use string annotation style.

Message

String type hints are no longer necessary in Python, use the type identifier directly.

Has Autofix: Yes

VALID Code Examples

# 1:

from a.b import Class

def foo() -> Class:
    return Class()

# 2:

import typing
from a.b import Class

def foo() -> typing.Type[Class]:
    return Class

# 3:

import typing
from a.b import Class
from c import func

def foo() -> typing.Optional[typing.Type[Class]]:
    return Class if func() else None

# 4:

from a.b import Class

def foo(arg: Class) -> None:
    pass

foo(Class())

# 5:

from a.b import Class

module_var: Class = Class()

# 6:

from typing import Literal

def foo() -> Literal["a", "b"]:
    return "a"

# 7:

import typing

def foo() -> typing.Optional[typing.Literal["a", "b"]]:
    return "a"

INVALID Code Examples

# 1:

from __future__ import annotations

from a.b import Class

def foo() -> "Class":
    return Class()

Autofix:

---
+++
@@ -3,5 +3,5 @@

 from a.b import Class

-def foo() -> "Class":
+def foo() -> Class:
     return Class()

# 2:

from __future__ import annotations

from a.b import Class

async def foo() -> "Class":
    return await Class()

Autofix:

---
+++
@@ -3,5 +3,5 @@

 from a.b import Class

-async def foo() -> "Class":
+async def foo() -> Class:
     return await Class()

# 3:

from __future__ import annotations

import typing
from a.b import Class

def foo() -> typing.Type["Class"]:
    return Class

Autofix:

---
+++
@@ -4,5 +4,5 @@
 import typing
 from a.b import Class

-def foo() -> typing.Type["Class"]:
+def foo() -> typing.Type[Class]:
     return Class

# 4:

from __future__ import annotations

import typing
from a.b import Class
from c import func

def foo() -> Optional[typing.Type["Class"]]:
    return Class if func() else None

Autofix:

---
+++
@@ -5,5 +5,5 @@
 from a.b import Class
 from c import func

-def foo() -> Optional[typing.Type["Class"]]:
+def foo() -> Optional[typing.Type[Class]]:
     return Class if func() else None

# 5:

from __future__ import annotations

from a.b import Class

def foo(arg: "Class") -> None:
    pass

foo(Class())

Autofix:

---
+++
@@ -3,7 +3,7 @@

 from a.b import Class

-def foo(arg: "Class") -> None:
+def foo(arg: Class) -> None:
     pass

 foo(Class())

# 6:

from __future__ import annotations

from a.b import Class

module_var: "Class" = Class()

Autofix:

---
+++
@@ -3,4 +3,4 @@

 from a.b import Class

-module_var: "Class" = Class()
+module_var: Class = Class()

# 7:

from __future__ import annotations

import typing
from typing_extensions import Literal
from a.b import Class

def foo() -> typing.Tuple[Literal["a", "b"], "Class"]:
    return Class()

Autofix:

---
+++
@@ -5,5 +5,5 @@
 from typing_extensions import Literal
 from a.b import Class

-def foo() -> typing.Tuple[Literal["a", "b"], "Class"]:
+def foo() -> typing.Tuple[Literal["a", "b"], Class]:
     return Class()