This repository was archived by the owner on Feb 19, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
Linting check of use of string join() with generator expression #26
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fb16d3d
Linting check of use of string join() with generator expression
68e0fa9
Fix coverage
f24beca
Fix python version comparison
c16ed24
Update pandas_dev_flaker/_ast_helpers.py
MarcoGorelli f39b9fe
Update pandas_dev_flaker/_ast_helpers.py
MarcoGorelli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
""" | ||
Based on | ||
|
||
https://github.com/asottile/pyupgrade/blob/5fb168667ae73f157dd579344708e1cdfb0c0341/pyupgrade/_plugins/generator_expressions_pep289.py | ||
""" | ||
|
||
import ast | ||
from typing import Iterator, Tuple | ||
|
||
from pandas_dev_flaker._ast_helpers import is_str_constant | ||
from pandas_dev_flaker._data_tree import State, register | ||
|
||
MSG = "PDF024 found string join() with generator expressions" | ||
|
||
|
||
@register(ast.Call) | ||
def visit_Call( | ||
state: State, | ||
node: ast.Call, | ||
parent: ast.AST, | ||
) -> Iterator[Tuple[int, int, str]]: | ||
if ( | ||
isinstance(node.func, ast.Attribute) | ||
and node.func.attr == "join" | ||
and is_str_constant(node) | ||
and node.args | ||
and isinstance(node.args[0], ast.GeneratorExp) | ||
): | ||
yield (node.lineno, node.col_offset, MSG) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
""" | ||
Based on | ||
|
||
https://github.com/asottile/pyupgrade/blob/5fb168667ae73f157dd579344708e1cdfb0c0341/pyupgrade/_plugins/generator_expressions_pep289.py | ||
""" | ||
|
||
import ast | ||
import tokenize | ||
from io import StringIO | ||
|
||
import pytest | ||
|
||
from pandas_dev_flaker.__main__ import run | ||
|
||
|
||
def results(s): | ||
return { | ||
"{}:{}: {}".format(*r) | ||
for r in run( | ||
ast.parse(s), | ||
list(tokenize.generate_tokens(StringIO(s).readline)), | ||
) | ||
} | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"source", | ||
( | ||
pytest.param( | ||
"''.join([str(i) for i in range(5)])", | ||
id="String join() with list comprehension", | ||
), | ||
pytest.param( | ||
"''.join([\n" | ||
" str(i) for i in range(5)\n" | ||
" ]\n" | ||
")\n", | ||
id="String join() with multiline list comprehension", | ||
), | ||
), | ||
) | ||
def test_list_comprehensions(source): | ||
assert not results(source) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"source, expected", | ||
( | ||
pytest.param( | ||
"''.join(str(i) for i in range(5))", | ||
"1:0: PDF024 found string join() with generator expressions", | ||
id="String join() with generator expression", | ||
), | ||
pytest.param( | ||
"''.join((str(i) for i in range(5)))", | ||
"1:0: PDF024 found string join() with generator expressions", | ||
id="String join() with parenthesised generator expression", | ||
), | ||
pytest.param( | ||
"''.join((\n" | ||
" str(i) for i in range(5)\n" | ||
" )\n" | ||
")\n", | ||
"1:0: PDF024 found string join() with generator expressions", | ||
id="String join() with multiline generator expression", | ||
), | ||
), | ||
) | ||
def test_generator_expressions(source, expected): | ||
(result,) = results(source) | ||
assert result == expected |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can just do
sys.version_info < (3, 8)