Skip to content

Commit

Permalink
Add option to allow overriding isort's skip_gitignore option
Browse files Browse the repository at this point in the history
  • Loading branch information
gschaffner committed Dec 7, 2022
1 parent 65c1a59 commit 0bacb49
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
7 changes: 6 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
------------------

Expand Down
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----------
+------------+-----------------------------------------------------------+
Expand Down
23 changes: 21 additions & 2 deletions flake8_isort.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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):
Expand All @@ -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
Expand Down
10 changes: 8 additions & 2 deletions run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,15 +212,21 @@ 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)

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
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down

0 comments on commit 0bacb49

Please sign in to comment.