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

Recognize and replace if sys.version_info[0] blocks #918

Open
EwoutH opened this issue Nov 16, 2023 · 4 comments
Open

Recognize and replace if sys.version_info[0] blocks #918

EwoutH opened this issue Nov 16, 2023 · 4 comments

Comments

@EwoutH
Copy link

EwoutH commented Nov 16, 2023

pyupgrade currently does detect the if sys.version_info < (3, 5): syntax, it doesn't recognize the lesser but still sometimes used if sys.version_info[0] < 3: syntax in if or if-else blocks. It would be a nice feature if pyupgrade also could recognize and automatically replace such blocks.

A few examples I encountered in the wild:

if sys.version_info[0] < 3:
    import shutil
    shutil.rmtree(cls._tmpdir)
else:
    cls._tmpdir.cleanup()
if (sys.version_info[0] >=3 and
        isinstance(node, StringNode) and
        node.unicode_value is not None):
    return (node.unicode_value, node.pos)
return (node.compile_time_value(empty_scope), node.pos)
if sys.version_info[0] != 2:
    self.assertTrue(StringEncoding.string_contains_lone_surrogates("\uD800\uDFFF"))
@asottile
Copy link
Owner

I've never seen someone write that

@EwoutH
Copy link
Author

EwoutH commented Nov 16, 2023

For some reason Cython is full of it: cython/cython#5828/files (don't ask me why)

@hugovk
Copy link
Contributor

hugovk commented Nov 16, 2023

There's a lot of it out there: tensorflow, protobuf, opencv, pytorch...

And it's in the CPython 2-to-3 porting guide:

@jond01

This comment was marked as off-topic.

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

No branches or pull requests

4 participants