Skip to content

Commit

Permalink
Use tomli library for Python < 3.11 (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
tr11 authored Feb 19, 2024
1 parent 2f28254 commit 88b7f3c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11"]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]

steps:
- name: Checkout
Expand Down
16 changes: 6 additions & 10 deletions config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,15 @@
except ImportError: # pragma: no cover
yaml = None

if sys.version_info[1] < 11:
if sys.version_info < (3, 11): # pragma: no cover
try:
import toml
import tomli as toml

toml_readtype = "rt"
except ImportError: # pragma: no cover
toml = None
toml_readtype = ""
else:
except ImportError:
toml = None # type: ignore
else: # pragma: no cover
import tomllib as toml

toml_readtype = "rb"


from .configuration import Configuration
from .configuration_set import ConfigurationSet
Expand Down Expand Up @@ -871,7 +867,7 @@ def _reload(self, data: Union[str, TextIO], read_from_file: bool = False) -> Non
"""Reload the TOML data."""
if read_from_file:
if isinstance(data, str):
with open(data, toml_readtype) as f:
with open(data, "rb") as f:
loaded = toml.load(f)
else:
loaded = toml.load(data) # type: ignore [arg-type,unused-ignore]
Expand Down
48 changes: 29 additions & 19 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ azure-identity = { version = "^1.13.0", optional = true }
azure-keyvault = { version = "^4.2.0", optional = true }
boto3 = { version = "^1.28.20", optional = true }
google-cloud-secret-manager = { version = "^2.16.3", optional = true }
hvac = { version ="^1.1.1", optional = true }
hvac = { version = "^1.1.1", optional = true }
pyyaml = { version = "^6.0", optional = true }
toml = { version = "^0.10.0", optional = true }
jsonschema = { version = "^4.18.6", optional = true }
tomli = { version = "^2.0.1", optional = true }

[tool.poetry.group.dev.dependencies]
mypy = "^1.4.1"
Expand All @@ -40,22 +40,36 @@ pytest-ruff = "^0.2.1"
aws = ["boto3"]
azure = ["azure-keyvault", "azure-identity"]
gcp = ["google-cloud-secret-manager"]
toml = ["toml"]
toml = ["tomli"]
vault = ["hvac"]
yaml = ["pyyaml"]
validation = ["jsonschema"]

[tool.ruff]
line-length = 88
select = ['F', 'E', 'W', 'I', 'N', 'D', 'B', 'A', 'COM', 'C4', 'T20', 'Q', 'SIM']
select = [
'F',
'E',
'W',
'I',
'N',
'D',
'B',
'A',
'COM',
'C4',
'T20',
'Q',
'SIM',
]
exclude = ["tests", "docs"]


[tool.tox]
legacy_tox_ini = """
[tox]
isolated_build = true
envlist = py38, py39, py310, py311
envlist = py38, py39, py310, py311, py312
[testenv]
allowlist_externals = poetry
Expand All @@ -76,40 +90,36 @@ disallow_untyped_decorators = true
no_implicit_optional = true
warn_unused_ignores = true
warn_redundant_casts = true
exclude = [
'tests'
]
exclude = ['tests']

[[tool.mypy.overrides]]
module= [
'google.auth.credentials',
'yaml',
'toml',
module = [
'google.auth.credentials',
'yaml',
'toml',
'boto3',
'botocore.exceptions',
'botocore.exceptions',
'hvac',
'hvac.exceptions',
'jsonschema',
'jsonschema.exceptions'
'jsonschema.exceptions',
]
ignore_missing_imports = true

[tool.coverage.run]
branch = true
include = [
'config/*'
]
include = ['config/*']

[tool.coverage.html]
directory = 'cover'

[tool.pytest.ini_options]
minversion = "6.0"
addopts = '--cov --cov-report=html --cov-report term-missing --ruff --mypy --black'
filterwarnings =[
filterwarnings = [
'ignore::pytest.PytestDeprecationWarning',
'ignore::DeprecationWarning',
'ignore::pytest.PytestWarning'
'ignore::pytest.PytestWarning',
]

[build-system]
Expand Down
11 changes: 1 addition & 10 deletions tests/test_toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,7 @@ def test_load_toml_file(): # type: ignore
with tempfile.NamedTemporaryFile() as f:
f.file.write(TOML.encode())
f.file.flush()

# Two different toml libraries are in use
import sys

if sys.version_info[1] < 11:
read_type = "rt"
else:
read_type = "rb"

cfg = config_from_toml(open(f.name, read_type), read_from_file=True)
cfg = config_from_toml(open(f.name, "rb"), read_from_file=True)
assert cfg["a1.b1.c1"] == 1
assert cfg["a1.b1"].as_dict() == {"c1": 1, "c2": 2, "c3": 3}
assert cfg["a1.b2"].as_dict() == {"c1": "a", "c2": True, "c3": 1.1}
Expand Down

0 comments on commit 88b7f3c

Please sign in to comment.