From 9e984178fe97b8d748e91e718611930880dcc32c Mon Sep 17 00:00:00 2001 From: Jan-Eric Nitschke <47750513+JanEricNitschke@users.noreply.github.com> Date: Tue, 31 Oct 2023 11:45:07 +0100 Subject: [PATCH] Tried to enable strict mode for pyright. Too many missing stubs for that. --- awpy/analytics/nav.py | 4 ++-- awpy/analytics/states.py | 16 +++++++++------- awpy/utils.py | 17 +++++++++++------ pyproject.toml | 11 +++++++---- 4 files changed, 29 insertions(+), 19 deletions(-) diff --git a/awpy/analytics/nav.py b/awpy/analytics/nav.py index 788da6caa..9ad26d252 100644 --- a/awpy/analytics/nav.py +++ b/awpy/analytics/nav.py @@ -299,7 +299,7 @@ def dist_heuristic(node_a: TileId, node_b: TileId) -> float: ) try: - geodesic_path = nx.astar_path( + geodesic_path: list[TileId] = nx.astar_path( map_graph, area_a, area_b, heuristic=dist_heuristic, weight="weight" ) geodesic_cost = sum( @@ -629,7 +629,7 @@ def _get_area_place_mapping(map_name: str) -> dict[str, list[TileId]]: dict[str, list[TileId]]: The mapping "areaName": [areas that have this area name] for each "areaName" """ - area_mapping = defaultdict(list) + area_mapping: dict[str, list[TileId]] = defaultdict(list) # Get the mapping "areaName": [areas that have this area name] for area in NAV[map_name]: area_mapping[NAV[map_name][area]["areaName"]].append(area) diff --git a/awpy/analytics/states.py b/awpy/analytics/states.py index b1e5a09e1..df4e0404e 100644 --- a/awpy/analytics/states.py +++ b/awpy/analytics/states.py @@ -1,9 +1,11 @@ """Functions to generate game stats based on snapshots from a demofile.""" # pylint: disable=unused-argument +from typing import Any + from awpy.types import GameFrame -def generate_vector_state(frame: GameFrame, map_name: str) -> dict: +def generate_vector_state(frame: GameFrame, map_name: str) -> dict[str, Any]: """Returns a game state in a dictionary format. Args: @@ -11,9 +13,9 @@ def generate_vector_state(frame: GameFrame, map_name: str) -> dict: map_name (str): String indicating the map name Returns: - dict: With keys for each feature. + dict[str, Any]: With keys for each feature. """ - game_state: dict = { + game_state: dict[str, Any] = { "mapName": map_name, "secondsSincePhaseStart": frame["seconds"], "bombPlanted": frame["bombPlanted"], @@ -72,27 +74,27 @@ def generate_vector_state(frame: GameFrame, map_name: str) -> dict: return game_state -def generate_graph_state(frame: GameFrame) -> dict: +def generate_graph_state(frame: GameFrame) -> dict[str, Any]: """Returns a game state as a graph. Args: frame (GameFrame): Dict output of a frame generated from the DemoParser class Returns: - dict: With keys "T", "CT" and "Global", + dict[str, Any]: With keys "T", "CT" and "Global", where each entry is a vector. Global vector is CT + T concatenated """ return {"ct": [], "t": [], "global": []} -def generate_set_state(frame: GameFrame) -> dict: +def generate_set_state(frame: GameFrame) -> dict[str, Any]: """Returns a game state as a set. Args: frame (GameFrame): Dict output of a frame generated from the DemoParser class Returns: - dict: With keys "T", "CT" and "Global", + dict[str, Any]: With keys "T", "CT" and "Global", where each entry is a vector. Global vector is CT + T concatenated """ return {"ct": [], "t": [], "global": []} diff --git a/awpy/utils.py b/awpy/utils.py index f6034226b..9922390e5 100644 --- a/awpy/utils.py +++ b/awpy/utils.py @@ -2,21 +2,26 @@ import re import subprocess -from typing import Any +from typing import Self, TypeVar import pandas as pd +from typing_extensions import override from awpy.types import Area, TileId +_KT = TypeVar("_KT") +_VT = TypeVar("_VT") -class AutoVivification(dict): + +class AutoVivification(dict[_KT, _VT | Self]): """Implementation of perl's autovivification feature. Stolen from: https://stackoverflow.com/questions/651794/whats-the-best-way-to-initialize-a-dict-of-dicts-in-python """ - def __getitem__(self, item: Any) -> Any: # noqa: ANN401 + @override + def __getitem__(self, item: _KT) -> _VT | Self: """Autovivified get item from dict. Tries to get the item as normal. @@ -24,13 +29,13 @@ def __getitem__(self, item: Any) -> Any: # noqa: ANN401 AutoVivification dict is added instead. Args: - item (Any): Item to retrieve the value for. + item (_KT): Item to retrieve the value for. Returns: - Any: Retrieved value. + _VT | Self: Retrieved value. """ try: - return dict.__getitem__(self, item) + return dict[_KT, _VT | Self].__getitem__(self, item) except KeyError: value = self[item] = type(self)() return value diff --git a/pyproject.toml b/pyproject.toml index a03d4d002..6e4b13bc4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -129,7 +129,7 @@ strictDictionaryInference = true strictSetInference = true useLibraryCodeForTypes = false reportPropertyTypeMismatch = "error" -reportFunctionMemberAccess = "warning" +reportFunctionMemberAccess = "error" reportMissingTypeStubs = "none" reportUntypedFunctionDecorator = "error" reportUntypedClassDecorator = "error" @@ -138,14 +138,17 @@ reportUntypedNamedTuple = "error" reportPrivateUsage = "error" reportConstantRedefinition = "error" reportOverlappingOverload = "error" -reportMissingParameterType = "warning" -reportUnnecessaryIsInstance = "none" +reportMissingParameterType = "error" +# Because this is externally visible +# and we want to have the check for users +# that do not use type checkers. +reportUnnecessaryIsInstance = "warning" reportUnnecessaryCast = "error" reportUnnecessaryComparison = "error" reportUnnecessaryContains = "error" reportAssertAlwaysTrue = "error" reportUnnecessaryTypeIgnoreComment = "error" -reportImplicitOverride = "none" +reportImplicitOverride = "error" reportShadowedImports = "error"