From cc3e474f0f37368209adc0d5815a225bd6b0fad5 Mon Sep 17 00:00:00 2001 From: Chad Weider Date: Fri, 18 Sep 2026 11:14:23 -0700 Subject: [PATCH] Annotate `get_us_counties()` 8bd6642 (#42 - "add type hints") added type hints all of the methods of `GeonamesCache` except for `get_us_counties()`. There's no discussion of the method in the pull request and no indication in the code that the absense of type annotations is important. It is likely that it was omitted, because it was the one usage of `_load_data()` that was incompatible with the type that _it_ specified (`list` vs. `dict`). The specification of `dict` by that method is not necessary. Specifying an unbound `TypeVar` will accomodate the usage by `get_us_counties()`. Being an annotation, the change will have no runtime affect and only a minimal effect for those consumers using a static type checker: the unusual consumer that attempts to `del` a key or assigns a value with an incompatible type will receive errors from their type checker. Additions to the dictionary will be permitted, since `USCountry` is not `closed`. As adding the annotation will remove the need for a `cast` by consumers, it's a worthwhile amendment to the interface. --- geonamescache/__init__.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/geonamescache/__init__.py b/geonamescache/__init__.py index 20f2a43..6c8b32b 100644 --- a/geonamescache/__init__.py +++ b/geonamescache/__init__.py @@ -24,6 +24,7 @@ ) TDict = TypeVar('TDict', bound=Mapping[str, Any]) +TData = TypeVar('TData') class GeonamesCache: @@ -72,7 +73,7 @@ def get_cities_by_name(self, name: str) -> list[dict[GeoNameIdStr, City]]: self.cities_by_names[name] = [{gid: city} for gid, city in self.cities_items if city['name'] == name] return self.cities_by_names[name] - def get_us_counties(self): + def get_us_counties(self) -> list[USCounty]: return self._load_data(self.us_counties, 'us_counties.json') def search_cities( @@ -105,7 +106,7 @@ def search_cities( return results @staticmethod - def _load_data(datadict: dict[Any, Any] | None, datafile: str) -> dict[Any, Any]: + def _load_data(datadict: TData | None, datafile: str) -> TData: if datadict is None: with open(os.path.join(os.path.dirname(__file__), 'data', datafile)) as f: datadict = json.load(f)