Skip to content

Commit

Permalink
fix: better typing
Browse files Browse the repository at this point in the history
Signed-off-by: Frost Ming <[email protected]>
  • Loading branch information
frostming committed Jul 2, 2024
1 parent 15052dd commit 3e21350
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
20 changes: 12 additions & 8 deletions src/dep_logic/tags/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
from dataclasses import dataclass
from enum import Enum
from functools import cached_property
from typing import TYPE_CHECKING

from . import os

if TYPE_CHECKING:
from typing import Self


class PlatformError(Exception):
pass
Expand All @@ -19,7 +23,7 @@ class Platform:
arch: Arch

@classmethod
def parse(cls, platform: str) -> Platform:
def parse(cls, platform: str) -> Self:
"""Parse a platform string (e.g., `linux_x86_64`, `macosx_10_9_x86_64`, or `win_amd64`)
Available operating systems:
Expand Down Expand Up @@ -47,18 +51,18 @@ def parse(cls, platform: str) -> Platform:
return cls(os.Macos(12, 0), Arch.Aarch64)
elif platform == "alpine":
return cls(os.Musllinux(1, 2), Arch.X86_64)
elif platform == "windows_amd64":
return cls(os.Windows(), Arch.X86_64)
elif platform == "windows_x86":
return cls(os.Windows(), Arch.X86)
elif platform == "windows_arm64":
return cls(os.Windows(), Arch.Aarch64)
elif platform.startswith("windows_"):
return cls(os.Windows(), Arch.parse(platform.split("_")[1]))
elif platform == "macos_arm64":
return cls(os.Macos(12, 0), Arch.Aarch64)
elif platform == "macos_x86_64":
return cls(os.Macos(12, 0), Arch.X86_64)
elif platform.startswith("macos_"):
parts = platform.split("_")
if len(parts) < 4:
raise PlatformError(
f"Unsupported platform {platform}, expected one of {cls.choices()}"
)
major = int(parts[1])
minor = int(parts[2])
return cls(os.Macos(major, minor), Arch.parse(parts[3]))
Expand All @@ -85,7 +89,7 @@ def __str__(self) -> str:
return f"{self.os}_{self.arch}"

@classmethod
def current(cls) -> Platform:
def current(cls) -> Self:
"""Return the current platform."""
import platform

Expand Down
12 changes: 6 additions & 6 deletions src/dep_logic/tags/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from .platform import Platform

if TYPE_CHECKING:
from typing import Literal
from typing import Literal, Self


def parse_wheel_tags(filename: str) -> tuple[list[str], list[str], list[str]]:
Expand Down Expand Up @@ -63,7 +63,7 @@ def short(self) -> str:
return "pt"

@classmethod
def current(cls) -> Implementation:
def current(cls) -> Self:
import sysconfig

implementation = python_implementation()
Expand All @@ -73,11 +73,11 @@ def current(cls) -> Implementation:
)

@classmethod
def parse(cls, name: str, gil_disabled: bool = False) -> Implementation:
def parse(cls, name: str, gil_disabled: bool = False) -> Self:
if gil_disabled and name != "cpython":
raise UnsupportedImplementation("Only CPython supports GIL disabled mode")
if name in ("cpython", "pypy", "pyston"):
return Implementation(name, gil_disabled)
return cls(name, gil_disabled)
else:
raise UnsupportedImplementation(
f"Unsupported implementation: {name}, expected cpython, pypy, or pyston"
Expand Down Expand Up @@ -105,15 +105,15 @@ def from_spec(
platform: str,
implementation: str,
gil_disabled: bool = False,
) -> EnvSpec:
) -> Self:
return cls(
_ensure_version_specifier(requires_python),
Platform.parse(platform),
Implementation.parse(implementation, gil_disabled=gil_disabled),
)

@classmethod
def current(cls) -> EnvSpec:
def current(cls) -> Self:
requires_python = _ensure_version_specifier(f"=={python_version()}")
platform = Platform.current()
implementation = Implementation.current()
Expand Down

0 comments on commit 3e21350

Please sign in to comment.