Skip to content

Commit cf2f200

Browse files
authored
Check type issues with ty and fix problems it finds (#100)
* Check type issues with ty and fix problems it finds Adds ty to the dev dependency group and clears every diagnostic it surfaces against the `us` package: - Drop unused `# type: ignore` directives on jellyfish and pytest. - Restructure `lookup()` so `fallback_key` is only ever bound to a `str`, removing the false `None | str` dict-key write. - Guard `state.shapefile_urls()` against `None` in the CLI and in the skipped network test before iterating. - Narrow `state.fips` with an assert in `test_county_fips_prefixed_by_state`. - Suppress the `requests` import inside the permanently-skipped `test_head` with `# ty: ignore[unresolved-import]`.
1 parent 43aed4f commit cf2f200

6 files changed

Lines changed: 51 additions & 9 deletions

File tree

.github/workflows/pythonpackage.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ jobs:
2525
- name: Check code format with Ruff
2626
run: uv run ruff format --check us
2727

28+
- name: Check types with ty
29+
run: uv run ty check us
30+
2831
testing:
2932
needs: linting
3033
runs-on: ubuntu-latest

pyproject.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ dependencies = ['jellyfish']
2929
states = "us.cli.states:main"
3030

3131
[dependency-groups]
32-
dev = ['ruff', 'pytest', 'pytz']
32+
dev = [
33+
'ruff',
34+
'pytest',
35+
'pytz',
36+
"ty>=0.0.38",
37+
]
3338

3439
[tool.ruff]
3540
line-length = 120

us/cli/states.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ def main():
3838

3939
sys.stdout.write("\n")
4040
sys.stdout.write(" shapefiles:\n")
41-
for region, url in state.shapefile_urls().items():
42-
sys.stdout.write(" %s: %s\n" % (region, url))
41+
urls = state.shapefile_urls()
42+
if urls is not None:
43+
for region, url in urls.items():
44+
sys.stdout.write(" %s: %s\n" % (region, url))
4345

4446
sys.stdout.write("\n")
4547

us/states.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from typing import Any, Callable, Dict, Iterable, List, Optional, Type
55
from urllib.parse import urljoin
66

7-
import jellyfish # type: ignore
7+
import jellyfish
88

99
FIPS_RE = re.compile(r"^\d{2}$")
1010
ABBR_RE = re.compile(r"^[a-zA-Z]{2}$")
@@ -137,7 +137,6 @@ def lookup(
137137
# fallback results are cached under a separate, fallback-specific key so
138138
# they never leak into non-fallback lookups -- or lookups using a
139139
# different fallback -- of the same value
140-
fallback_key = None
141140
if fallback_func is not None:
142141
fallback_key = f"{cache_key}:fallback:{fallback_func!r}"
143142
if use_cache and fallback_key in _lookup_cache:

us/tests/test_us.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import re
22
from itertools import chain
33

4-
import jellyfish # type: ignore
5-
import pytest # type: ignore
4+
import jellyfish
5+
import pytest
66
import pytz
77

88
import us
@@ -251,10 +251,15 @@ def test_dc():
251251

252252
@pytest.mark.skip
253253
def test_head():
254-
import requests
254+
# `requests` is intentionally not declared as a dependency; this test
255+
# is permanently skipped and the import is dead code for ty.
256+
import requests # ty: ignore[unresolved-import]
255257

256258
for state in us.STATES_AND_TERRITORIES:
257-
for url in state.shapefile_urls().values():
259+
urls = state.shapefile_urls()
260+
if urls is None:
261+
continue
262+
for url in urls.values():
258263
resp = requests.head(url)
259264
assert resp.status_code == 200
260265

@@ -312,6 +317,7 @@ def test_county_fips_format():
312317

313318
def test_county_fips_prefixed_by_state():
314319
for state in us.STATES_AND_TERRITORIES:
320+
assert state.fips is not None, f"{state.abbr}: missing state fips"
315321
for county in state.counties:
316322
assert county.fips.startswith(state.fips), (
317323
f"{state.abbr}: county {county.name} fips {county.fips} not prefixed by state fips {state.fips}"

uv.lock

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)