From 2c330fc00496a2c94e27861b18fc8f672e67fd08 Mon Sep 17 00:00:00 2001 From: Arpit Jain Date: Wed, 8 Jul 2026 22:14:22 +0900 Subject: [PATCH] Fix crash in states CLI when printing counties The counties attribute added in 4.0.0 is a list of County objects, but the CLI's attribute loop tries to ", ".join() every list value, which raises a TypeError on the County objects. This crashed `states ` for every state. Show the county count instead and add a regression test. Signed-off-by: Arpit Jain --- us/cli/states.py | 6 +++++- us/tests/test_us.py | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/us/cli/states.py b/us/cli/states.py index ae79819..c075ad4 100644 --- a/us/cli/states.py +++ b/us/cli/states.py @@ -31,7 +31,11 @@ def main(): for key in sorted(data.keys()): val = data[key] - if isinstance(val, (list, tuple)): + if key == "counties": + # counties is a list of County objects, not strings, so it + # can't be joined like the other list attributes + val = "%d counties" % len(val) + elif isinstance(val, (list, tuple)): val = ", ".join(val) sys.stdout.write(" %s: %s\n" % (key, val)) diff --git a/us/tests/test_us.py b/us/tests/test_us.py index b98482f..4c1d4d3 100644 --- a/us/tests/test_us.py +++ b/us/tests/test_us.py @@ -6,6 +6,7 @@ import pytz import us +from us.cli import states as states_cli from us.states import County # attribute @@ -246,6 +247,19 @@ def test_dc(): assert us.states.DC not in us.STATES +# cli + + +def test_cli_lookup(capsys, monkeypatch): + # the counties attribute is a list of County objects, which used to crash + # the "other attributes" loop when it tried to join them as strings + monkeypatch.setattr("sys.argv", ["states", "MD"]) + states_cli.main() + out = capsys.readouterr().out + assert "Maryland" in out + assert "counties" in out + + # shapefiles