Skip to content

Pylint should not emit logging-not-lazy when using explicit string concatenation for the message #8410

@yilei

Description

@yilei

Bug description

# pylint: disable=missing-docstring
import logging

logging.error('first' + 'second')

Since there is no difference in byte code whether the string is explicitly concatenated with + or not, the logging isn't really not lazy.

Extra context: the Black formatter will by default wrap long strings in parens with extra indentation:

logging.info("this is a super super super super super super super super super super super super super long message: %s", content)

is reformatted to:

logging.info(
    (
        "this is a super super super super super super super super super super super"
        " super super long message: %s"
    ),
    content,
)

Not all people like the extra indentation level, so they could workaround this by using explicit str concatenations:

logging.info(
    "this is a super super super super super super super super super super"
    + " super super super long message: %s",
    content
)

Above code won't be wrapped. See example: https://black.vercel.app/?version=stable&state=_Td6WFoAAATm1rRGAgAhARYAAAB0L-Wj4AGJAJZdAD2IimZxl1N_Wl9XMO7It8_a9_zDM7vdFRRO_5rVL_TbaRMZFsldltUpcVRrU3ciQwYZYh1fEUKKhGKajA7blNBzUd1tD9xf20knxX9nUEnMw4r5um_wmnJAbkpayNa6vKQJW31lZlMZ-h8FDJRG0yvSqO-tn_wDhAqHt1M950Ia2LlS8KBt25Oi9a35Qd-DQeQ5aXTpAAAAAJCojbjuFauOAAGyAYoDAAAVp5UpscRn-wIAAAAABFla

But this triggers the logging-not-lazy error where it really shouldn't.

Configuration

No response

Command used

pylint a.py

Pylint output

************* Module flag3app
a.py:4:0: W1201: Use lazy % formatting in logging functions (logging-not-lazy)

------------------------------------------------------------------
Your code has been rated at 5.00/10 (previous run: 5.00/10, +0.00)

Expected behavior

It should not raise logging-not-lazy

Pylint version

pylint 2.17.0
astroid 2.15.0
Python 3.11.2 (main, Mar  3 2023, 16:02:46) [GCC 12.2.0]

OS / Environment

No response

Additional dependencies

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    False Positive 🦟A message is emitted but nothing is wrong with the codeNeeds PRThis issue is accepted, sufficiently specified and now needs an implementation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions