简体   繁体   中英

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM