diff --git a/condax/config.py b/condax/config.py index f2be6ae..a11731a 100644 --- a/condax/config.py +++ b/condax/config.py @@ -2,10 +2,10 @@ import platform import warnings from pathlib import Path -from typing import TYPE_CHECKING, Generator, Optional +from typing import TYPE_CHECKING, Generator, List, Optional import yaml -from pydantic import BaseSettings, Field +from pydantic import ConfigDict, Field with warnings.catch_warnings(): # requests which is a transitive dependency has some chatty warnings during import @@ -13,6 +13,7 @@ import requests # noqa: F401 from ensureconda.api import ensureconda +from pydantic_settings import BaseSettings if TYPE_CHECKING: from _typeshed import StrPath @@ -25,15 +26,13 @@ def is_windows() -> bool: class Config(BaseSettings): prefix_path: Path = Path("~").expanduser() / ".condax" link_destination: Path = Path("~").expanduser() / ".local" / "bin" - channels = ["conda-forge", "defaults"] + channels: List[str] = ["conda-forge", "defaults"] conda_executable: Optional[Path] = Field( default=ensureconda( mamba=True, micromamba=True, conda=True, conda_exe=True, no_install=True ) ) - - class Config: - env_prefix = "CONDAX_" + model_config = ConfigDict(env_prefix="CONDAX_") def ensure_conda_executable(self, require_mamba: bool = True): def candidates() -> "Generator[Optional[StrPath], None, None]": diff --git a/condax/core.py b/condax/core.py index e91e220..aeba321 100644 --- a/condax/core.py +++ b/condax/core.py @@ -5,7 +5,7 @@ import sys from enum import Enum from pathlib import Path -from typing import Any, Collection, Dict, Generator, List, Optional +from typing import Collection, Dict, Generator, List, Optional import typer @@ -130,17 +130,12 @@ def create_links( def prefix_metadata(env_prefix: Path) -> Generator[PrefixMetadata, None, None]: metadata_path = env_prefix / ".condax-metadata.json" if metadata_path.exists(): - ret = PrefixMetadata.parse_file(metadata_path) + ret = PrefixMetadata.model_validate_json(metadata_path.read_text()) else: ret = PrefixMetadata(prefix=env_prefix) yield ret - def encoder(obj: Any) -> "str | Any": - if isinstance(obj, Path): - return str(obj) - return obj - - metadata_path.write_text(ret.json(indent=2, exclude_unset=True, encoder=encoder)) + metadata_path.write_text(ret.model_dump_json(indent=2, exclude_unset=True)) def remove_links(executables_to_unlink: Collection[Path], env_prefix: Path) -> None: diff --git a/news/upgrade-pydantic.md b/news/upgrade-pydantic.md new file mode 100644 index 0000000..c7a0578 --- /dev/null +++ b/news/upgrade-pydantic.md @@ -0,0 +1,3 @@ +### Fixed: + +* Update to support v2 of Pydantic. diff --git a/pyproject.toml b/pyproject.toml index d25a86c..01180f3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,7 +21,8 @@ dependencies = [ "PyYAML", "ensureconda", "typer", - "pydantic", + "pydantic >=2", + "pydantic-settings", 'importlib_metadata; python_version < "3.8.0"', ] dynamic = ["version"]