generated from bachya/pypi-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add model for GET /v1/sensors responses
- Loading branch information
Showing
8 changed files
with
355 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Define backports.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
"""Define enum backports from the standard library.""" | ||
from __future__ import annotations | ||
|
||
from enum import Enum | ||
from typing import Any, TypeVar | ||
|
||
_StrEnumSelfT = TypeVar("_StrEnumSelfT", bound="StrEnum") | ||
|
||
|
||
class StrEnum(str, Enum): | ||
"""Define a partial backport of Python 3.11's StrEnum.""" | ||
|
||
def __new__( | ||
cls: type[_StrEnumSelfT], value: str, *args: Any, **kwargs: Any | ||
) -> _StrEnumSelfT: | ||
"""Create a new StrEnum instance. | ||
Args: | ||
value: The enum value. | ||
args: Additional args. | ||
kwargs: Additional kwargs. | ||
Returns: | ||
The enum. | ||
Raises: | ||
TypeError: Raised when an enumerated value isn't a string. | ||
""" | ||
if not isinstance(value, str): | ||
raise TypeError(f"{value!r} is not a string") | ||
return super().__new__(cls, value, *args, **kwargs) | ||
|
||
def __str__(self) -> str: | ||
"""Return self.value. | ||
Returns: | ||
The string value. | ||
""" | ||
return str(self.value) | ||
|
||
@staticmethod | ||
def _generate_next_value_( | ||
name: str, | ||
start: int, | ||
count: int, | ||
last_values: list[Any], | ||
) -> Any: | ||
"""Make `auto()` explicitly unsupported. | ||
We may revisit this when it's very clear that Python 3.11's `StrEnum.auto()` | ||
behavior will no longer change. | ||
Args: | ||
name: The name of the enum. | ||
start: The starting index. | ||
count: The total number of enumerated values. | ||
last_values: Previously enumerated values. | ||
Raises: | ||
TypeError: Always raised. | ||
""" | ||
raise TypeError("auto() is not supported by this implementation") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Define tests for backports.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
"""Define tests for enum backports.""" | ||
# pylint: disable=too-few-public-methods,unused-variable | ||
from enum import auto | ||
|
||
import pytest | ||
|
||
from aiopurpleair.backports.enum import StrEnum | ||
|
||
|
||
def test_strenum() -> None: | ||
"""Test StrEnum.""" | ||
|
||
class TestEnum(StrEnum): | ||
"""Define a test StrEnum.""" | ||
|
||
TEST = "test" | ||
|
||
assert str(TestEnum.TEST) == "test" | ||
assert TestEnum.TEST == "test" # type: ignore[comparison-overlap] | ||
assert TestEnum("test") is TestEnum.TEST | ||
assert TestEnum(TestEnum.TEST) is TestEnum.TEST | ||
|
||
with pytest.raises(ValueError): | ||
TestEnum(42) # type: ignore[arg-type] | ||
|
||
with pytest.raises(ValueError): | ||
TestEnum("str but unknown") | ||
|
||
with pytest.raises(TypeError): | ||
|
||
class FailEnum(StrEnum): | ||
"""Define an incorrect StrEnum.""" | ||
|
||
TEST = 42 | ||
|
||
with pytest.raises(TypeError): | ||
|
||
class FailEnum2(StrEnum): | ||
"""Define an StrEnum that implements auto().""" | ||
|
||
TEST = auto() |
Oops, something went wrong.