From 16c9e050cd711c745112aec4f919d1862a2dd32b Mon Sep 17 00:00:00 2001 From: MartinBelthle Date: Wed, 31 Jul 2024 09:45:32 +0200 Subject: [PATCH] v1.0.1 (#7) --- pyproject.toml | 16 ++++++++-------- src/antares/study/version/cli.py | 7 +++++-- src/antares/study/version/model/study_antares.py | 13 +++++++------ src/antares/study/version/model/study_version.py | 3 +++ .../study/version/upgrade_app/__init__.py | 4 ++-- .../version/upgrade_app/scenario_mapping.py | 2 +- .../study/version/upgrade_app/upgrade_method.py | 2 +- tests/test_cli.py | 10 ++++++---- tests/test_using_pydantic.py | 10 +++++----- tests/upgrade_app/test_filter_out_child_files.py | 11 ++++++----- 10 files changed, 44 insertions(+), 34 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 36bfe36..0822f19 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -50,10 +50,10 @@ packages = ["src/antares"] [tool.hatch.envs.default] dependencies = [ - "coverage[toml]>=6.5", - "pytest<8", - "pydantic<2", - "pytest-freezer<0.5", + "coverage[toml]>=7.6.0", + "pytest>=8.2.2", + "pydantic>=2.8.2", + "freezegun~=1.5.1" ] @@ -77,10 +77,10 @@ python = ["3.8", "3.9", "3.10", "3.11", "3.12"] [tool.hatch.envs.types] dependencies = [ - "pytest<8", - "pydantic<2", - "pytest-freezer<0.5", - "mypy>=1.0.0", + "pytest>=8.2.2", + "pydantic>=2.8.2", + "mypy>=1.10.1", + "freezegun~=1.5.1" ] diff --git a/src/antares/study/version/cli.py b/src/antares/study/version/cli.py index beb9bd9..a1e99b5 100644 --- a/src/antares/study/version/cli.py +++ b/src/antares/study/version/cli.py @@ -7,8 +7,11 @@ - antares-study-version create: create a new study. """ +from pathlib import Path + import click +from antares.study.version import StudyVersion from antares.study.version.__about__ import __date__, __version__ from antares.study.version.create_app import CreateApp, available_versions from antares.study.version.exceptions import ApplicationError @@ -38,7 +41,7 @@ def show(study_dir: str) -> None: STUDY_DIR: The directory containing the study. """ try: - app = ShowApp(study_dir) # type: ignore + app = ShowApp(Path(study_dir)) except (ValueError, FileNotFoundError) as e: click.echo(f"Error: {e}", err=True) raise click.Abort() @@ -102,7 +105,7 @@ def create(study_dir: str, caption: str, version: str, author: str) -> None: STUDY_DIR: The directory where the study will be created. """ try: - app = CreateApp(study_dir, caption=caption, version=version, author=author) # type: ignore + app = CreateApp(Path(study_dir), caption=caption, version=StudyVersion.parse(version), author=author) except (ValueError, FileExistsError) as e: click.echo(f"Error: {e}", err=True) raise click.Abort() diff --git a/src/antares/study/version/model/study_antares.py b/src/antares/study/version/model/study_antares.py index 08490d3..dcadfb4 100644 --- a/src/antares/study/version/model/study_antares.py +++ b/src/antares/study/version/model/study_antares.py @@ -94,10 +94,7 @@ def __post_init__(self) -> None: @classmethod def _validate_caption(cls, value: t.Any) -> str: - value = str(value).strip() - if value: - return value - raise ValueError("Caption cannot be empty") + return str(value).strip() @classmethod def _validate_version(cls, value: t.Any) -> StudyVersion: @@ -108,7 +105,11 @@ def _validate_date(cls, value: t.Any) -> datetime.datetime: if isinstance(value, datetime.datetime): return value if isinstance(value, str): - value = float(value) + try: + value = float(value) + except ValueError: + return datetime.datetime.utcnow() + return datetime.datetime.utcfromtimestamp(value) _validate_created_date = _validate_date @@ -146,7 +147,7 @@ def from_ini_file(cls, study_dir: t.Union[str, Path]) -> "StudyAntares": section = parser["antares"] return cls( caption=section["caption"], - version=section["version"], # type: ignore + version=StudyVersion.parse(section["version"]), created_date=section["created"], # type: ignore last_save_date=section["lastsave"], # type: ignore author=section["author"], diff --git a/src/antares/study/version/model/study_version.py b/src/antares/study/version/model/study_version.py index 0e58aa4..9066e78 100644 --- a/src/antares/study/version/model/study_version.py +++ b/src/antares/study/version/model/study_version.py @@ -75,6 +75,9 @@ def __lt__(self, other): else: return NotImplemented + def __le__(self, other): + return self.__lt__(other) or self.__eq__(other) + # Format method def __format__(self, format_spec: str) -> str: diff --git a/src/antares/study/version/upgrade_app/__init__.py b/src/antares/study/version/upgrade_app/__init__.py index 02b567f..5353429 100644 --- a/src/antares/study/version/upgrade_app/__init__.py +++ b/src/antares/study/version/upgrade_app/__init__.py @@ -73,7 +73,7 @@ def study_antares(self) -> StudyAntares: try: return StudyAntares.from_ini_file(self.study_dir) except ValidationError as e: - raise ApplicationError(str(e)) from e + raise ApplicationError(e.args[0]) from e @property def upgrade_methods(self) -> t.List[UpgradeMethod]: @@ -83,7 +83,7 @@ def upgrade_methods(self) -> t.List[UpgradeMethod]: try: return scenarios[start:end] # type: ignore except KeyError as e: - raise ApplicationError(str(e)) from e + raise ApplicationError(e.args[0]) from e @property def should_denormalize(self) -> bool: diff --git a/src/antares/study/version/upgrade_app/scenario_mapping.py b/src/antares/study/version/upgrade_app/scenario_mapping.py index fba2f11..48bd2ac 100644 --- a/src/antares/study/version/upgrade_app/scenario_mapping.py +++ b/src/antares/study/version/upgrade_app/scenario_mapping.py @@ -99,7 +99,7 @@ def _iter_upgrade_methods( # Then, find the next upgrade methods until the end version curr = meth.new for meth in iter_methods: - if meth.can_upgrade(curr) and (end is None or meth.new <= end): # type: ignore + if meth.can_upgrade(curr) and (end is None or meth.new <= end): yield meth curr = meth.new else: diff --git a/src/antares/study/version/upgrade_app/upgrade_method.py b/src/antares/study/version/upgrade_app/upgrade_method.py index f197d23..ede47b9 100644 --- a/src/antares/study/version/upgrade_app/upgrade_method.py +++ b/src/antares/study/version/upgrade_app/upgrade_method.py @@ -30,7 +30,7 @@ def __post_init__(self): self.new = StudyVersion.parse(self.new) def can_upgrade(self, version: StudyVersion) -> bool: - return self.old <= version < self.new # type: ignore + return self.old <= version < self.new @classmethod def upgrade(cls, study_dir: Path) -> None: diff --git a/tests/test_cli.py b/tests/test_cli.py index f244d92..5c53578 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -29,7 +29,7 @@ def test_cli__version(self) -> None: def test_cli__show(self, study_dir: Path) -> None: runner = CliRunner() - result = runner.invoke(cli, ["show", str(study_dir)]) + result = runner.invoke(t.cast(click.BaseCommand, cli), ["show", str(study_dir)]) assert result.exit_code == 0 show_str = result.output.strip() assert "Caption: Thermal fleet optimization" in show_str @@ -45,7 +45,7 @@ def test_cli__create(self, tmp_path: Path, study_version: StudyVersion) -> None: study_dir = tmp_path / "My Study" runner = CliRunner() args = ["create", str(study_dir), f"--version={study_version:2d}", "--author=Jane Doe", "--caption=New Study"] - result = runner.invoke(cli, args) + result = runner.invoke(t.cast(click.BaseCommand, cli), args) assert result.exit_code == 0, result.output study_antares_file = study_dir / "study.antares" @@ -69,7 +69,7 @@ def test_cli__create(self, tmp_path: Path, study_version: StudyVersion) -> None: def test_cli__create__versions(self) -> None: runner = CliRunner() args = ["create", "--versions"] - result = runner.invoke(cli, args) + result = runner.invoke(t.cast(click.BaseCommand, cli), args) assert result.exit_code == 0 show_str = result.output.strip() @@ -78,7 +78,9 @@ def test_cli__create__versions(self) -> None: def test_upgrade__nominal_case(self, study_assets: StudyAssets) -> None: runner = CliRunner() target_version = "8.8" - result = runner.invoke(cli, ["upgrade", str(study_assets.study_dir), f"--version={target_version}"]) + result = runner.invoke( + t.cast(click.BaseCommand, cli), ["upgrade", str(study_assets.study_dir), f"--version={target_version}"] + ) assert result.exit_code == 0 # compare the content of the input directory diff --git a/tests/test_using_pydantic.py b/tests/test_using_pydantic.py index 49f725a..8474d96 100644 --- a/tests/test_using_pydantic.py +++ b/tests/test_using_pydantic.py @@ -4,8 +4,8 @@ import uuid from unittest import mock -import freezegun # type: ignore -from pydantic import BaseModel, Field, validator +import freezegun +from pydantic import BaseModel, Field, field_validator from antares.study.version import StudyVersion @@ -16,7 +16,7 @@ class StudyDTO(BaseModel): version: StudyVersion created: datetime.datetime = Field(default_factory=lambda: datetime.datetime.now(tz=datetime.timezone.utc)) - @validator("version", pre=True) + @field_validator("version", mode="before") def _validate_version(cls, v: t.Any) -> StudyVersion: return StudyVersion.parse(v) @@ -43,11 +43,11 @@ def test_init_with_string(self) -> None: def test_to_json(self) -> None: with mock.patch("uuid.uuid4", return_value=uuid.UUID("4930a577-63d2-4ea9-b0b9-581110d97475")): study = StudyDTO(name="foo", version=StudyVersion(4, 5)) - result = study.json() + result = study.model_dump_json() assert isinstance(result, str) # Compare dicts, since the order of the keys is not guaranteed assert json.loads(result) == { - "created": "2024-12-31T12:30:00+00:00", + "created": "2024-12-31T12:30:00Z", "id": "4930a577-63d2-4ea9-b0b9-581110d97475", "name": "foo", "version": {"major": 4, "minor": 5, "patch": 0}, diff --git a/tests/upgrade_app/test_filter_out_child_files.py b/tests/upgrade_app/test_filter_out_child_files.py index 8d8bed8..28a3637 100644 --- a/tests/upgrade_app/test_filter_out_child_files.py +++ b/tests/upgrade_app/test_filter_out_child_files.py @@ -1,4 +1,5 @@ import typing as t +from pathlib import Path import pytest @@ -14,16 +15,16 @@ ["document.txt"], ), ( - ["input", "input/other", "input/other/other"], + ["input", str(Path("input").joinpath("other")), str(Path("input") / "other" / "other")], ["input"], ), ( - ["input/other1.txt", "input/other2.txt"], - ["input/other1.txt", "input/other2.txt"], + [str(Path("input") / "other1.txt"), str(Path("input") / "other2.txt")], + [str(Path("input") / "other1.txt"), str(Path("input") / "other2.txt")], ), ( - ["input/other1.txt", "input/other2.txt", "other3.txt"], - ["input/other1.txt", "input/other2.txt", "other3.txt"], + [str(Path("input") / "other1.txt"), str(Path("input") / "other2.txt"), str(Path("input") / "other3.txt")], + [str(Path("input") / "other1.txt"), str(Path("input") / "other2.txt"), str(Path("input") / "other3.txt")], ), ], )