Skip to content

Commit

Permalink
Merge pull request #50 from Contextualist/tomllib-migrate
Browse files Browse the repository at this point in the history
  • Loading branch information
coltonbh authored Oct 21, 2023
2 parents 61a9237 + 5f85105 commit 6fbf1c4
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 36 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@ on: [push, pull_request]
jobs:
tests:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8", "3.11"]
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
name: Set up Python
with:
python-version: "3.8"
python-version: ${{ matrix.python-version }}
- name: Install poetry
run: pip install poetry
- name: Install repo
run: poetry install --no-interaction --no-ansi
- name: Run tests
run: bash scripts/tests.sh
- name: Upload coverage HTML
if: ${{ matrix.python-version == '3.8' }}
uses: actions/upload-artifact@v3
with:
name: htmlcov
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ repos:
hooks:
- id: mypy
additional_dependencies:
[tokenize-rt==3.2.0, pydantic>=2.0.0, types-toml, types-pyyaml]
[tokenize-rt==3.2.0, pydantic>=2.0.0, types-pyyaml]
- repo: https://github.com/crate-ci/typos
rev: v1.16.10
hooks:
Expand Down
21 changes: 14 additions & 7 deletions chemcloud/http_client.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import json
import sys
from base64 import urlsafe_b64decode
from getpass import getpass
from pathlib import Path
from time import time
from typing import Any, Dict, List, Optional, Tuple, Union

import httpx
import toml

if sys.version_info >= (3, 11):
import tomllib
else:
import tomli as tomllib

import tomli_w
from qcio.utils import json_dumps

from chemcloud.models import FutureOutput, FutureOutputGroup, QCIOInputs
Expand Down Expand Up @@ -72,8 +79,8 @@ def _set_tokens(self) -> None:
access_token, refresh_token = self._tokens_from_username_password(un, pw)

elif credentials_file.is_file(): # Look for tokens in credentials file
with open(credentials_file) as f:
credentials = toml.load(f)
with open(credentials_file, "rb") as f:
credentials = tomllib.load(f)
try:
access_token = credentials[self._profile]["access_token"]
refresh_token = credentials[self._profile]["refresh_token"]
Expand Down Expand Up @@ -146,8 +153,8 @@ def write_tokens_to_credentials_file(
)

if credentials_file.is_file():
with open(credentials_file, "r") as f:
credentials = toml.load(f)
with open(credentials_file, "rb") as f:
credentials = tomllib.load(f)
else:
credentials_file.parent.mkdir(parents=True, exist_ok=True)
credentials = {}
Expand All @@ -156,8 +163,8 @@ def write_tokens_to_credentials_file(
"access_token": access_token,
"refresh_token": refresh_token,
}
with open(credentials_file, "w") as f:
toml.dump(credentials, f)
with open(credentials_file, "wb") as f:
tomli_w.dump(credentials, f)

def _request(
self,
Expand Down
2 changes: 2 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Changed

- Updated GitHub actions to run on `pull_request` in addition to `push`.
- Migrated dependency `uiri/toml` to `hukkin/tomli` (Python < 3.11) or the built-in `tomllib` (Python >= 3.11)
- Updated GitHub actions to test against both Python 3.8 and Python 3.11

## [0.8.2] - 2023-09-25

Expand Down
26 changes: 18 additions & 8 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ classifiers = [

[tool.poetry.dependencies]
python = "^3.8"
toml = "^0.10.2"
tomli = { version = ">=1.1.0", python = "<3.11" }
tomli-w = "^1.0.0"
httpx = "^0.23.1"
qcio = "^0.7.0"
pydantic = ">=2.0.0, !=2.4.0"
Expand All @@ -43,7 +44,6 @@ pytest-cov = "^4.1.0"
coverage = "^7.3.0"
pytest-httpx = "<0.23.0"
pytest-mock = "^3.11.1"
types-toml = "^0.10.8.7"


[tool.poetry.group.docs.dependencies]
Expand Down
6 changes: 3 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import Dict

import pytest
import toml
import tomli_w
from pytest_httpx import HTTPXMock
from qcio import Molecule

Expand Down Expand Up @@ -42,8 +42,8 @@ def _write_credentials_file(
credentials_file = (
settings.chemcloud_base_directory / settings.chemcloud_credentials_file
)
with open(credentials_file, "w+") as f:
toml.dump(credentials, f)
with open(credentials_file, "wb+") as f:
tomli_w.dump(credentials, f)

return _write_credentials_file

Expand Down
30 changes: 17 additions & 13 deletions tests/test_http_client.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import json
import re
import sys
from pathlib import Path

import toml
if sys.version_info >= (3, 11):
import tomllib
else:
import tomli as tomllib
from pytest_httpx import HTTPXMock
from qcio import Model, ProgramInput, SinglePointOutput

Expand Down Expand Up @@ -135,8 +139,8 @@ def test_write_tokens_to_credentials_file(settings):

assert credentials_file.is_file()

with open(credentials_file) as f:
data = toml.load(f)
with open(credentials_file, "rb") as f:
data = tomllib.load(f)

assert (
data[settings.chemcloud_credentials_profile]["access_token"]
Expand Down Expand Up @@ -166,8 +170,8 @@ def test_write_tokens_to_credentials_file_adds_new_profiles_to_credentials_file(

assert credentials_file.is_file()

with open(credentials_file) as f:
data = toml.load(f)
with open(credentials_file, "rb") as f:
data = tomllib.load(f)

assert (
data[settings.chemcloud_credentials_profile]["access_token"]
Expand All @@ -186,8 +190,8 @@ def test_write_tokens_to_credentials_file_adds_new_profiles_to_credentials_file(
new_profile_access_token, new_profile_refresh_token, profile=new_profile_name
)

with open(credentials_file) as f:
data = toml.load(f)
with open(credentials_file, "rb") as f:
data = tomllib.load(f)

# Default profile still exists
assert (
Expand Down Expand Up @@ -224,8 +228,8 @@ def test_write_tokens_to_credentials_file_overwrites_tokens_of_existing_profiles

assert credentials_file.is_file()

with open(credentials_file) as f:
data = toml.load(f)
with open(credentials_file, "rb") as f:
data = tomllib.load(f)

assert (
data[settings.chemcloud_credentials_profile]["access_token"]
Expand All @@ -241,8 +245,8 @@ def test_write_tokens_to_credentials_file_overwrites_tokens_of_existing_profiles

client.write_tokens_to_credentials_file(new_access_token, new_refresh_token)

with open(credentials_file) as f:
data = toml.load(f)
with open(credentials_file, "rb") as f:
data = tomllib.load(f)

# Default profile has new tokens
assert (
Expand Down Expand Up @@ -316,8 +320,8 @@ def test__refresh_tokens_writes_to_credentials_file_only_if_flag_set(
client._refresh_tokens(original_refresh_token)
assert credentials_file.is_file()

with open(credentials_file) as f:
data = toml.load(f)
with open(credentials_file, "rb") as f:
data = tomllib.load(f)

assert (
data[settings.chemcloud_credentials_profile]["access_token"]
Expand Down

0 comments on commit 6fbf1c4

Please sign in to comment.