use-types-from-typing

Enforces the use of types from the typing module in type annotations in place of builtins.{builtin_type} since the type system doesn’t recognize the latter as a valid type before Python 3.9.

Message

You are using builtins.{builtin_type} as a type annotation but the type system doesn’t recognize it as a valid type. Use typing.{correct_type} instead.

Valid examples

def function(list: List[str]) -> None:
    pass
def function() -> None:
    thing: Dict[str, str] = {}
def function() -> None:
    thing: Tuple[str]
Show more
from typing import Dict, List
def function() -> bool:
        return Dict == List
from typing import List as list
from graphene import List

def function(a: list[int]) -> List[int]:
        return []
from builtins import list as ListType

LT = ListType

class LT:
    def __class_getitem__(cls, item):
        return cls

def func(value: LT[str]) -> None:
    pass

Invalid examples

from typing import List
def whatever(list: list[str]) -> None:
    pass

Suggested fix

from typing import List
def whatever(list: List[str]) -> None:
    pass
Show more
def function(list: list[str]) -> None:
    pass
def func() -> None:
    thing: dict[str, str] = {}
def func() -> None:
    thing: tuple[str]
from typing import Dict
def func() -> None:
    thing: dict[str, str] = {}

Suggested fix

from typing import Dict
def func() -> None:
    thing: Dict[str, str] = {}
from builtins import list as ListType

def func(value: ListType[str]) -> None:
    pass
from builtins import list as ListType

LT = ListType

def func(value: LT[str]) -> None:
    pass
from builtins import list as ListType

LT = OtherLT = ListType

def func(value: OtherLT[str]) -> None:
    pass