Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⬆ Upgrade to pydantic~=2.7 #24

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions aiida_project/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Optional

import dotenv
from pydantic import BaseSettings
from pydantic_settings import BaseSettings, SettingsConfigDict
from rich import print

DEFAULT_PROJECT_STRUCTURE = {
Expand All @@ -23,19 +23,18 @@ class ProjectConfig(BaseSettings):
aiida_default_python_path: Optional[Path] = None
aiida_project_structure: dict = DEFAULT_PROJECT_STRUCTURE
aiida_project_shell: str = "bash"

class Config:
env_file = Path.home() / Path(".aiida_project.env")
env_file_encoding = "utf-8"
model_config = SettingsConfigDict(
env_file=Path.home() / Path(".aiida_project.env"), env_file_encoding="utf-8"
)

def is_not_initialised(self):
if dotenv.get_key(self.Config.env_file, "aiida_project_shell") is None:
if dotenv.get_key(self.model_config["env_file"], "aiida_project_shell") is None:
print("[bold red]Error:[/bold red] The AiiDA project config has not been initialised.")
print("[bold blue]Info:[/bold blue] Please run `aiida-project init` to get started.")
return True

def set_key(self, key, value):
dotenv.set_key(self.Config.env_file, key, value)
dotenv.set_key(self.model_config["env_file"], key, value)

def get_key(self, key):
return dotenv.get_key(key)
8 changes: 4 additions & 4 deletions aiida_project/project/venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@
import subprocess
from pathlib import Path

from aiida_project.project.base import BaseProject
from aiida_project.config import ProjectConfig
from aiida_project.project.base import BaseProject


class VenvProject(BaseProject):
"""An AiiDA environment based on `venv`."""

_engine = "venv"

shell_activate_mapping = {
shell_activate_mapping: dict = {
"bash": "activate",
"zsh": "activate",
"fish": "activate.fish",
}
shell_deactivate_mapping = {
shell_deactivate_mapping: dict = {
"bash": "deactivate () {",
"zsh": "deactivate () {",
"fish": 'function deactivate -d "Exit virtual environment and return to normal shell environment"',
"fish": 'function deactivate -d "Exit virtual environment"',
}

def create(self, python_path: Path) -> None:
Expand Down
11 changes: 7 additions & 4 deletions aiida_project/shell.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
"""Functionality to support various shells with `aiida-project`."""
from __future__ import annotations

from enum import Enum
from importlib import resources
from pydantic import BaseModel, validator
import yaml
from pathlib import Path

import yaml
from pydantic import BaseModel, field_validator


class ShellType(str, Enum):
bash = "bash"
Expand All @@ -23,7 +25,8 @@ class Shell(BaseModel):
deactivate: str
"""AiiDA-specific lines to add to the environment's deactivate script."""

@validator('config_file')
@field_validator("config_file")
@classmethod
def resolve_config_file(cls, value: Path) -> Path:
"""Resolve the shell configuration file."""
return Path.home() / value
Expand All @@ -33,6 +36,6 @@ def load_shell(shell_str: str) -> Shell:
"""Load the project class corresponding the engine type."""
from . import data

with (resources.files(data) / "shell_fields.yaml").open('r') as handle:
with (resources.files(data) / "shell_fields.yaml").open("r") as handle:
specs = yaml.safe_load(handle)
return Shell(**specs[shell_str])
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ keywords = ["aiida", "workflows"]
requires-python = ">=3.8"
dependencies = [
"py~=1.11",
"pydantic~=1.10,>=1.10.8",
"pydantic~=2.7",
"pydantic-settings~=2.2",
"python-dotenv~=1.0",
"typer[all]~=0.9",
"pyyaml~=6.0",
"eval_type_backport",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's this dependency for?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, been a while, but I think I added this because of pydantic/pydantic#7873. Not sure if it's still necessary, let me check.

]

[project.urls]
Expand All @@ -53,6 +55,7 @@ line-length = 100
module = [
"dotenv",
"pydantic",
"pydantic_settings",
"yaml",
"typer",
"rich"
Expand Down