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

Overhaul #117

Merged
merged 9 commits into from Oct 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
103 changes: 73 additions & 30 deletions .github/workflows/tests.yml
@@ -1,48 +1,91 @@
name: Tests

name: Testing
on:
push:
branches: [master]
pull_request:
branches: [master]
paths:
- "flake8_isort.py"
- "test_flake8_isort.py"
- "setup.py"
- ".github/workflows/tests.yml"

env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
jobs:
tests:
test:
name: py-${{ matrix.python-version }}/isort-${{ matrix.isort }}/flake8-${{ matrix.flake8 }}
runs-on: ubuntu-latest
name: Python ${{ matrix.python-version }} x isort ${{ matrix.isort }} x flake8 ${{ matrix.flake8 }}

strategy:
matrix:
python-version: ['3.6', '3.7', '3.8', '3.9', '3.10', 'pypy3']
isort: [4.3.21, 5.10.1]
flake8: [3.9.2, 4.0.1, 5.0.4]
python-version: ["3.10", 3.9, 3.8, 3.7, pypy-3.9]
isort: [5.10.1]
flake8: [5.0.4]
include:
- python-version: 3.9
isort: 5.10.1
flake8: 4.0.1
qa: 'true'
- python-version: 3.9
isort: 5.10.1
flake8: 3.9.2

