Skip to content

Commit

Permalink
Merge pull request #67 from maresb/pydantic2
Browse files Browse the repository at this point in the history
Upgrade to Pydantic v2
  • Loading branch information
mariusvniekerk authored Jul 9, 2023
2 parents fcbb5c6 + c92bba2 commit 8b91ddb
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 15 deletions.
11 changes: 5 additions & 6 deletions condax/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@
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
warnings.simplefilter("ignore", Warning)
import requests # noqa: F401

from ensureconda.api import ensureconda
from pydantic_settings import BaseSettings

if TYPE_CHECKING:
from _typeshed import StrPath
Expand All @@ -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]":
Expand Down
11 changes: 3 additions & 8 deletions condax/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions news/upgrade-pydantic.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Fixed:

* Update to support v2 of Pydantic.
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ dependencies = [
"PyYAML",
"ensureconda",
"typer",
"pydantic",
"pydantic >=2",
"pydantic-settings",
'importlib_metadata; python_version < "3.8.0"',
]
dynamic = ["version"]
Expand Down

0 comments on commit 8b91ddb

Please sign in to comment.