From 69c3d41a8ad967db27e99235c2f7b9196d29e6b9 Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Wed, 7 Jul 2021 16:01:20 -0700 Subject: [PATCH] Disallow partially specified generics. --- game/theater/base.py | 4 +++- game/transfers.py | 4 ++-- game/unitdelivery.py | 20 +++++++++++--------- gen/naming.py | 9 ++++++--- mypy.ini | 2 +- 5 files changed, 23 insertions(+), 16 deletions(-) diff --git a/game/theater/base.py b/game/theater/base.py index 4547e3d3..7d1bff11 100644 --- a/game/theater/base.py +++ b/game/theater/base.py @@ -2,6 +2,8 @@ import itertools import logging from typing import Any +from dcs.unit import UnitType as DcsUnitType + from game.dcs.aircrafttype import AircraftType from game.dcs.groundunittype import GroundUnitType from game.dcs.unittype import UnitType @@ -31,7 +33,7 @@ class Base: total += unit_type.price * count return total - def total_units_of_type(self, unit_type: UnitType) -> int: + def total_units_of_type(self, unit_type: UnitType[DcsUnitType]) -> int: return sum( [ c diff --git a/game/transfers.py b/game/transfers.py index 64870b50..6774964c 100644 --- a/game/transfers.py +++ b/game/transfers.py @@ -519,14 +519,14 @@ class TransportMap(Generic[TransportType]): yield from destination_dict.values() -class ConvoyMap(TransportMap): +class ConvoyMap(TransportMap[Convoy]): def create_transport( self, origin: ControlPoint, destination: ControlPoint ) -> Convoy: return Convoy(origin, destination) -class CargoShipMap(TransportMap): +class CargoShipMap(TransportMap[CargoShip]): def create_transport( self, origin: ControlPoint, destination: ControlPoint ) -> CargoShip: diff --git a/game/unitdelivery.py b/game/unitdelivery.py index a6de6a71..0a2a3db3 100644 --- a/game/unitdelivery.py +++ b/game/unitdelivery.py @@ -5,6 +5,8 @@ from collections import defaultdict from dataclasses import dataclass from typing import Optional, TYPE_CHECKING, Any +from dcs.unittype import UnitType as DcsUnitType + from game.theater import ControlPoint from .dcs.groundunittype import GroundUnitType from .dcs.unittype import UnitType @@ -28,16 +30,16 @@ class PendingUnitDeliveries: self.destination = destination # Maps unit type to order quantity. - self.units: dict[UnitType, int] = defaultdict(int) + self.units: dict[UnitType[DcsUnitType], int] = defaultdict(int) def __str__(self) -> str: return f"Pending delivery to {self.destination}" - def order(self, units: dict[UnitType, int]) -> None: + def order(self, units: dict[UnitType[DcsUnitType], int]) -> None: for k, v in units.items(): self.units[k] += v - def sell(self, units: dict[UnitType, int]) -> None: + def sell(self, units: dict[UnitType[DcsUnitType], int]) -> None: for k, v in units.items(): self.units[k] -= v @@ -46,27 +48,27 @@ class PendingUnitDeliveries: self.units = defaultdict(int) def refund_ground_units(self, game: Game) -> None: - ground_units: dict[UnitType[Any], int] = { + ground_units: dict[UnitType[DcsUnitType], int] = { u: self.units[u] for u in self.units.keys() if isinstance(u, GroundUnitType) } self.refund(game, ground_units) for gu in ground_units.keys(): del self.units[gu] - def refund(self, game: Game, units: dict[UnitType, int]) -> None: + def refund(self, game: Game, units: dict[UnitType[DcsUnitType], int]) -> None: for unit_type, count in units.items(): logging.info(f"Refunding {count} {unit_type} at {self.destination.name}") game.adjust_budget( unit_type.price * count, player=self.destination.captured ) - def pending_orders(self, unit_type: UnitType) -> int: + def pending_orders(self, unit_type: UnitType[DcsUnitType]) -> int: pending_units = self.units.get(unit_type) if pending_units is None: pending_units = 0 return pending_units - def available_next_turn(self, unit_type: UnitType) -> int: + def available_next_turn(self, unit_type: UnitType[DcsUnitType]) -> int: current_units = self.destination.base.total_units_of_type(unit_type) return self.pending_orders(unit_type) + current_units @@ -79,9 +81,9 @@ class PendingUnitDeliveries: ) self.refund_ground_units(game) - bought_units: dict[UnitType, int] = {} + bought_units: dict[UnitType[DcsUnitType], int] = {} units_needing_transfer: dict[GroundUnitType, int] = {} - sold_units: dict[UnitType, int] = {} + sold_units: dict[UnitType[DcsUnitType], int] = {} for unit_type, count in self.units.items(): coalition = "Ally" if self.destination.captured else "Enemy" d: dict[Any, int] diff --git a/gen/naming.py b/gen/naming.py index df56ab64..f43c0a2e 100644 --- a/gen/naming.py +++ b/gen/naming.py @@ -3,6 +3,7 @@ import time from typing import List from dcs.country import Country +from dcs.unittype import UnitType as DcsUnitType from game.dcs.aircrafttype import AircraftType from game.dcs.unittype import UnitType @@ -293,7 +294,9 @@ class NameGenerator: ) @classmethod - def next_unit_name(cls, country: Country, parent_base_id: int, unit_type: UnitType): + def next_unit_name( + cls, country: Country, parent_base_id: int, unit_type: UnitType[DcsUnitType] + ) -> str: cls.number += 1 return "unit|{}|{}|{}|{}|".format( country.id, cls.number, parent_base_id, unit_type.name @@ -301,8 +304,8 @@ class NameGenerator: @classmethod def next_infantry_name( - cls, country: Country, parent_base_id: int, unit_type: UnitType - ): + cls, country: Country, parent_base_id: int, unit_type: UnitType[DcsUnitType] + ) -> str: cls.infantry_number += 1 return "infantry|{}|{}|{}|{}|".format( country.id, diff --git a/mypy.ini b/mypy.ini index 3549d3b7..480b478b 100644 --- a/mypy.ini +++ b/mypy.ini @@ -3,7 +3,7 @@ check_untyped_defs = True # disallow_any_decorated = True # disallow_any_expr = True -# disallow_any_generics = True +disallow_any_generics = True # disallow_any_unimported = True disallow_untyped_decorators = True # disallow_untyped_defs = True