flake8 plugin which checks for misuse of `sys.version` or `sys.version_info`
Project description
flake8-2020
flake8 plugin which checks for misuse of sys.version
or sys.version_info
this will become a problem when python3.10
or python4.0
exists (presumably
during the year 2020).
installation
pip install flake8-2020
flake8 codes
Code | Description |
---|---|
YTT101 | sys.version[:...] referenced (python3.10) |
YTT102 | sys.version[2] referenced (python3.10) |
YTT201 | sys.version_info[0] == 3 referenced (python4) |
YTT202 | six.PY3 referenced (python4) |
YTT301 | sys.version[0] referenced (python10) |
rationale
lots of code incorrectly references the sys.version
and sys.version_info
members. in particular, this will cause some issues when the version of python
after python3.9 is released. my current recommendation is 3.10 since I believe
it breaks less code, here's a few patterns that will cause issues:
# in python3.10 this will report as '3.1' (should be '3.10')
python_version = sys.version[:3] # YTT101
# in python3.10 this will report as '1' (should be '10')
py_minor = sys.version[2]
# correct way to do this
python_version = '{}.{}'.format(*sys.version_info)
py_minor = str(sys.version_info[1])
# in python4 this will report as `False` (and suddenly run python2 code!)
is_py3 = sys.version_info[0] == 3 # YTT201
# in python4 this will report as `False` (six violates YTT201!)
if six.PY3: # YTT202
print('python3!')
# correct way to do this
is_py3 = sys.version_info > (3,)
# in python10 this will report as '1'
python_major_version = sys.version[0] # YTT301
# correct way to do this
python_major_version = str(sys.version_info[0])
as a pre-commit hook
See pre-commit for instructions
Sample .pre-commit-config.yaml
:
- repo: https://gitlab.com/pycqa/flake8
rev: 3.7.8
hooks:
- id: flake8
additional_dependencies: [flake8-2020==1.0.0]
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file flake8_2020-1.0.0.tar.gz
.
File metadata
- Download URL: flake8_2020-1.0.0.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 549860ef81de5a6ffbc15c317aaeefc39586228295df890d9d3f6635ab200f39 |
|
MD5 | ab846fa41fa2e21a825036df48cd72ef |
|
BLAKE2b-256 | 5d12bdfa470286d7c23bbbbe9cd6cdd33b4d31d61294b5ca52617a3262b498bd |
Provenance
File details
Details for the file flake8_2020-1.0.0-py2.py3-none-any.whl
.
File metadata
- Download URL: flake8_2020-1.0.0-py2.py3-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d49a86d8df51810b3e9593ddfe6ee580c591a9fd264ae8d8ac5654417ccdf722 |
|
MD5 | 730d6a976944132376dc9e028c36c0ab |
|
BLAKE2b-256 | 268fbf632d60abf1036b256bb09df2a78749ae6b0184a6c2b146a45f15849d8f |