Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

UP036 should identify sys.version_info[0] or other parts #12993

Closed
inoa-jboliveira opened this issue Aug 19, 2024 · 4 comments · Fixed by #13175
Closed

UP036 should identify sys.version_info[0] or other parts #12993

inoa-jboliveira opened this issue Aug 19, 2024 · 4 comments · Fixed by #13175
Assignees
Labels
rule Implementing or modifying a lint rule

Comments

@inoa-jboliveira
Copy link

inoa-jboliveira commented Aug 19, 2024

I found there was some leftover py2k code in a project that UP rules would not catch because it just matches sys.version_info as a tuple. If you just check for the 1st element, which is kinda common in the old days, it will not show an error.

import sys

if sys.version_info[0] < 3:
    print('old')

if sys.version_info < (3, 0):
    print('also old')

UP036

$ ruff check --isolated --select UP036 foo.py
foo.py:6:4: UP036 Version block is outdated for minimum Python version
  |
4 |     print('old')
5 |
6 | if sys.version_info < (3, 0):
  |    ^^^^^^^^^^^^^^^^^^^^^^^^^ UP036
7 |     print('also old')
  |
  = help: Remove outdated version block

Found 1 error.
No fixes available (1 hidden fix can be enabled with the `--unsafe-fixes` option).

It would be helpful if both options were found. I believe this is only relevant for python 2 since python 4 is no longer going to exist, but python 3 will be the forever version with the 2nd number being the de facto python version.

If so, this rule could also catch these cases.

if sys.version_info[1] < 12: ...

In time: Ruff 0.6.1

@dhruvmanila
Copy link
Member

I think checking against sys.version_info[0] seems reasonable, not sure about sys.version_info[1]. What do you think @AlexWaygood ?

@dhruvmanila dhruvmanila added the rule Implementing or modifying a lint rule label Aug 20, 2024
@inoa-jboliveira
Copy link
Author

inoa-jboliveira commented Aug 20, 2024

I think sys.version_info[1] would only be relevant when PEP 2026 gets accepted, which makes the number "3" useless.

Also ruff only runs in Python 3 code.

I imagine in a few years people saying: "this thing runs only on python 28 or 29" instead of 3.28 and 3.29

For a litmus test, you can check how often this is used on github repos. There several usages together with [0] and also by itself.

https://github.com/search?q=sys.version_info%5B1%5D+language%3Apython&type=code

@MichaReiser
Copy link
Member

I would prefer waiting with sys.version_info[1] until a PEP makes python 3 officially the final major version.

@inoa-jboliveira
Copy link
Author

Cool, thank you!

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants