Skip to main content

py.test plugin to check import ordering using isort

Latest Version CI Status

Support

Python 3.7, 3.8, 3.9 and 3.10. pytest>=5.

Usage

install using pip:

pip install pytest-isort

Activate isort checks when calling py.test:

py.test --isort

This will execute an isort check against every .py file (if its not ignored).

Example

Given you have some files with incorrect sorted imports:

# content of file1.py

import os
import sys
import random

# content of file2.py

import json
import sys
import os

If you run py.test and activate the isort plugin you’ll se something like this:

$ py.test --isort
========================= test session starts ==========================
platform darwin -- Python 2.7.9 -- py-1.4.26 -- pytest-2.6.4
plugins: isort
collected 2 items

file1.py F
file2.py F

=============================== FAILURES ===============================
_____________________________ isort-check ______________________________
ERROR: file1.py Imports are incorrectly sorted.

 import os
+import random
 import sys
-import random
_____________________________ isort-check ______________________________
ERROR: file2.py Imports are incorrectly sorted.

 import json
+import os
 import sys
-import os
======================= 2 failed in 0.02 seconds =======================

If you can’t change the import ordering for file2.py, you have the option to exclude file2.py from isort checks.

Simply add the isort_ignore setting to your py.test configuration file:

[pytest]
isort_ignore =
    file2.py

Then re-run the tests:

$ py.test --isort
========================= test session starts ==========================
platform darwin -- Python 2.7.9 -- py-1.4.26 -- pytest-2.6.4
plugins: isort
collected 1 items

file1.py F

=============================== FAILURES ===============================
_____________________________ isort-check ______________________________
ERROR: file1.py Imports are incorrectly sorted.

 import os
+import random
 import sys
-import random
======================= 1 failed in 0.02 seconds =======================

As you can see, file2.py is ignored and not checked. Now fix the import ordering in file1.py and re-run the tests:

$ py.test --isort
========================= test session starts ==========================
platform darwin -- Python 2.7.9 -- py-1.4.26 -- pytest-2.6.4
plugins: isort
collected 1 items

file1.py .

======================= 1 passed in 0.01 seconds ======================

Everything is properly again. Congratulations!

If you run your testsuite again and again, py.test will only check changed files to speed up. You see this by adding -rs to your py.test options:

$ py.test --isort -rs
========================= test session starts ==========================
platform darwin -- Python 2.7.9 -- py-1.4.26 -- pytest-2.6.4
plugins: isort
collected 1 items

file1.py s
======================= short test summary info ========================
SKIP [1] pytest_isort.py:145: file(s) previously passed isort checks

====================== 1 skipped in 0.01 seconds ======================

Configuration

You can exclude files from isort checks by using the isort_ignore setting in your py.test configuration file (e.g. pytest.ini):

# content of setup.cfg
[pytest]
isort_ignore =
    docs/conf.py
    *migrations/*.py

This will ignore the conf.py python file inside the docs folder and also ignore any python file in migrations folders.

In addition, excluded files in isort’s configuration will be ignored too.

Notes

You can use isort to rewrite your python files and re-order the imports but this is not part of this plugin.

Release files for pytest-isort 4.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-isort 4.0.0
File Size Uploaded
pytest_isort-4.0.0.tar.gz 5.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-isort 4.0.0
File Interpreter ABI Platform
pytest_isort-4.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 12.5 kB

Release files / pytest_isort-4.0.0.tar.gz

Download URL pytest_isort-4.0.0.tar.gz
Size 5.6 kB
Tags Source
SHA-256 checksum
How to use checksums
00e99642e282b00b849cf9b49d9102a02ab8c4ec549ace57d7868b723713aaa9
BLAKE2b-256 checksum
How to use checksums
69f0024f4a2ee0b4799a720f352dd7ef9a99077b48bd2c050ff2b08c14feb41d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.12.2 Linux/6.5.0-1015-azure

Release files / pytest_isort-4.0.0-py3-none-any.whl

Download URL pytest_isort-4.0.0-py3-none-any.whl
Size 6.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
14bb3281bab587d6beb53129481e8885232249ec5cfeaf5d903a561ff0589620
BLAKE2b-256 checksum
How to use checksums
f1a6848a7bdec751e5648e7200c0e5339398eb9607666d83848840c8c1553a80
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.12.2 Linux/6.5.0-1015-azure

Release history Release notifications | RSS feed

This release

4.0.0 This release

2 release files

3.1.0

2 release files

3.0.0

2 release files

2.0.0

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page