From 0bacb498a8c3567ecd51140460fc25215f4f5aac Mon Sep 17 00:00:00 2001 From: Ganden Schaffner Date: Wed, 7 Dec 2022 00:00:00 -0800 Subject: [PATCH] Add option to allow overriding isort's skip_gitignore option --- CHANGES.rst | 7 ++++++- README.rst | 2 ++ flake8_isort.py | 23 +++++++++++++++++++++-- run_tests.py | 10 ++++++++-- setup.py | 2 +- 5 files changed, 38 insertions(+), 6 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index e8a0630..6a5d5a5 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -3,12 +3,17 @@ Changelog ========= -5.0.4 (unreleased) +5.1.0 (unreleased) ------------------ - Drop isort 4.x support. [gforcada] +- Add `--isort-no-skip-gitignore` option to allow temporarily overriding the set + value of isort's `skip_gitignore` option with `False`. This can cause + flake8-isort to run significantly faster at the cost of making flake8-isort's + behavior differ slightly from the behavior of `isort --check`. [gschaffner] + 5.0.3 (2022-11-20) ------------------ diff --git a/README.rst b/README.rst index 39b9f15..563dee2 100644 --- a/README.rst +++ b/README.rst @@ -29,6 +29,8 @@ Configuration ------------- If using the `select` `option from flake8`_ be sure to enable the `I` category as well, see below for the specific error codes reported by `flake8-isort`. +See ``flake8 --help`` for available flake8-isort options. + Error codes ----------- +------------+-----------------------------------------------------------+ diff --git a/flake8_isort.py b/flake8_isort.py index 577acdb..cc2299b 100644 --- a/flake8_isort.py +++ b/flake8_isort.py @@ -25,6 +25,7 @@ class Flake8IsortBase: isort_add_unexp = 'I005 isort found an unexpected missing import' show_traceback = False + no_skip_gitignore = False stdin_display_name = None search_current = True @@ -40,11 +41,23 @@ def add_options(option_manager): parse_from_config=True, help='Show full traceback with diff from isort', ) + option_manager.add_option( + '--isort-no-skip-gitignore', + action='store_true', + parse_from_config=True, + help=( + "Temporarily override the set value of isort's `skip_gitignore` option " + 'with `False`. This can cause flake8-isort to run significantly faster ' + "at the cost of making flake8-isort's behavior differ slightly from " + 'the behavior of `isort --check`.' + ), + ) @classmethod def parse_options(cls, option_manager, options, args): cls.stdin_display_name = options.stdin_display_name cls.show_traceback = options.isort_show_traceback + cls.no_skip_gitignore = options.isort_no_skip_gitignore class Flake8Isort5(Flake8IsortBase): @@ -53,10 +66,16 @@ class Flake8Isort5(Flake8IsortBase): def run(self): if self.filename is not self.stdin_display_name: file_path = Path(self.filename) - isort_config = isort.settings.Config(settings_path=file_path.parent) + settings_path = file_path.parent else: file_path = None - isort_config = isort.settings.Config(settings_path=Path.cwd()) + settings_path = Path.cwd() + if self.no_skip_gitignore: + isort_config = isort.settings.Config( + settings_path=settings_path, skip_gitignore=False + ) + else: + isort_config = isort.settings.Config(settings_path=settings_path) input_string = ''.join(self.lines) traceback = '' isort_changed = False diff --git a/run_tests.py b/run_tests.py index 118d5cf..f44a2ae 100644 --- a/run_tests.py +++ b/run_tests.py @@ -212,7 +212,13 @@ def test_isort_formatted_output(tmpdir): from sys import pid """ options = collections.namedtuple( - 'Options', ['no_isort_config', 'isort_show_traceback', 'stdin_display_name'] + 'Options', + [ + 'no_isort_config', + 'isort_show_traceback', + 'stdin_display_name', + 'isort_no_skip_gitignore', + ], ) (file_path, lines) = write_python_file(tmpdir, source) @@ -220,7 +226,7 @@ def test_isort_formatted_output(tmpdir): diff = ' from __future__ import division\n+\n import os' checker = Flake8Isort(None, file_path, lines) - checker.parse_options(None, options(None, True, 'stdin'), None) + checker.parse_options(None, options(None, True, 'stdin', None), None) ret = list(checker.run()) assert len(ret) == 1 assert ret[0][0] == 3 diff --git a/setup.py b/setup.py index 29fca34..d21fe4e 100644 --- a/setup.py +++ b/setup.py @@ -17,7 +17,7 @@ def read_file(filename): setup( name='flake8-isort', - version='5.0.4.dev0', + version='5.1.0.dev0', description=short_description, long_description=long_description, # Get more from https://pypi.org/classifiers/