Skip to content

Commit

Permalink
Use sys.version_info in compatibility layer (#220)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people authored Dec 12, 2023
1 parent 39eff9b commit a613867
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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+']")
Expand Down

0 comments on commit a613867

Please sign in to comment.