.. _UseFstringRule:

==============
UseFstringRule
==============

Encourages the use of f-string instead of %-formatting or .format() for high code quality and efficiency.

Following two cases not covered:

1. arguments length greater than 30 characters: for better readibility reason
    For example:

    1: this is the answer: %d" % (a_long_function_call() + b_another_long_function_call())
    2: f"this is the answer: {a_long_function_call() + b_another_long_function_call()}"
    3: result = a_long_function_call() + b_another_long_function_call()
    f"this is the answer: {result}"

    Line 1 is more readable than line 2. Ideally, we’d like developers to manually fix this case to line 3

2. only %s placeholders are linted against for now. We leave it as future work to support other placeholders.
    For example, %d raises TypeError for non-numeric objects, whereas f“{x:d}” raises ValueError.
    This discrepancy in the type of exception raised could potentially break the logic in the code where the exception is handled

-------
Message
-------
Do not use printf style formatting or .format(). Use f-string instead to be more readable and efficient. See https://www.python.org/dev/peps/pep-0498/

----------------
Has Autofix: Yes
----------------

-------------------
VALID Code Examples
-------------------

# 1:







.. code-block:: python

            somebody='you'; f"Hey, {somebody}."




# 2:







.. code-block:: python

            "hey"




# 3:







.. code-block:: python

            "hey" + "there"




.. container:: toggle


    # 4:







    .. code-block:: python

                b"a type %s" % var





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

# 1:







.. code-block:: python

            "Hey, {somebody}.".format(somebody="you")




# 2:







.. code-block:: python

            "%s" % "hi"




Autofix:

.. code-block:: python

    --- 
    +++ 
    @@ -1 +1 @@
    -"%s" % "hi"
    +f"{'hi'}"

# 3:







.. code-block:: python

            "a name: %s" % name




Autofix:

.. code-block:: python

    --- 
    +++ 
    @@ -1 +1 @@
    -"a name: %s" % name
    +f"a name: {name}"

.. container:: toggle


    # 4:







    .. code-block:: python

                "an attribute %s ." % obj.attr




    Autofix:

    .. code-block:: python

        --- 
        +++ 
        @@ -1 +1 @@
        -"an attribute %s ." % obj.attr
        +f"an attribute {obj.attr} ."


    # 5:







    .. code-block:: python

                r"raw string value=%s" % val




    Autofix:

    .. code-block:: python

        --- 
        +++ 
        @@ -1 +1 @@
        -r"raw string value=%s" % val
        +fr"raw string value={val}"


    # 6:







    .. code-block:: python

                "{%s}" % val




    Autofix:

    .. code-block:: python

        --- 
        +++ 
        @@ -1 +1 @@
        -"{%s}" % val
        +f"{{{val}}}"


    # 7:







    .. code-block:: python

                "{%s" % val




    Autofix:

    .. code-block:: python

        --- 
        +++ 
        @@ -1 +1 @@
        -"{%s" % val
        +f"{{{val}"


    # 8:







    .. code-block:: python

                "The type of var: %s" % type(var)




    Autofix:

    .. code-block:: python

        --- 
        +++ 
        @@ -1 +1 @@
        -"The type of var: %s" % type(var)
        +f"The type of var: {type(var)}"


    # 9:







    .. code-block:: python

                "%s" % obj.this_is_a_very_long_expression(parameter)["a_very_long_key"]





    # 10:







    .. code-block:: python

                "type of var: %s, value of var: %s" % (type(var), var)




    Autofix:

    .. code-block:: python

        --- 
        +++ 
        @@ -1 +1 @@
        -"type of var: %s, value of var: %s" % (type(var), var)
        +f"type of var: {type(var)}, value of var: {var}"


    # 11:







    .. code-block:: python

                '%s" double quote is used' % var




    Autofix:

    .. code-block:: python

        --- 
        +++ 
        @@ -1 +1 @@
        -'%s" double quote is used' % var
        +f'{var}" double quote is used'


    # 12:







    .. code-block:: python

                "var1: %s, var2: %s, var3: %s, var4: %s" % (class_object.attribute, dict_lookup["some_key"], some_module.some_function(), var4)




    Autofix:

    .. code-block:: python

        --- 
        +++ 
        @@ -1 +1 @@
        -"var1: %s, var2: %s, var3: %s, var4: %s" % (class_object.attribute, dict_lookup["some_key"], some_module.some_function(), var4)
        +f"var1: {class_object.attribute}, var2: {dict_lookup['some_key']}, var3: {some_module.some_function()}, var4: {var4}"


    # 13:







    .. code-block:: python

                "a list: %s" % " ".join(var)




    Autofix:

    .. code-block:: python

        --- 
        +++ 
        @@ -1 +1 @@
        -"a list: %s" % " ".join(var)
        +f"a list: {' '.join(var)}"

