.. _NoStringTypeAnnotationRule:

==========================
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 <https://www.python.org/dev/peps/pep-0563/#forward-references>`_
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:







.. code-block:: python


    from a.b import Class

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





# 2:







.. code-block:: python


    import typing
    from a.b import Class

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





# 3:







.. code-block:: python


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

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





.. container:: toggle


    # 4:







    .. code-block:: python


        from a.b import Class

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

        foo(Class())






    # 5:







    .. code-block:: python


        from a.b import Class

        module_var: Class = Class()






    # 6:







    .. code-block:: python


        from typing import Literal

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






    # 7:







    .. code-block:: python


        import typing

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






---------------------
INVALID Code Examples
---------------------

# 1:







.. code-block:: python


    from __future__ import annotations

    from a.b import Class

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





Autofix:

.. code-block:: python

    --- 
    +++ 
    @@ -3,5 +3,5 @@
 
     from a.b import Class
 
    -def foo() -> "Class":
    +def foo() -> Class:
         return Class()

# 2:







.. code-block:: python


    from __future__ import annotations

    from a.b import Class

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





Autofix:

.. code-block:: python

    --- 
    +++ 
    @@ -3,5 +3,5 @@
 
     from a.b import Class
 
    -async def foo() -> "Class":
    +async def foo() -> Class:
         return await Class()

# 3:







.. code-block:: python


    from __future__ import annotations

    import typing
    from a.b import Class

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





Autofix:

.. code-block:: python

    --- 
    +++ 
    @@ -4,5 +4,5 @@
     import typing
     from a.b import Class
 
    -def foo() -> typing.Type["Class"]:
    +def foo() -> typing.Type[Class]:
         return Class

.. container:: toggle


    # 4:







    .. code-block:: python


        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:

    .. code-block:: python

        --- 
        +++ 
        @@ -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:







    .. code-block:: python


        from __future__ import annotations

        from a.b import Class

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

        foo(Class())





    Autofix:

    .. code-block:: python

        --- 
        +++ 
        @@ -3,7 +3,7 @@
 
         from a.b import Class
 
        -def foo(arg: "Class") -> None:
        +def foo(arg: Class) -> None:
             pass
 
         foo(Class())


    # 6:







    .. code-block:: python


        from __future__ import annotations

        from a.b import Class

        module_var: "Class" = Class()





    Autofix:

    .. code-block:: python

        --- 
        +++ 
        @@ -3,4 +3,4 @@
 
         from a.b import Class
 
        -module_var: "Class" = Class()
        +module_var: Class = Class()


    # 7:







    .. code-block:: python


        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:

    .. code-block:: python

        --- 
        +++ 
        @@ -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()

