Skip to content

Commit

Permalink
fix lazy logging codemod bug with acceptable concatnation (#497)
Browse files Browse the repository at this point in the history
  • Loading branch information
clavedeluna authored Apr 22, 2024
1 parent b8a62db commit d16ff03
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/core_codemods/lazy_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,34 @@ class LazyLogging(SimpleCodemod, NameAndAncestorResolutionMixin):
...
{_log_funcs}
- patterns:
- pattern: logging.$FUNC(..., $ANYTHING + ..., ...)
- pattern: logging.$FUNC($MSG + ..., ...)
{_pattern_inside}
{_log_funcs}
- patterns:
- pattern: logging.getLogger(...).$FUNC(..., $ANYTHING + ..., ...)
- pattern: logging.getLogger(...).$FUNC($MSG + ..., ...)
{_pattern_inside}
{_log_funcs}
- patterns:
- pattern: $VAR.$FUNC(..., $ANYTHING + ..., ...)
- pattern: $VAR.$FUNC($MSG + ..., ...)
- pattern-inside: |
import logging
...
$VAR = logging.getLogger(...)
...
{_log_funcs}
- patterns:
- pattern: logging.log($LEVEL, $MSG + ..., ...)
{_pattern_inside}
- patterns:
- pattern: logging.getLogger(...).log($LEVEL, $MSG + ..., ...)
{_pattern_inside}
- patterns:
- pattern: $VAR.log($LEVEL, $MSG + ..., ...)
- pattern-inside: |
import logging
...
$VAR = logging.getLogger(...)
...
"""

def on_result_found(self, original_node, updated_node):
Expand Down
21 changes: 21 additions & 0 deletions tests/codemods/test_lazy_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,27 @@ def test_var_assignments(self, tmpdir):
import logging
logging.info("% something", "hi")
""",
"""
import logging
logger = logging.getLogger(__name__)
logger.debug(
"At depth %d, updating best step: %s (score: %f).",
10,
[swap] + next_step.swaps_added,
next_score,
)
""",
"""
import logging
logger = logging.getLogger(__name__)
logger.log(
logging.INFO,
"At depth %d, updating best step: %s (score: %f).",
10,
[swap] + next_step.swaps_added,
next_score,
)
""",
],
)
def test_no_change(self, tmpdir, code):
Expand Down

0 comments on commit d16ff03

Please sign in to comment.