- python-version: 3.9
isort: 4.3.21
flake8: 5.0.4
- python-version: 3.9
isort: 4.3.21
flake8: 4.0.1
- python-version: 3.9
isort: 4.3.21
flake8: 3.9.2
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
architecture: x64
- name: Install matrix dependencies
run: pip install 'isort==${{ matrix.isort }}' 'flake8==${{ matrix.flake8 }}'
- name: Cache packages
uses: actions/cache@v3
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ matrix.python-version }}-${{ hashFiles('requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-${{ matrix.python-version }}-
- name: pip version
run: pip --version
- name: Install dependencies
run: |
python -m pip install -r requirements.txt
pip install 'isort==${{ matrix.isort }}' 'flake8==${{ matrix.flake8 }}'
# isort 4.x requires `toml` to be able to read pyproject.toml, so install it...
- name: Install toml if required
run: pip install toml
if: matrix.isort == '4.3.21'
- name: Install dependencies
run: pip install .[test]
- name: flake8
run: flake8 *.py
- name: pytest
run: pytest -v --cov flake8_isort --cov-report term-missing
- name: Upload coverage
# formatters
- name: Run pyupgrade
if: matrix.qa == 'true'
run: pyupgrade --py37-plus *.py
- name: Run isort
if: matrix.qa == 'true'
run: isort --check-only *.py
- name: Run black
if: matrix.qa == 'true'
run: black --check --skip-string-normalization *.py
# linters
- name: Lint with bandit
if: matrix.qa == 'true'
run: bandit --skip B101 *.py # B101 is assert statements
- name: Lint with codespell
if: matrix.qa == 'true'
run: codespell *.rst *.py
- name: Lint with flake8
if: matrix.qa == 'true'
run: flake8 *.py --count --max-complexity=18 --max-line-length=88 --show-source --statistics
- name: Lint with mypy
if: matrix.qa == 'true'
run: |
python -m pip install coveralls
coveralls --service=github
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
mkdir --parents --verbose .mypy_cache
mypy --ignore-missing-imports --install-types --non-interactive *.py || true
- name: Lint with safety
if: matrix.qa == 'true'
run: safety check || true
# tests and coverage
- name: Test
run: pytest run_tests.py --cov --cov-report term-missing
- name: Coverage
run: coveralls --service=github
2 changes: 2 additions & 0 deletions .gitignore
@@ -1,8 +1,10 @@
*.egg-info
*.pyc

.cache
.coverage
.installed.cfg
.hypothesis
bin
develop-eggs
include
Expand Down
7 changes: 6 additions & 1 deletion CHANGES.rst
Expand Up @@ -6,8 +6,13 @@ Changelog
4.2.1 (unreleased)
------------------

- Nothing changed yet.
- Update dependencies. [gforcada]

- Revamp GitHub actions. [gforcada]

- Drop python 3.6, and add python 3.10. [gforcada]

- Use linters and formatters to keep code sane and beautiful. [gforcada]

4.2.0 (2022-08-04)
------------------
Expand Down
17 changes: 0 additions & 17 deletions LICENSE.rst

This file was deleted.

12 changes: 10 additions & 2 deletions MANIFEST.in
@@ -1,3 +1,11 @@
include MANIFEST.in LICENSE *.rst *.py *.cfg *.ini
exclude .installed.cfg .coveragerc *.pyc
include MANIFEST.in
include LICENSE
include setup.cfg
include *.rst
include *.py
include *requirements.txt

exclude .installed.cfg
exclude .coveragerc
exclude *.pyc
exclude *.in
8 changes: 4 additions & 4 deletions README.rst
@@ -1,9 +1,9 @@
.. -*- coding: utf-8 -*-

.. image:: https://github.com/gforcada/flake8-isort/actions/workflows/tests.yml/badge.svg?branch=master
:target: https://github.com/gforcada/flake8-isort/actions/workflows/tests.yml
.. image:: https://github.com/gforcada/flake8-isort/actions/workflows/testing.yml/badge.svg?branch=master
:target: https://github.com/gforcada/flake8-isort/actions/workflows/testing.yml

.. image:: https://coveralls.io/repos/gforcada/flake8-isort/badge.svg?branch=master&service=github
.. image:: https://coveralls.io/repos/gforcada/flake8-isort/badge.svg?branch=master
:target: https://coveralls.io/github/gforcada/flake8-isort?branch=master

Flake8 meet isort
Expand Down Expand Up @@ -47,7 +47,7 @@ Error codes

Requirements
------------
- Python 2.7, 3.5, 3.6, pypy or pypy3
- Python 3.7, 3.8, 3.9, 3.10 and pypy3
- flake8
- isort

Expand Down
67 changes: 26 additions & 41 deletions flake8_isort.py
@@ -1,36 +1,20 @@
# -*- coding: utf-8 -*-

import warnings
from contextlib import redirect_stdout
from difflib import Differ
from difflib import unified_diff
from difflib import Differ, unified_diff
from io import StringIO
from pathlib import Path

import isort
import warnings


__version__ = '4.2.1.dev0'


class Flake8IsortBase(object):
class Flake8IsortBase:
name = 'flake8_isort'
version = __version__
isort_unsorted = (
'I001 isort found an import in the wrong position'
)
no_config_msg = (
'I002 no configuration found (.isort.cfg or [isort] in configs)'
)
isort_blank_req = (
'I003 isort expected 1 blank line in imports, found 0'
)
isort_blank_unexp = (
'I004 isort found an unexpected blank line in imports'
)
isort_add_unexp = (
'I005 isort found an unexpected missing import'
)
version = '4.2.1'
isort_unsorted = 'I001 isort found an import in the wrong position'
no_config_msg = 'I002 no configuration found (.isort.cfg or [isort] in configs)'
isort_blank_req = 'I003 isort expected 1 blank line in imports, found 0'
isort_blank_unexp = 'I004 isort found an unexpected blank line in imports'
isort_add_unexp = 'I005 isort found an unexpected missing import'

show_traceback = False
stdin_display_name = None
Expand All @@ -46,7 +30,7 @@ def add_options(cls, parser):
'--isort-show-traceback',
action='store_true',
parse_from_config=True,
help='Show full traceback with diff from isort'
help='Show full traceback with diff from isort',
)

@classmethod
Expand Down Expand Up @@ -101,9 +85,7 @@ def sortimports_linenum_msg(self, sort_result):
diff = differ.compare(sort_result.in_lines, sort_result.out_lines)

line_num = 0
additions = {
'+ {}'.format(add_import) for add_import in sort_result.add_imports
}
additions = {f'+ {add_import}' for add_import in sort_result.add_imports}
for line in diff:
if line.startswith(' ', 0, 2):
line_num += 1 # Ignore unchanged lines but increment line_num.
Expand Down Expand Up @@ -176,7 +158,8 @@ def _fixup_sortimports_wrapped(sort_imports):
for idx, line in enumerate(sort_imports.out_lines):
if '\n' in line:
for new_idx, new_line in enumerate(
sort_imports.out_lines.pop(idx).splitlines()):
sort_imports.out_lines.pop(idx).splitlines()
):
sort_imports.out_lines.insert(idx + new_idx, new_line)


Expand All @@ -186,12 +169,10 @@ 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)
isort_config = isort.settings.Config(settings_path=file_path.parent)
else:
file_path = None
isort_config = isort.settings.Config(
settings_path=Path.cwd())
isort_config = isort.settings.Config(settings_path=Path.cwd())
input_string = ''.join(self.lines)
traceback = ''
isort_changed = False
Expand All @@ -204,19 +185,23 @@ def run(self):
input_stream=input_stream,
output_stream=output_stream,
config=isort_config,
file_path=file_path)
file_path=file_path,
)
except isort.exceptions.FileSkipped:
pass
except isort.exceptions.ISortError as e:
warnings.warn(e)
if isort_changed:
outlines = output_stream.getvalue()
diff_delta = "".join(unified_diff(
input_string.splitlines(keepends=True),
outlines.splitlines(keepends=True),
fromfile="{}:before".format(self.filename),
tofile="{}:after".format(self.filename)))
traceback = (isort_stdout.getvalue() + "\n" + diff_delta)
diff_delta = ''.join(
unified_diff(
input_string.splitlines(keepends=True),
outlines.splitlines(keepends=True),
fromfile=f'{self.filename}:before',
tofile=f'{self.filename}:after',
)
)
traceback = f'{isort_stdout.getvalue()}\n{diff_delta}'
for line_num, message in self.isort_linenum_msg(diff_delta):
if self.show_traceback:
message += traceback
Expand Down
23 changes: 23 additions & 0 deletions requirements.in
@@ -0,0 +1,23 @@
bandit
black
codespell
coveralls
flake8-blind-except
flake8-bugbear
flake8-comprehensions
flake8-debugger
flake8-deprecated
flake8-isort
flake8-pep3101
flake8-print
flake8-quotes
flake8-todo
importlib-metadata; python_version < '3.8'
isort
mypy
pytest
pytest-cov
pyupgrade
safety
typed-ast; python_version < '3.8' # dependency of black and mypy
zipp; python_version < '3.8' # dependency of importlib-metadata