From 41e842c87673f4ceaf66958472715c5c5ef64edf Mon Sep 17 00:00:00 2001 From: namaevae Date: Thu, 17 Jul 2025 09:55:18 +0300 Subject: [PATCH 1/2] this reduces execution time from ~1500 ns to ~1250 ns --- pycodestyle.py | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/pycodestyle.py b/pycodestyle.py index 9df2ae4c..97714220 100755 --- a/pycodestyle.py +++ b/pycodestyle.py @@ -1160,6 +1160,22 @@ def imports_on_separate_lines(logical_line): yield found, "E401 multiple imports on one line" +_STRING_PREFIXES = frozenset(('u', 'U', 'b', 'B', 'r', 'R')) + + +def _is_string_literal(line): + if line: + first_char = line[0] + if first_char in _STRING_PREFIXES: + first_char = line[1] + return first_char == '"' or first_char == "'" + return False + + +_ALLOWED_KEYWORDS_IN_IMPORTS = frozenset([ + 'try', 'except', 'else', 'finally', 'with', 'if', 'elif']) + + @register_check def module_imports_on_top_of_file( logical_line, indent_level, checker_state, noqa): @@ -1178,15 +1194,6 @@ def module_imports_on_top_of_file( Okay: if x:\n import os """ # noqa - def is_string_literal(line): - if line[0] in 'uUbB': - line = line[1:] - if line and line[0] in 'rR': - line = line[1:] - return line and (line[0] == '"' or line[0] == "'") - - allowed_keywords = ( - 'try', 'except', 'else', 'finally', 'with', 'if', 'elif') if indent_level: # Allow imports in conditional statement/function return @@ -1194,17 +1201,17 @@ def is_string_literal(line): return if noqa: return - line = logical_line - if line.startswith('import ') or line.startswith('from '): + if logical_line.startswith('import ') or logical_line.startswith('from '): if checker_state.get('seen_non_imports', False): yield 0, "E402 module level import not at top of file" - elif re.match(DUNDER_REGEX, line): + elif re.match(DUNDER_REGEX, logical_line): return - elif any(line.startswith(kw) for kw in allowed_keywords): + elif any(logical_line.startswith(kw) + for kw in _ALLOWED_KEYWORDS_IN_IMPORTS): # Allow certain keywords intermixed with imports in order to # support conditional or filtered importing return - elif is_string_literal(line): + elif _is_string_literal(logical_line): # The first literal is a docstring, allow it. Otherwise, report # error. if checker_state.get('seen_docstring', False): From edab88367756980f73ab9f40f9ba9c4764c8e9b4 Mon Sep 17 00:00:00 2001 From: anthony sottile Date: Sat, 2 Aug 2025 12:59:30 -0400 Subject: [PATCH 2/2] further improvements to module_imports_on_top_of_file --- pycodestyle.py | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/pycodestyle.py b/pycodestyle.py index 97714220..dbd2afa7 100755 --- a/pycodestyle.py +++ b/pycodestyle.py @@ -1172,8 +1172,8 @@ def _is_string_literal(line): return False -_ALLOWED_KEYWORDS_IN_IMPORTS = frozenset([ - 'try', 'except', 'else', 'finally', 'with', 'if', 'elif']) +_ALLOWED_KEYWORDS_IN_IMPORTS = ( + 'try', 'except', 'else', 'finally', 'with', 'if', 'elif') @register_check @@ -1201,25 +1201,25 @@ def module_imports_on_top_of_file( return if noqa: return - if logical_line.startswith('import ') or logical_line.startswith('from '): + if logical_line.startswith(('import ', 'from ')): if checker_state.get('seen_non_imports', False): yield 0, "E402 module level import not at top of file" - elif re.match(DUNDER_REGEX, logical_line): - return - elif any(logical_line.startswith(kw) - for kw in _ALLOWED_KEYWORDS_IN_IMPORTS): - # Allow certain keywords intermixed with imports in order to - # support conditional or filtered importing - return - elif _is_string_literal(logical_line): - # The first literal is a docstring, allow it. Otherwise, report - # error. - if checker_state.get('seen_docstring', False): - checker_state['seen_non_imports'] = True + elif not checker_state.get('seen_non_imports', False): + if DUNDER_REGEX.match(logical_line): + return + elif logical_line.startswith(_ALLOWED_KEYWORDS_IN_IMPORTS): + # Allow certain keywords intermixed with imports in order to + # support conditional or filtered importing + return + elif _is_string_literal(logical_line): + # The first literal is a docstring, allow it. Otherwise, + # report error. + if checker_state.get('seen_docstring', False): + checker_state['seen_non_imports'] = True + else: + checker_state['seen_docstring'] = True else: - checker_state['seen_docstring'] = True - else: - checker_state['seen_non_imports'] = True + checker_state['seen_non_imports'] = True @register_check