Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

logging-fstring-interpolation not raised on function parameter hinted as logging.Logger #8781

Open
ringohoffman opened this issue Jun 17, 2023 · 4 comments
Labels
Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling

Comments

@ringohoffman
Copy link

ringohoffman commented Jun 17, 2023

Bug description

If I log to the root logger directly or if within a scope I call logging.getLogger and then log to that Logger, I will get the logging-fstring-interpolation warning, but if I have a function parameter that is hinted as a logging.Logger, I will not get the logging-fstring-interpolation warning on that logger:

"""A demonstration of a missing logging-fstring-interpolation warning."""
import logging

MSG = "we used an f-string in a logging statement"
logging.error(f"Something bad happened: {MSG}")

file_logger = logging.getLogger(__name__)
file_logger.error(f"Something bad happened: {MSG}")


def my_function(logger: logging.Logger) -> None:
    """A function that accepts a logger."""
    logging.error(f"Something bad happened: {MSG}")

    function_logger = logging.getLogger(my_function.__name__)
    function_logger.error(f"Something bad happened: {MSG}")

    logger.error(f"Something bad happened: {MSG}")  # no logging-fstring-interpolation warning


class MyClass:  # pylint: disable=too-few-public-methods
    """A class with a method that accepts a logger."""

    def __init__(self, logger: logging.Logger) -> None:
        logging.error(f"Something bad happened: {MSG}")

        class_logger = logging.getLogger(MyClass.__name__)
        class_logger.error(f"Something bad happened: {MSG}")

        logger.error(f"Something bad happened: {MSG}")  # no logging-fstring-interpolation warning

Configuration

N/A

Command used

pylint a.py

Pylint output

$ pylint a.py
************* Module a
a.py:5:0: W1203: Use lazy % formatting in logging functions (logging-fstring-interpolation)
a.py:8:0: W1203: Use lazy % formatting in logging functions (logging-fstring-interpolation)
a.py:13:4: W1203: Use lazy % formatting in logging functions (logging-fstring-interpolation)
a.py:16:4: W1203: Use lazy % formatting in logging functions (logging-fstring-interpolation)
a.py:25:8: W1203: Use lazy % formatting in logging functions (logging-fstring-interpolation)
a.py:28:8: W1203: Use lazy % formatting in logging functions (logging-fstring-interpolation)

Expected behavior

I expect to get a logging-fstring-interpolation warning on the logger.error statements too.

Pylint version

astroid==2.15.4
pylint==2.17.4


### OS / Environment

Darwin

### Additional dependencies

_No response_
@ringohoffman ringohoffman added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Jun 17, 2023
@Pierre-Sassoulas
Copy link
Member

I think it depends on #4813 and is not going to be solved as long as it's not done, so I'm hesitating to close as duplicate even if it seem it's two very different issues.

@ringohoffman
Copy link
Author

ringohoffman commented Jun 17, 2023

Yes, so it seems. Kind of a shocking to realize that astroid doesn't currently use type hints to infer the origin of an expression. It greatly diminishes the opportunities for this warning to be raised (and others as well I'm sure) in an object oriented setting.

@Pierre-Sassoulas
Copy link
Member

astroid was created in 2003 and typing did not exists at the time :)

@fenchu
Copy link

fenchu commented Sep 14, 2023

Just drop it I disable W1203 everywhere:
image

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants