Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Inconsistent logging detection #7502

Closed
konstin opened this issue Sep 18, 2023 · 4 comments
Closed

Inconsistent logging detection #7502

konstin opened this issue Sep 18, 2023 · 4 comments
Labels
rule Implementing or modifying a lint rule

Comments

@konstin
Copy link
Member

konstin commented Sep 18, 2023

There are three ways you can call the logging module:

import logging
logger = logging.getLoger(__name__)
logger.info("msg")
import logging
logging.info("msg")
from logging import info
info("msg")

The logging rules are enforced inconsistently between those three call kinds:

import logging
from logging import warn, info, error

logger = logging.getLogger(__name__)

logging.info(f"{1}")  # G004
logger.info(f"{1}")  # G004
info(f"{1}")

logging.info("", 2)  # PLE1205
logger.info("", 2)  # PLE1205
info("", 2)

logging.warn("a")  # PGH002, G010
logger.warn("a")  # G010
warn("a")  # PGH002

try:
    print(1 / 0)
except Exception:
    logging.error("a")  # TRY400
    logger.error("a")  # TRY400
    error("a")

Checking for a common logger name is generally consistent, but PGH002 mismatches expectations here.

It might also make sense to look for usages of logging.getLogger for a more semantic coverage, even though the vast majority of usages in the wild seem to match our pattern (see also https://grep.app/search?q=logging.getLogger&filter[lang][0]=Python, https://github.com/hummingbot/hummingbot/blob/b3ba248e7b9bab26c010fcb69a53040146084adb/hummingbot/client/config/conf_migration.py#L45, https://github.com/qutebrowser/qutebrowser/blob/4190a470c56ed90d97af89711a30c4603a347858/qutebrowser/utils/log.py#L104)

The main questions i'm trying to answer are, a) should we change the scope of the existing rule (or not) and b) which of the three cases should new logging rules capture?

@konstin konstin added rule Implementing or modifying a lint rule needs-decision Awaiting a decision from a maintainer labels Sep 18, 2023
@charliermarsh
Copy link
Member

We should be treating error as equivalent to logging.error -- that seems like a bug in is_logger_candidate.

And we should change PGH002 to match G010. Actually, we should probably just remove PGH002, and redirect to G010?

@charliermarsh
Copy link
Member

We should be treating error as equivalent to logging.error

I looked into this a bit and I think it requires some minor modifications to the various rules that call is_logger_candidate.

@qdegraaf
Copy link
Contributor

qdegraaf commented Sep 19, 2023

I looked into this a bit and I think it requires some minor modifications to the various rules that call is_logger_candidate.

Could it not be handled in is_logger_candidate() itself?

Am I correct in thinking the issue is that is_logger_candidate() assumes the func to be an Expr::Attribute whereas a loose error() call is an Expr::Name? If so, would it be better to add a branch of logic to is_logger_candidate for this scenario, seeing as resolve_call_path() and collect_call_path() already allow for both Expr::Attribute and Expr::Name values to be passed?

@charliermarsh
Copy link
Member

Closed by #7521.

charliermarsh pushed a commit that referenced this issue Sep 27, 2023
## Summary

Expands several rules to also check for `Expr::Name` values. As they
would previously not consider:
```python
from logging import error

error("foo")
```
as potential violations
```python
import logging

logging.error("foo")
```
as potential violations leading to inconsistent behaviour. 

The rules impacted are:

- `BLE001`
- `TRY400`
- `TRY401`
- `PLE1205`
- `PLE1206`
- `LOG007`
- `G001`-`G004`
- `G101`
- `G201`
- `G202`

## Test Plan

Fixtures for all impacted rules expanded. 

## Issue Link

Refers: #7502
charliermarsh pushed a commit that referenced this issue Sep 27, 2023
## Summary

`PGH002`, which checks for use of deprecated `logging.warn` calls, did
not check for calls made on the attribute `warn` yet. Since
#7521 we check both cases for
similar rules wherever possible. To be consistent this PR expands PGH002
to do the same.

## Test Plan

Expanded existing fixtures with `logger.warn()` calls

## Issue links

Fixes final inconsistency mentioned in
#7502
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule Implementing or modifying a lint rule
Projects
None yet
Development

No branches or pull requests

3 participants