Skip to content

Commit a613867

Browse files
hauntsaninjapre-commit-ci[bot]hukkin
authored
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 <[email protected]>
1 parent 39eff9b commit a613867

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,11 @@ tomli >= 1.1.0 ; python_version < "3.11"
131131
Then, in your code, import a TOML parser using the following fallback mechanism:
132132

133133
```python
134-
try:
134+
import sys
135+
136+
if sys.version_info >= (3, 11):
135137
import tomllib
136-
except ModuleNotFoundError:
138+
else:
137139
import tomli as tomllib
138140

139141
tomllib.loads("['This parses fine with Python 3.6+']")

0 commit comments

Comments
 (0)