Skip to content

Commit

Permalink
Add strict mypy typing (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
bachya committed Oct 26, 2022
1 parent 2e76455 commit 5afb7d2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 19 deletions.
20 changes: 20 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,26 @@ not_skip = "__init__.py"
sections = "FUTURE,STDLIB,INBETWEENS,THIRDPARTY,FIRSTPARTY,LOCALFOLDER"
use_parentheses = true

[tool.mypy]
check_untyped_defs = true
disallow_incomplete_defs = true
disallow_subclassing_any = true
disallow_untyped_calls = true
disallow_untyped_decorators = true
disallow_untyped_defs = true
follow_imports = "silent"
ignore_missing_imports = true
no_implicit_optional = true
python_version = "3.10"
show_error_codes = true
strict_equality = true
warn_incomplete_stub = true
warn_redundant_casts = true
warn_return_any = true
warn_unreachable = true
warn_unused_configs = true
warn_unused_ignores = true

[tool.poetry]
name = "pytile"
version = "2022.02.0"
Expand Down
6 changes: 3 additions & 3 deletions pytile/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import asyncio
from time import time
from typing import Any
from typing import Any, cast
from uuid import uuid4

from aiohttp import ClientSession
Expand Down Expand Up @@ -46,7 +46,7 @@ def __init__(
self.user_uuid: str | None = None

async def _async_request(
self, method: str, endpoint: str, **kwargs
self, method: str, endpoint: str, **kwargs: dict[str, Any]
) -> dict[str, Any]:
"""Make a request against Tile."""
if self._session_expiry and self._session_expiry <= int(time() * 1000):
Expand Down Expand Up @@ -74,7 +74,7 @@ async def _async_request(

LOGGER.debug("Data received from /%s: %s", endpoint, data)

return data
return cast(dict[str, Any], data)

async def async_get_tiles(self) -> dict[str, Tile]:
"""Get all active Tiles from the user's account."""
Expand Down
32 changes: 16 additions & 16 deletions pytile/tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from collections.abc import Awaitable, Callable
from datetime import datetime
from typing import Any
from typing import Any, cast

from .const import LOGGER

Expand Down Expand Up @@ -33,39 +33,39 @@ def accuracy(self) -> float | None:
"""Return the accuracy of the last measurement."""
if (last_state := self._tile_data["result"].get("last_tile_state")) is None:
return None
return last_state["h_accuracy"]
return cast(float, last_state["h_accuracy"])

@property
def altitude(self) -> float | None:
"""Return the last detected altitude."""
if (last_state := self._tile_data["result"].get("last_tile_state")) is None:
return None
return last_state["altitude"]
return cast(float, last_state["altitude"])

@property
def archetype(self) -> str:
"""Return the archetype."""
return self._tile_data["result"]["archetype"]
return cast(str, self._tile_data["result"]["archetype"])

@property
def dead(self) -> bool:
"""Return whether the Tile is dead."""
return self._tile_data["result"]["is_dead"]
return cast(bool, self._tile_data["result"]["is_dead"])

@property
def firmware_version(self) -> str:
"""Return the firmware version."""
return self._tile_data["result"]["firmware_version"]
return cast(str, self._tile_data["result"]["firmware_version"])

@property
def hardware_version(self) -> str:
"""Return the hardware version."""
return self._tile_data["result"]["hw_version"]
return cast(str, self._tile_data["result"]["hw_version"])

@property
def kind(self) -> str:
"""Return the type of Tile."""
return self._tile_data["result"]["tile_type"]
return cast(str, self._tile_data["result"]["tile_type"])

@property
def last_timestamp(self) -> datetime | None:
Expand All @@ -77,14 +77,14 @@ def latitude(self) -> float | None:
"""Return the last detected latitude."""
if (last_state := self._tile_data["result"].get("last_tile_state")) is None:
return None
return last_state["latitude"]
return cast(float, last_state["latitude"])

@property
def longitude(self) -> float | None:
"""Return the last detected longitude."""
if (last_state := self._tile_data["result"].get("last_tile_state")) is None:
return None
return last_state["longitude"]
return cast(float, last_state["longitude"])

@property
def lost(self) -> bool:
Expand All @@ -96,7 +96,7 @@ def lost(self) -> bool:
"""
if (last_state := self._tile_data["result"].get("last_tile_state")) is None:
return True
return last_state["is_lost"]
return cast(bool, last_state["is_lost"])

@property
def lost_timestamp(self) -> datetime | None:
Expand All @@ -106,31 +106,31 @@ def lost_timestamp(self) -> datetime | None:
@property
def name(self) -> str:
"""Return the name."""
return self._tile_data["result"]["name"]
return cast(str, self._tile_data["result"]["name"])

@property
def ring_state(self) -> str | None:
"""Return the ring state."""
if (last_state := self._tile_data["result"].get("last_tile_state")) is None:
return None
return last_state["ring_state"]
return cast(str, last_state["ring_state"])

@property
def uuid(self) -> str:
"""Return the UUID."""
return self._tile_data["result"]["tile_uuid"]
return cast(str, self._tile_data["result"]["tile_uuid"])

@property
def visible(self) -> bool:
"""Return whether the Tile is visible."""
return self._tile_data["result"]["visible"]
return cast(bool, self._tile_data["result"]["visible"])

@property
def voip_state(self) -> str | None:
"""Return the VoIP state."""
if (last_state := self._tile_data["result"].get("last_tile_state")) is None:
return None
return last_state["voip_state"]
return cast(str, last_state["voip_state"])

def _save_timestamps(self, tile_data: dict[str, Any]) -> None:
"""Save UTC timestamps from a Tile data set."""
Expand Down

0 comments on commit 5afb7d2

Please sign in to comment.