Skip to content

Commit

Permalink
Use tomllib on Python >= 3.11 (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
abravalheri authored May 18, 2022
2 parents 6bf8ca4 + 68d0c99 commit ae8593f
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 14 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
Changelog
=========

Version 0.9
===========

- Use ``tomllib`` from the standard library in Python 3.11+, #42

Version 0.8.1
=============

Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ exclude =

[options.extras_require]
all =
tomli>=1.2.1
tomli>=1.2.1; python_version<"3.11"
packaging>=20.4
trove-classifiers>=2021.10.20

Expand Down
9 changes: 6 additions & 3 deletions src/validate_pyproject/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,17 @@
T = TypeVar("T", bound=NamedTuple)


try:
from tomli import TOMLDecodeError, loads
try: # pragma: no cover
if sys.version_info[:2] >= (3, 11):
from tomllib import TOMLDecodeError, loads
else:
from tomli import TOMLDecodeError, loads
except ImportError: # pragma: no cover
try:
from toml import TomlDecodeError as TOMLDecodeError # type: ignore
from toml import loads # type: ignore
except ImportError as ex:
raise ImportError("Please install a TOML parser (e.g. `tomli`)") from ex
raise ImportError("Please install `tomli` (TOML parser)") from ex

_REGULAR_EXCEPTIONS = (ValidationError, TOMLDecodeError)

Expand Down
10 changes: 10 additions & 0 deletions tests/helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import sys
from pathlib import Path

if sys.version_info[:2] >= (3, 11):
import tomllib

toml_ = tomllib
else:
import tomli

toml_ = tomli

HERE = Path(__file__).parent
EXAMPLES = HERE / "examples"
INVALID = HERE / "invalid-examples"
Expand Down
5 changes: 3 additions & 2 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
from functools import partial, wraps

import pytest
import tomli
from validate_pyproject._vendor import fastjsonschema as FJS

from validate_pyproject import api, errors, plugins, types

from .helpers import toml_

PYPA_SPECS = "https://packaging.python.org/en/latest/specifications"


Expand Down Expand Up @@ -84,7 +85,7 @@ class TestValidator:

@property
def valid_example(self):
return tomli.loads(self.example_toml)
return toml_.loads(self.example_toml)

@property
def invalid_example(self):
Expand Down
9 changes: 9 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import inspect
import logging
import sys
from pathlib import Path
from uuid import uuid4

Expand Down Expand Up @@ -161,3 +163,10 @@ def test_multiple_files(tmp_path, capsys):
captured = captured.replace(repl, "invalid file:")
assert number_valid == N
assert number_invalid == N + 3


@pytest.mark.skipif(sys.version_info[:2] < (3, 11), reason="requires 3.11+")
def test_parser_is_tomllib():
"""Make sure Python >= 3.11 uses tomllib instead of tomli"""
module_name = inspect.getmodule(cli.loads).__name__
assert module_name.startswith("tomllib")
7 changes: 3 additions & 4 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import logging

import pytest
import tomli
from validate_pyproject._vendor.fastjsonschema import JsonSchemaValueException

from validate_pyproject import api, cli

from .helpers import EXAMPLES, INVALID, error_file, examples, invalid_examples
from .helpers import EXAMPLES, INVALID, error_file, examples, invalid_examples, toml_


@pytest.mark.parametrize("example", examples())
def test_examples_api(example):
toml_equivalent = tomli.loads((EXAMPLES / example).read_text())
toml_equivalent = toml_.loads((EXAMPLES / example).read_text())
validator = api.Validator()
return validator(toml_equivalent) is not None

Expand All @@ -25,7 +24,7 @@ def test_examples_cli(example):
def test_invalid_examples_api(example):
example_file = INVALID / example
expected_error = error_file(example_file).read_text("utf-8")
toml_equivalent = tomli.loads(example_file.read_text())
toml_equivalent = toml_.loads(example_file.read_text())
validator = api.Validator()
with pytest.raises(JsonSchemaValueException) as exc_info:
validator(toml_equivalent)
Expand Down
7 changes: 3 additions & 4 deletions tests/test_pre_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
from pathlib import Path

import pytest
import tomli
from validate_pyproject._vendor.fastjsonschema import JsonSchemaValueException

from validate_pyproject.pre_compile import cli, pre_compile

from .helpers import EXAMPLES, INVALID, error_file, examples, invalid_examples
from .helpers import EXAMPLES, INVALID, error_file, examples, invalid_examples, toml_

MAIN_FILE = "hello_world.py" # Let's use something different that `__init__.py`

Expand Down Expand Up @@ -139,7 +138,7 @@ def _validate(vendored_path, toml_equivalent):
@pytest.mark.parametrize("example", examples())
@pytest.mark.parametrize("pre_compiled", _PRE_COMPILED)
def test_examples_api(tmp_path, pre_compiled_validate, example, pre_compiled):
toml_equivalent = tomli.loads((EXAMPLES / example).read_text())
toml_equivalent = toml_.loads((EXAMPLES / example).read_text())
pre_compiled_path = pre_compiled(Path(tmp_path))
return pre_compiled_validate(pre_compiled_path, toml_equivalent) is not None

Expand All @@ -149,7 +148,7 @@ def test_examples_api(tmp_path, pre_compiled_validate, example, pre_compiled):
def test_invalid_examples_api(tmp_path, pre_compiled_validate, example, pre_compiled):
example_file = INVALID / example
expected_error = error_file(example_file).read_text("utf-8")
toml_equivalent = tomli.loads(example_file.read_text())
toml_equivalent = toml_.loads(example_file.read_text())
pre_compiled_path = pre_compiled(Path(tmp_path))
with pytest.raises(JsonSchemaValueException) as exc_info:
pre_compiled_validate(pre_compiled_path, toml_equivalent)
Expand Down

0 comments on commit ae8593f

Please sign in to comment.