From 69b293b695dc8258b3f15cd1cc922497ed16dedc Mon Sep 17 00:00:00 2001 From: Valdir Stumm Junior Date: Thu, 18 Oct 2018 18:05:42 -0300 Subject: [PATCH] wip: scp06 --- finders/oldstyle.py | 19 +++++++++++++++++++ flake8_scrapy.py | 12 ++++++++++-- tests/test_oldstyle.py | 13 +++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/finders/oldstyle.py b/finders/oldstyle.py index 0910d97..6ed77fd 100644 --- a/finders/oldstyle.py +++ b/finders/oldstyle.py @@ -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)] + diff --git a/flake8_scrapy.py b/flake8_scrapy.py index c1198f4..427197a 100644 --- a/flake8_scrapy.py +++ b/flake8_scrapy.py @@ -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' @@ -22,7 +24,10 @@ def __init__(self, *args, **kwargs): ], 'Call': [ UrlJoinIssueFinder(), - ] + ], + 'Subscript': [ + GetFirstByIndexIssueFinder(), + ], } def find_issues_visitor(self, visitor, node): @@ -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 diff --git a/tests/test_oldstyle.py b/tests/test_oldstyle.py index 29e9f50..a8a9146 100644 --- a/tests/test_oldstyle.py +++ b/tests/test_oldstyle.py @@ -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