From d788957ec4647d7af4c74ba342c55eda41d14b4e Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Fri, 4 Aug 2023 19:25:34 -0400 Subject: [PATCH] Allow capitalized names for logger candidate heuristic match (#6356) Closes https://github.com/astral-sh/ruff/issues/6353. --- .../test/fixtures/flake8_logging_format/G004.py | 3 +++ ...ff__rules__flake8_logging_format__tests__G004.py.snap | 9 +++++++++ crates/ruff_python_semantic/src/analyze/logging.rs | 8 +++++++- 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/crates/ruff/resources/test/fixtures/flake8_logging_format/G004.py b/crates/ruff/resources/test/fixtures/flake8_logging_format/G004.py index abbbe8b7d04ca..56e005293a62a 100644 --- a/crates/ruff/resources/test/fixtures/flake8_logging_format/G004.py +++ b/crates/ruff/resources/test/fixtures/flake8_logging_format/G004.py @@ -3,3 +3,6 @@ name = "world" logging.info(f"Hello {name}") logging.log(logging.INFO, f"Hello {name}") + +_LOGGER = logging.getLogger() +_LOGGER.info(f"{__name__}") diff --git a/crates/ruff/src/rules/flake8_logging_format/snapshots/ruff__rules__flake8_logging_format__tests__G004.py.snap b/crates/ruff/src/rules/flake8_logging_format/snapshots/ruff__rules__flake8_logging_format__tests__G004.py.snap index b94da46ca98f2..b54d2a4862767 100644 --- a/crates/ruff/src/rules/flake8_logging_format/snapshots/ruff__rules__flake8_logging_format__tests__G004.py.snap +++ b/crates/ruff/src/rules/flake8_logging_format/snapshots/ruff__rules__flake8_logging_format__tests__G004.py.snap @@ -15,6 +15,15 @@ G004.py:5:27: G004 Logging statement uses f-string 4 | logging.info(f"Hello {name}") 5 | logging.log(logging.INFO, f"Hello {name}") | ^^^^^^^^^^^^^^^ G004 +6 | +7 | _LOGGER = logging.getLogger() + | + +G004.py:8:14: G004 Logging statement uses f-string + | +7 | _LOGGER = logging.getLogger() +8 | _LOGGER.info(f"{__name__}") + | ^^^^^^^^^^^^^ G004 | diff --git a/crates/ruff_python_semantic/src/analyze/logging.rs b/crates/ruff_python_semantic/src/analyze/logging.rs index 70efd2e688f45..205e3bb8e4c3b 100644 --- a/crates/ruff_python_semantic/src/analyze/logging.rs +++ b/crates/ruff_python_semantic/src/analyze/logging.rs @@ -49,7 +49,13 @@ pub fn is_logger_candidate( // logger names. if let Some(call_path) = collect_call_path(value) { if let Some(tail) = call_path.last() { - if tail.starts_with("log") || tail.ends_with("logger") || tail.ends_with("logging") { + if tail.starts_with("log") + || tail.ends_with("logger") + || tail.ends_with("logging") + || tail.starts_with("LOG") + || tail.ends_with("LOGGER") + || tail.ends_with("LOGGING") + { return true; } }