From 3ec09808d5b2ae935bbd837a7ed105e142bbe3fa Mon Sep 17 00:00:00 2001 From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Mon, 17 Apr 2023 13:44:24 -0700 Subject: [PATCH 1/2] 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. --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6f4b78d..c056093 100644 --- a/README.md +++ b/README.md @@ -131,9 +131,10 @@ 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+']") From 8bc077b1d8c9fab6dbee00e5cf3de64128e802a3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 29 Apr 2023 19:55:32 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index c056093..0eddd9e 100644 --- a/README.md +++ b/README.md @@ -132,6 +132,7 @@ Then, in your code, import a TOML parser using the following fallback mechanism: ```python import sys + if sys.version_info >= (3, 11): import tomllib else: