pytorch
09896eda - Fix version comparisons for Python 3.6, 3.10 and 4 (#32389)

Commit
4 years ago
Fix version comparisons for Python 3.6, 3.10 and 4 (#32389) Summary: There's some code which uses `six.PY3`, similar to: ```python if six.PY3: print("Python 3+ code") else: print "Python 2 code" ``` Where: ```python PY3 = sys.version_info[0] == 3 ``` When run on Python 4, this will run the Python 2 code! Instead, use `six.PY2` and avoid `six.PY3`. --- Similarly, there's some `sys.version_info[0] == 3` checks, better done as `sys.version_info[0] >= 3`. --- Also, it's better to avoid comparing the `sys.version` string, as it makes assumptions that each version component is exactly one character long, which will break in Python 3.10: ```pycon >>> sys.version '3.8.1 (v3.8.1:1b293b6006, Dec 18 2019, 14:08:53) \n[Clang 6.0 (clang-600.0.57)]' >>> sys.version < "3.3" False >>> fake_v3_10 = '3.10.1 (v3.8.1:1b293b6006, Dec 18 2019, 14:08:53) \n[Clang 6.0 (clang-600.0.57)]' >>> fake_v3_10 < "3.3" True ``` --- Finally, I think the intention here is to skip when the Python version is < 3.6: ```python unittest.skipIf(sys.version_info[0] < 3 and sys.version_info[1] < 6, "dict not ordered") ``` However, it will really skip for Python 0.0-0.5, 1.0-1.5 and 2.0-2.5. It's best to compare to the `sys.version_info` tuple and not `sys.version_info[1]`: ```python unittest.skipIf(sys.version_info < (3, 6), "dict not ordered") ``` --- Found using https://github.com/asottile/flake8-2020: ```console $ pip install -U flake8-2020 $ flake8 --select YTT ``` Pull Request resolved: https://github.com/pytorch/pytorch/pull/32389 Reviewed By: zou3519 Differential Revision: D24424662 Pulled By: ezyang fbshipit-source-id: 1266c4dbcc8ae4d2e2e9b1d7357cba854562177c
Author
Parents
Loading