Disallow partially specified generics.

This commit is contained in:
Dan Albert 2021-07-07 16:01:20 -07:00
parent fc32b98341
commit 69c3d41a8a
5 changed files with 23 additions and 16 deletions

View File

@ -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

View File

@ -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:

View File

@ -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]

View File

@ -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,

View File

@ -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