Skip to content

Commit

Permalink
Use mypy recommended conditional imports for compats (#154)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
adferrand authored Sep 16, 2023
1 parent 6833891 commit 66e95a0
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/pyotp/compat.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 66e95a0

Please sign in to comment.