feat: update to pydantic v2 and refactor accordingly

This commit is contained in:
cătălin 2023-07-22 13:29:46 +02:00
commit eeb1573f99
Signed by: catalin
GPG key ID: 0178DF42F43E5FD2
6 changed files with 1151 additions and 855 deletions

View file

@ -1,6 +1,3 @@
default_language_version:
python: python3.10
files: ^halig|tests$ files: ^halig|tests$
repos: repos:
@ -22,7 +19,7 @@ repos:
args: [ --fix=lf ] args: [ --fix=lf ]
- repo: https://github.com/charliermarsh/ruff-pre-commit - repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.267 rev: v0.0.278
hooks: hooks:
- id: ruff - id: ruff
args: args:
@ -30,7 +27,7 @@ repos:
- --exit-non-zero-on-fix - --exit-non-zero-on-fix
- repo: https://github.com/psf/black - repo: https://github.com/psf/black
rev: 23.3.0 rev: 23.7.0
hooks: hooks:
- id: black - id: black
pass_filenames: false pass_filenames: false

View file

@ -21,8 +21,9 @@ class NotebooksCommand(BaseCommand):
break break
for item in sorted(current_folder_path.iterdir()): for item in sorted(current_folder_path.iterdir()):
if item.is_dir(): if item.is_dir():
item_tree_node = current_tree_node.add(item.name) if item.name != ".git":
q.append((item, item_tree_node, depth + 1)) item_tree_node = current_tree_node.add(item.name)
q.append((item, item_tree_node, depth + 1))
else: else:
current_tree_node.add(item.name) current_tree_node.add(item.name)
return tree return tree

View file

@ -6,7 +6,9 @@ import httpx
import httpx_cache import httpx_cache
import platformdirs import platformdirs
import yaml import yaml
from pydantic import BaseSettings, DirectoryPath, FilePath, HttpUrl, validator from pydantic import DirectoryPath, Field, FilePath, HttpUrl, field_validator
from pydantic_core import Url
from pydantic_settings import BaseSettings, SettingsConfigDict
class Settings(BaseSettings): class Settings(BaseSettings):
@ -25,13 +27,18 @@ class Settings(BaseSettings):
""" """
notebooks_root_path: DirectoryPath notebooks_root_path: DirectoryPath
identity_paths: list[FilePath] = [Path("~/.ssh/id_ed25519").expanduser()] identity_paths: list[FilePath] = Field(
recipient_paths: list[FilePath | HttpUrl] = [ default=[Path("~/.ssh/id_ed25519").expanduser()],
Path("~/.ssh/id_ed25519.pub").expanduser(), )
] recipient_paths: list[FilePath | HttpUrl] = Field(
default=[
Path("~/.ssh/id_ed25519.pub").expanduser(),
],
)
@validator("identity_paths", "recipient_paths", pre=True) @field_validator("identity_paths", "recipient_paths", mode="before")
def validate_paths(cls, v: Any): # noqa: N805 @classmethod
def validate_paths(cls, v: Any):
if not isinstance(v, list): if not isinstance(v, list):
v = [v] v = [v]
new_v = [] new_v = []
@ -44,8 +51,9 @@ class Settings(BaseSettings):
) )
return new_v return new_v
@validator("notebooks_root_path", pre=True) @field_validator("notebooks_root_path", mode="before")
def validate_notebooks_path(cls, v: Any): # noqa: N805 @classmethod
def validate_notebooks_path(cls, v: Any):
if isinstance(v, str): if isinstance(v, str):
return Path(v).expanduser() return Path(v).expanduser()
if isinstance(v, Path): if isinstance(v, Path):
@ -62,7 +70,7 @@ class Settings(BaseSettings):
def load_public_keys(self) -> set[str]: def load_public_keys(self) -> set[str]:
keys = set() keys = set()
for path in self.recipient_paths: for path in self.recipient_paths:
if isinstance(path, HttpUrl): if isinstance(path, Url):
with httpx_cache.Client(cache=httpx_cache.FileCache()) as client: with httpx_cache.Client(cache=httpx_cache.FileCache()) as client:
response = client.get(str(path)) response = client.get(str(path))
if response.status_code == httpx.codes.OK: if response.status_code == httpx.codes.OK:
@ -74,8 +82,7 @@ class Settings(BaseSettings):
keys.add(f.read()) keys.add(f.read())
return keys return keys
class Config: model_config = SettingsConfigDict(env_prefix="halig_")
env_prefix = "halig_"
@lru_cache @lru_cache

1953
pdm.lock generated

File diff suppressed because it is too large Load diff

View file

@ -6,13 +6,14 @@ requires-python = ">=3.10"
dependencies = [ dependencies = [
"typer<1.0.0,>=0.6.1", "typer<1.0.0,>=0.6.1",
"rich>=13.3.3", "rich>=13.3.3",
"pydantic>=1.10.7", "pydantic>=2.0.3",
"pyyaml>=6.0", "pyyaml>=6.0",
"pyrage>=1.0.3", "pyrage>=1.1.1",
"pendulum>=2.1.2", "pendulum>=2.1.2",
"httpx>=0.24.0", "httpx>=0.24.0",
"platformdirs>=3.5.1", "platformdirs>=3.5.1",
"httpx-cache>=0.9.0", "httpx-cache>=0.9.0",
"pydantic-settings>=2.0.2",
] ]
name = "halig" name = "halig"
dynamic = ["version"] dynamic = ["version"]
@ -76,6 +77,9 @@ docs = [
"mkdocs-material>=9.1.5", "mkdocs-material>=9.1.5",
"mkdocstrings[python]>=0.20.0", "mkdocstrings[python]>=0.20.0",
] ]
dev = [
"bump-pydantic>=0.6.0",
]
[tool.pytest] [tool.pytest]
mock_use_standalone_module = true mock_use_standalone_module = true

View file

@ -11,7 +11,7 @@ def test_settings_from_env(settings: Settings, notebooks_root_path_envvar):
def test_settings_from_non_existing_file_raises_value_error(): def test_settings_from_non_existing_file_raises_value_error():
with pytest.raises(ValueError, match="field required"): with pytest.raises(ValueError, match="Field required"):
Settings() # type: ignore[call-arg] Settings() # type: ignore[call-arg]