Skip to content

Commit

Permalink
Fix Python 3.10 errors
Browse files Browse the repository at this point in the history
typing.Self requires Python 3.11. Replaced it with the class name for
types that aren't subclassed or a TypeVar for types that are.

The type annotation on Hardware.from_dict() must be given as a string,
or pyupgrade will convert it to type[_Self], which breaks because
Hardware has a member named "type" (which cannot be renamed since it
must match the name of the YAML files the class represents).

Fixes #9
  • Loading branch information
joelspadin committed Oct 6, 2024
1 parent 42d1143 commit ff153c2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
4 changes: 2 additions & 2 deletions zmk/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from collections.abc import Iterable, Mapping, Sequence
from dataclasses import asdict, dataclass, field
from pathlib import Path
from typing import Any, Self, TypeVar, cast, overload
from typing import Any, TypeVar, cast, overload

import dacite

Expand Down Expand Up @@ -57,7 +57,7 @@ class BuildMatrix:
_data: dict[str, Any] | None

@classmethod
def from_repo(cls, repo: Repo) -> Self:
def from_repo(cls, repo: Repo) -> "BuildMatrix":
"""Get the build matrix for a repo"""
return cls(repo.build_matrix_path)

Expand Down
7 changes: 5 additions & 2 deletions zmk/hardware.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from dataclasses import dataclass, field
from functools import reduce
from pathlib import Path
from typing import Any, Literal, Self, TypeAlias, TypeGuard
from typing import Any, Literal, Type, TypeAlias, TypeGuard, TypeVar

import dacite

Expand All @@ -22,6 +22,9 @@
# TODO: dict should match { id: str, features: list[Feature] }
Variant: TypeAlias = str | dict[str, str]

# TODO: replace with typing.Self once minimum Python version is >= 3.11
_Self = TypeVar("_Self", bound="Hardware")


@dataclass
class Hardware:
Expand All @@ -47,7 +50,7 @@ def __rich__(self) -> Any:
return f"{self.id} [dim]{self.name}"

@classmethod
def from_dict(cls, data) -> Self:
def from_dict(cls: "Type[_Self]", data) -> _Self:
"""Read a hardware description from a dict"""
return dacite.from_dict(cls, data)

Expand Down

0 comments on commit ff153c2

Please sign in to comment.