Skip to content

Commit

Permalink
Adapt code to be compliant on python >=3.9
Browse files Browse the repository at this point in the history
  • Loading branch information
thomass-dev committed Oct 10, 2024
1 parent 3c3c4fa commit 64a499a
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 37 deletions.
35 changes: 15 additions & 20 deletions skore/src/skore/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,18 @@ def cli(args: list[str]):

parsed_args: argparse.Namespace = parser.parse_args(args)

match parsed_args.subcommand:
case None:
parser.print_help()
case "launch":
__launch(
project_name=parsed_args.project_name,
port=parsed_args.port,
open_browser=parsed_args.open_browser,
)
case "create":
__create(
project_name=parsed_args.project_name,
working_dir=parsed_args.working_dir,
)
case "quickstart":
__quickstart()
case _:
# `parser.parse_args` raises an error if an unknown subcommand is passed,
# so this case is impossible
return
if parsed_args.subcommand == "launch":
__launch(
project_name=parsed_args.project_name,
port=parsed_args.port,
open_browser=parsed_args.open_browser,
)
elif parsed_args.subcommand == "create":
__create(
project_name=parsed_args.project_name,
working_dir=parsed_args.working_dir,
)
elif parsed_args.subcommand == "quickstart":
__quickstart()

parser.print_help()
7 changes: 5 additions & 2 deletions skore/src/skore/cli/create_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pathlib import Path

from skore.cli import logger
from typing import Optional, Union


class ProjectNameTooLong(Exception):
Expand All @@ -25,7 +26,7 @@ class ImproperProjectName(Exception):
"""


def validate_project_name(project_name: str) -> (bool, Exception | None):
def validate_project_name(project_name: str) -> Union[bool, Exception, None]:
"""Validate the project name (the part before ".skore").
Returns `(True, None)` if validation succeeded and `(False, Exception(...))`
Expand Down Expand Up @@ -78,7 +79,9 @@ class ProjectPermissionError(Exception):
"""Permissions in the directory do not allow creating a file."""


def __create(project_name: str | Path, working_dir: Path | None = None) -> Path:
def __create(
project_name: Union[str, Path], working_dir: Optional[Path] = None
) -> Path:
"""Create a project file named according to `project_name`.
Parameters
Expand Down
3 changes: 2 additions & 1 deletion skore/src/skore/cli/launch_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from skore.cli import logger
from skore.project import load
from skore.ui.app import create_app
from typing import Union


class ProjectNotFound(Exception):
Expand All @@ -18,7 +19,7 @@ class ProjectNotFound(Exception):
project_path: Path


def __launch(project_name: str | Path, port: int, open_browser: bool):
def __launch(project_name: Union[str, Path], port: int, open_browser: bool):
"""Launch the UI to visualize a project.
Parameters
Expand Down
10 changes: 5 additions & 5 deletions skore/src/skore/item/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

import inspect
from abc import ABC, abstractmethod
from datetime import UTC, datetime
from datetime import datetime, timezone
from functools import cached_property
from typing import Any
from typing import Any, Optional


class Item(ABC):
Expand All @@ -33,10 +33,10 @@ class Item(ABC):

def __init__(
self,
created_at: str | None = None,
updated_at: str | None = None,
created_at: Optional[str] = None,
updated_at: Optional[str] = None,
):
now = datetime.now(tz=UTC).isoformat()
now = datetime.now(tz=timezone.utc).isoformat()

self.created_at = created_at or now
self.updated_at = updated_at or now
Expand Down
4 changes: 2 additions & 2 deletions skore/src/skore/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging
from functools import singledispatchmethod
from pathlib import Path
from typing import Any, Literal
from typing import Any, Literal, Union

from skore.item import (
Item,
Expand Down Expand Up @@ -163,7 +163,7 @@ class ProjectLoadError(Exception):
"""Failed to load project."""


def load(project_name: str | Path) -> Project:
def load(project_name: Union[str, Path]) -> Project:
"""Load an existing Project given a project name or path."""
# Transform a project name to a directory path:
# - Resolve relative path to current working directory,
Expand Down
3 changes: 2 additions & 1 deletion skore/src/skore/ui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
from skore.project import Project, load
from skore.ui.dependencies import get_static_path
from skore.ui.project_routes import router as project_router
from typing import Optional


def create_app(
project: Project | None = None, lifespan: Lifespan | None = None
project: Optional[Project] = None, lifespan: Optional[Lifespan] = None
) -> FastAPI:
"""FastAPI factory used to create the API to interact with `stores`."""
app = FastAPI(lifespan=lifespan)
Expand Down
4 changes: 2 additions & 2 deletions skore/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from datetime import UTC, datetime
from datetime import datetime, timezone

import pytest


@pytest.fixture
def mock_now():
return datetime.now(tz=UTC)
return datetime.now(tz=timezone.utc)


@pytest.fixture
Expand Down
8 changes: 4 additions & 4 deletions skore/tests/unit/item/test_item_repository.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from datetime import UTC, datetime
from datetime import datetime, timezone

import pytest
from skore.item import ItemRepository, MediaItem


class TestItemRepository:
def test_get_item(self):
now = datetime.now(tz=UTC).isoformat()
now = datetime.now(tz=timezone.utc).isoformat()
item_representation = dict(
media_bytes=b"media",
media_encoding="utf-8",
Expand Down Expand Up @@ -35,7 +35,7 @@ def test_get_item(self):
repository.get_item("key2")

def test_put_item(self):
now = datetime.now(tz=UTC).isoformat()
now = datetime.now(tz=timezone.utc).isoformat()
item = MediaItem(
media_bytes=b"media",
media_encoding="utf-8",
Expand All @@ -61,7 +61,7 @@ def test_put_item(self):
}
}

now2 = datetime.now(tz=UTC).isoformat()
now2 = datetime.now(tz=timezone.utc).isoformat()
item2 = MediaItem(
media_bytes=b"media2",
media_encoding="utf-8",
Expand Down

0 comments on commit 64a499a

Please sign in to comment.