Skip to content

Commit

Permalink
chore(type): improve typing for bases functions (#609)
Browse files Browse the repository at this point in the history
Where possible, we use more specific typing, but still fall back to the
general typing used.
  • Loading branch information
lengau authored Jul 31, 2024
1 parent 604233d commit 7b2fee5
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions craft_providers/bases/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

# Backward compatible, will be removed in 2.0
import sys
from typing import Dict, NamedTuple, Tuple, Type, Union
from typing import Dict, Literal, NamedTuple, Tuple, Type, Union, overload

from craft_providers.errors import BaseCompatibilityError, BaseConfigurationError
from craft_providers.base import Base
Expand Down Expand Up @@ -67,9 +67,21 @@ class BaseName(NamedTuple):
}


@overload
def get_base_alias(
base_name: Tuple[str, str],
) -> BaseAlias:
base_name: Tuple[Literal["ubuntu"], str]
) -> ubuntu.BuilddBaseAlias: ...
@overload
def get_base_alias(
base_name: Tuple[Literal["centos"], str]
) -> centos.CentOSBaseAlias: ...
@overload
def get_base_alias(
base_name: Tuple[Literal["almalinux"], str]
) -> almalinux.AlmaLinuxBaseAlias: ...
@overload
def get_base_alias(base_name: BaseName) -> BaseAlias: ...
def get_base_alias(base_name):
"""Return a Base alias from a base (name, version) tuple."""
base_name = BaseName(*base_name)
if base_name.name == "ubuntu" and base_name in BASE_NAME_TO_BASE_ALIAS:
Expand All @@ -83,9 +95,15 @@ def get_base_alias(
raise BaseConfigurationError(f"Base alias not found for {base_name}")


@overload
def get_base_from_alias(alias: ubuntu.BuilddBaseAlias) -> Type[ubuntu.BuilddBase]: ...
@overload
def get_base_from_alias(alias: centos.CentOSBaseAlias) -> Type[centos.CentOSBase]: ...
@overload
def get_base_from_alias(
alias: BaseAlias,
) -> Type[Base]:
alias: almalinux.AlmaLinuxBaseAlias,
) -> Type[almalinux.AlmaLinuxBase]: ...
def get_base_from_alias(alias: BaseAlias) -> Type[Base]:
"""Return a Base class from a known base alias."""
if isinstance(alias, ubuntu.BuilddBaseAlias):
return ubuntu.BuilddBase
Expand Down

0 comments on commit 7b2fee5

Please sign in to comment.