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$
repos:
@ -22,7 +19,7 @@ repos:
args: [ --fix=lf ]
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.267
rev: v0.0.278
hooks:
- id: ruff
args:
@ -30,7 +27,7 @@ repos:
- --exit-non-zero-on-fix
- repo: https://github.com/psf/black
rev: 23.3.0
rev: 23.7.0
hooks:
- id: black
pass_filenames: false

View file

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

View file

@ -6,7 +6,9 @@ import httpx
import httpx_cache
import platformdirs
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):
@ -25,13 +27,18 @@ class Settings(BaseSettings):
"""
notebooks_root_path: DirectoryPath
identity_paths: list[FilePath] = [Path("~/.ssh/id_ed25519").expanduser()]
recipient_paths: list[FilePath | HttpUrl] = [
Path("~/.ssh/id_ed25519.pub").expanduser(),
]
identity_paths: list[FilePath] = Field(
default=[Path("~/.ssh/id_ed25519").expanduser()],
)
recipient_paths: list[FilePath | HttpUrl] = Field(
default=[
Path("~/.ssh/id_ed25519.pub").expanduser(),
],
)
@validator("identity_paths", "recipient_paths", pre=True)
def validate_paths(cls, v: Any): # noqa: N805
@field_validator("identity_paths", "recipient_paths", mode="before")
@classmethod
def validate_paths(cls, v: Any):
if not isinstance(v, list):
v = [v]
new_v = []
@ -44,8 +51,9 @@ class Settings(BaseSettings):
)
return new_v
@validator("notebooks_root_path", pre=True)
def validate_notebooks_path(cls, v: Any): # noqa: N805
@field_validator("notebooks_root_path", mode="before")
@classmethod
def validate_notebooks_path(cls, v: Any):
if isinstance(v, str):
return Path(v).expanduser()
if isinstance(v, Path):
@ -62,7 +70,7 @@ class Settings(BaseSettings):
def load_public_keys(self) -> set[str]:
keys = set()
for path in self.recipient_paths:
if isinstance(path, HttpUrl):
if isinstance(path, Url):
with httpx_cache.Client(cache=httpx_cache.FileCache()) as client:
response = client.get(str(path))
if response.status_code == httpx.codes.OK:
@ -74,8 +82,7 @@ class Settings(BaseSettings):
keys.add(f.read())
return keys
class Config:
env_prefix = "halig_"
model_config = SettingsConfigDict(env_prefix="halig_")
@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 = [
"typer<1.0.0,>=0.6.1",
"rich>=13.3.3",
"pydantic>=1.10.7",
"pydantic>=2.0.3",
"pyyaml>=6.0",
"pyrage>=1.0.3",
"pyrage>=1.1.1",
"pendulum>=2.1.2",
"httpx>=0.24.0",
"platformdirs>=3.5.1",
"httpx-cache>=0.9.0",
"pydantic-settings>=2.0.2",
]
name = "halig"
dynamic = ["version"]
@ -76,6 +77,9 @@ docs = [
"mkdocs-material>=9.1.5",
"mkdocstrings[python]>=0.20.0",
]
dev = [
"bump-pydantic>=0.6.0",
]
[tool.pytest]
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():
with pytest.raises(ValueError, match="field required"):
with pytest.raises(ValueError, match="Field required"):
Settings() # type: ignore[call-arg]