asottile-archive / flake8-2020 Goto Github PK
View Code? Open in Web Editor NEWflake8 plugin which checks for misuse of `sys.version` or `sys.version_info`
License: MIT License
flake8 plugin which checks for misuse of `sys.version` or `sys.version_info`
License: MIT License
Should it test version[0]
and not sys.version[0]
?
With this code:
import six
if not six.PY3:
pass
The plugin correctly gives:
$ flake8 --select YTT 1.py
1.py:2:8: YTT202: `six.PY3` referenced (python4), use `not six.PY2`
Perhaps it could detect the not
before six.PY3
, and suggest a clean six.PY2
instead?
For example:
$ flake8 --select YTT 1.py
1.py:2:X: YTT20X: `not six.PY3` referenced (python4), use `six.PY2`
That is:
# Bad
if not six.PY3:
# Better (sort of)
if not not six.PY2:
# Best
if six.PY2:
I have observed code using platform.python_version_tuple
to check version numbers, but since it is a tuple of strings it gets tripped up by Python 3.10:
>>> platform.python_version_tuple()
('3', '10', '0a3')
>>> platform.python_version_tuple() > ("3", "3")
False
>>> "10" > "3"
False
>>> platform.python_version_tuple()
('3', '8', '5')
>>> platform.python_version_tuple() > ("3", "3")
True
>>>
>>> "8" > "3"
True
Would you be interested in having a check for this?
I got this in a PR today:
python_version = float(sys.version_info.major) + 0.1 * float(
sys.version_info.minor,
)
flake8-2020
doesn't catch this, would it be a welcome addition?
Originally from #4 (comment)
Right now it's reported as YTT103
I haven't seen this one in the wild, but I think it's ~reasonably well defined for 3.0 - 3.9 so it's possible:
Python 3.5.7 (default, Mar 30 2019, 01:05:18)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.version >= '3.5'
True
Python 3.10.0a0 (default, Jul 18 2019, 06:46:14)
[GCC 7.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.version >= '3.5'
False
the version slice should probably only check for :1
and :3
as there's a few "legitimate" usages of slicing sys.version
I've seen in the wild, in particular here's one from cpython that seems to be well defined:
@hugovk submitted a pull request to urllib3 based on this plugin: urllib3/urllib3#1669. Thanks! But it failed to catch all issues, as PY3
was sometimes imported directly. See urllib3/urllib3#2004 for examples.
This may cause false positives, though.
This is used in three places in scipy, and I've seen this sort of thing in other repos too:
if sys.version_info.major >= 3 and sys.version_info.minor >= 5:
from math import gcd
else:
from fractions import gcd
The if
is incorrectly False
for 4.0 - 4.4, 5.0 - 5.4, etc.
To fix:
-if sys.version_info.major >= 3 and sys.version_info.minor >= 5:
+if sys.version_info >= (3, 5):
from math import gcd
else:
from fractions import gcd
Can this easily be detected?
Not sure whether this can be properly fixed now, but at least I'm going post is as feedback item.
I do expect libraries to follow at least some kind of semver-alike versioning. If there are breaking changes, I do expect major
version to be bumped. However it's not the case with flake8-2020
, we got an error when trying to update 1.6.1 -> 1.7.0
:
flake8-2020 1.7.0 depends on flake8>=5
We do use flake8 less than 5, because there are other plugins (namely, wemake-python-styleguide
) which require older flake8
. And changing to the newer flake8
is a major version bump. This was really unexpected when flake8-2020
got a major change breaking backward compatibility without bumping its major version.
Could you please check and maybe at least somehow improve the release process to avoid such issues in the future? Ideally would be to fix current releases, but I understand this is unlikely to happen....
Very similar to YTT101, but for platform.python_version
:
Python 3.10.0a2+ (heads/master:bbeb2d2, Nov 13 2020, 10:34:11)
[GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform
>>> platform.python_version()[:3]
'3.1'
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.