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

usage of .css()[0].extract() instead of .extract_first()/.get() #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions finders/oldstyle.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,22 @@ def find_issues(self, node):
for kw in node.value.keywords:
if self.has_response_for_keyword_parameter(kw):
return [(node.lineno, node.col_offset, self.message)]


class GetFirstByIndexIssueFinder(IssueFinder):
msg_code = 'SCP06'
msg_info = 'use .get() or .extract_first() to get the first item'

def find_issues(self, node):
issue_exists = (
node.slice.value.n == 0 and
isinstance(node.value, ast.Call) and
isinstance(node.value.func, ast.Attribute) and
node.value.func.attr == 'extract' and
node.value.func.attr in ('css', 'xpath')
)
if not issue_exists:
return

return [(node.lineno, node.col_offset, self.message)]

12 changes: 10 additions & 2 deletions flake8_scrapy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
from finders.domains import (
UnreachableDomainIssueFinder, UrlInAllowedDomainsIssueFinder,
)
from finders.oldstyle import OldSelectorIssueFinder, UrlJoinIssueFinder
from finders.oldstyle import (
GetFirstByIndexIssueFinder, OldSelectorIssueFinder, UrlJoinIssueFinder,
)


__version__ = '0.0.1'
Expand All @@ -22,7 +24,10 @@ def __init__(self, *args, **kwargs):
],
'Call': [
UrlJoinIssueFinder(),
]
],
'Subscript': [
GetFirstByIndexIssueFinder(),
],
}

def find_issues_visitor(self, visitor, node):
Expand All @@ -40,6 +45,9 @@ def visit_Assign(self, node):
def visit_Call(self, node):
self.find_issues_visitor('Call', node)

def visit_Subscript(self, node):
self.find_issues_visitor('Subscript', node)


class ScrapyStyleChecker(object):
options = None
Expand Down
13 changes: 13 additions & 0 deletions tests/test_oldstyle.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,16 @@ def test_dont_find_old_style_urljoin(code):
def test_find_old_style_selector(code, expected):
issues = run_checker(code)
assert len(issues) == expected


@pytest.mark.parametrize('code,expected', [
('response.css("*")[0].extract()', 1),
('response.xpath("//*")[0].extract()', 1),
# ('response.css("*").extract()[0]', 1),
# ('response.xpath("//*").extract()[0]', 1),
# ('response.css("*").getall()[0]', 1),
# ('response.xpath("//*")[0].get()', 1),
])
def test_find_oldstyle_get_first_by_index(code, expected):
issues = run_checker(code)
assert len(issues) == expected