From a6138675bcca68eea5b8abec7c2ec06d57f965a0 Mon Sep 17 00:00:00 2001 From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Tue, 12 Dec 2023 03:55:13 -0800 Subject: [PATCH] Use sys.version_info in compatibility layer (#220) * Use sys.version_info in compatibility layer Fixes #219 Static type checkers are able to understand checks against sys.version_info much better than try-except. In general, if type checkers tried to handle conditional imports fancily, this would result in unsoundness. Another reason is that you may have tomli conditionally installed based on Python version, in which case a type checker would not even have a way of knowing what tomli is. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Taneli Hukkinen <3275109+hukkin@users.noreply.github.com> --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6f4b78d..0eddd9e 100644 --- a/README.md +++ b/README.md @@ -131,9 +131,11 @@ tomli >= 1.1.0 ; python_version < "3.11" Then, in your code, import a TOML parser using the following fallback mechanism: ```python -try: +import sys + +if sys.version_info >= (3, 11): import tomllib -except ModuleNotFoundError: +else: import tomli as tomllib tomllib.loads("['This parses fine with Python 3.6+']")