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

Y066 cases that can't be rewritten #491

Closed
tusharsadhwani opened this issue May 25, 2024 · 5 comments
Closed

Y066 cases that can't be rewritten #491

tusharsadhwani opened this issue May 25, 2024 · 5 comments
Labels

Comments

@tusharsadhwani
Copy link

tusharsadhwani commented May 25, 2024

Consider this case:

import sys

if sys.version_info < (3, 8):  # Y066 use "if sys.version_info >= (3, 8)"
    def bar(x): ...
elif sys.version_info >= (3, 10):
    def bar(x, *, bar=True): ...
else:
    def bar(x, *, bar=True, baz=False): ...

I don't think it is possible to rewrite this code with if sys.version_info >= (3, 8). Should Y066 be raised in this case?

@tusharsadhwani
Copy link
Author

Specifically, Y066's suggestions are only applicable if Y066 is raised on every if and elif condition in the chain.

@AlexWaygood
Copy link
Collaborator

Can't your example be rewritten to

if sys.version_info >= (3, 10):
    def bar(x, *, bar=True): ...
elif sys.version_info >= (3, 8):
    def bar(x, *, bar=True, baz=False): ...
else:
    def bar(x): ...

?

@tusharsadhwani
Copy link
Author

My bad! You're right, the else clause is equivalent to >= 3.8.

@tusharsadhwani
Copy link
Author

It does get a bit dicey in some cases, like:

if sys.version_info < (3, 8):  # Y066 use "if sys.version_info >= (3, 8)"
    def bar(x): ...
elif sys.version_info == (3, 10):
    def bar(x, *, bar=True): ...
else:
    def bar(x, *, bar=True, baz=False): ...

is not equivalent to:

if sys.version_info == (3, 10):
    def bar(x, *, bar=True): ...
elif sys.version_info >= (3, 8):
    def bar(x, *, bar=True, baz=False): ...
else:
    def bar(x): ...

For Python 3.11, it will execute the wrong block, but it's hard to argue about that one.

@AlexWaygood
Copy link
Collaborator

I think we have another rule that warns against == comparisons with sys.version_info, because sys.version_info is always a tuple longer than 2 elements, so sys.version_info == (3, 10) will always resolve to False

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants