mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-18 17:41:12 +00:00
Include inline instantiations when detecting loggers (#11154)
## Summary Closes https://github.com/astral-sh/ruff/issues/11031.
This commit is contained in:
parent
22d4f11348
commit
b15e9e6e05
3 changed files with 34 additions and 11 deletions
|
@ -7,6 +7,9 @@ logging.log(logging.INFO, f"Hello {name}")
|
||||||
_LOGGER = logging.getLogger()
|
_LOGGER = logging.getLogger()
|
||||||
_LOGGER.info(f"{__name__}")
|
_LOGGER.info(f"{__name__}")
|
||||||
|
|
||||||
|
logging.getLogger().info(f"{name}")
|
||||||
|
|
||||||
from logging import info
|
from logging import info
|
||||||
|
|
||||||
info(f"{name}")
|
info(f"{name}")
|
||||||
info(f"{__name__}")
|
info(f"{__name__}")
|
||||||
|
|
|
@ -25,23 +25,31 @@ G004.py:8:14: G004 Logging statement uses f-string
|
||||||
8 | _LOGGER.info(f"{__name__}")
|
8 | _LOGGER.info(f"{__name__}")
|
||||||
| ^^^^^^^^^^^^^ G004
|
| ^^^^^^^^^^^^^ G004
|
||||||
9 |
|
9 |
|
||||||
10 | from logging import info
|
10 | logging.getLogger().info(f"{name}")
|
||||||
|
|
|
|
||||||
|
|
||||||
G004.py:11:6: G004 Logging statement uses f-string
|
G004.py:10:26: G004 Logging statement uses f-string
|
||||||
|
|
|
|
||||||
10 | from logging import info
|
8 | _LOGGER.info(f"{__name__}")
|
||||||
11 | info(f"{name}")
|
9 |
|
||||||
|
10 | logging.getLogger().info(f"{name}")
|
||||||
|
| ^^^^^^^^^ G004
|
||||||
|
11 |
|
||||||
|
12 | from logging import info
|
||||||
|
|
|
||||||
|
|
||||||
|
G004.py:14:6: G004 Logging statement uses f-string
|
||||||
|
|
|
||||||
|
12 | from logging import info
|
||||||
|
13 |
|
||||||
|
14 | info(f"{name}")
|
||||||
| ^^^^^^^^^ G004
|
| ^^^^^^^^^ G004
|
||||||
12 | info(f"{__name__}")
|
15 | info(f"{__name__}")
|
||||||
|
|
|
|
||||||
|
|
||||||
G004.py:12:6: G004 Logging statement uses f-string
|
G004.py:15:6: G004 Logging statement uses f-string
|
||||||
|
|
|
|
||||||
10 | from logging import info
|
14 | info(f"{name}")
|
||||||
11 | info(f"{name}")
|
15 | info(f"{__name__}")
|
||||||
12 | info(f"{__name__}")
|
|
||||||
| ^^^^^^^^^^^^^ G004
|
| ^^^^^^^^^^^^^ G004
|
||||||
|
|
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -25,6 +25,18 @@ pub fn is_logger_candidate(
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// If the attribute is an inline instantiation, match against known constructors.
|
||||||
|
if let Expr::Call(ast::ExprCall { func, .. }) = &**value {
|
||||||
|
return semantic
|
||||||
|
.resolve_qualified_name(func)
|
||||||
|
.is_some_and(|qualified_name| {
|
||||||
|
matches!(
|
||||||
|
qualified_name.segments(),
|
||||||
|
["logging", "getLogger" | "Logger"]
|
||||||
|
)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// If the symbol was imported from another module, ensure that it's either a user-specified
|
// If the symbol was imported from another module, ensure that it's either a user-specified
|
||||||
// logger object, the `logging` module itself, or `flask.current_app.logger`.
|
// logger object, the `logging` module itself, or `flask.current_app.logger`.
|
||||||
if let Some(qualified_name) = semantic.resolve_qualified_name(value) {
|
if let Some(qualified_name) = semantic.resolve_qualified_name(value) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue