From 66e95a061168a2c29bad75f80c5cd068fbb5f4ac Mon Sep 17 00:00:00 2001 From: Adrien Ferrand Date: Sat, 16 Sep 2023 22:11:49 +0200 Subject: [PATCH] Use mypy recommended conditional imports for compats (#154) Concerning conditional imports based on the actual Python version at runtime, Mypy recommendation is to explicitly use `sys.version_info` to trigger the relevant import depending on the Python version. Reference to the documentation: https://mypy.readthedocs.io/en/stable/common_issues.html#python-version-and-system-platform-checks Apply this approach to the `compat` module. As a consequence, Mypy does an educated guess on the imports, and `#type: ignore` inline comment is not needed anymore. --- src/pyotp/compat.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/pyotp/compat.py b/src/pyotp/compat.py index 79758c0..b2f058f 100644 --- a/src/pyotp/compat.py +++ b/src/pyotp/compat.py @@ -1,7 +1,9 @@ +import sys + # Use secrets module if available (Python version >= 3.6) per PEP 506 -try: - from secrets import SystemRandom # type: ignore -except ImportError: +if sys.version_info >= (3, 6): + from secrets import SystemRandom +else: from random import SystemRandom random = SystemRandom()