z0idb3rg
z0idb3rg

Reputation: 661

flake8 conditional with python version in stub file

I need to support both python 3.8 and versions lower that 3.8, but the package I need to import into my stub (*.pyi) file had different name in <3.8

import sys
if sys.version_info.minor < 8:
    import xyz
else:
    import zyx

In general this should do the job, but when I run flake8 with *.pyi files config (flake8 --config flake8-pyi.ini) I get this:

Y002 If test must be a simple comparison against sys.platform or sys.version_info

Any ideas what could be done about that?

Thanks in advance!

Upvotes: 0

Views: 227

Answers (1)

anthony sottile
anthony sottile

Reputation: 70175

From the description of flake8-pyi (a flake8 plugin, not part of flake8 itself):

Y002: If test must be a simple comparison against sys.platform or sys.version_info. Stub files support simple conditionals to indicate differences between Python versions or platforms, but type checkers only understand a limited subset of Python syntax, and this warning triggers on conditionals that type checkers will probably not understand.

The fix is to change your condition to:

if sys.version_info < (3, 8):

note that your code would break for 2.8 (yes, some people do this!) and 4.0 so you should be careful with eliding parts of comparisons ;) -- I've written a flake8 plugin which helps lint against conditions that might be problematic: flake8-2020

Upvotes: 0

Related Questions