From 0b5d7841c43d452d682b303004725293e26ad1b8 Mon Sep 17 00:00:00 2001 From: Charlie Tonneslan Date: Sun, 17 May 2026 13:22:28 -0400 Subject: [PATCH] Switch ACS/SF1/PL endpoints on fields() too tables() and get() on the ACS, SF1, and PL clients call _switch_endpoints first to point at the correct dataset URL. fields() didn't, so it inherits Client.fields() unchanged and hits whatever endpoint was set last (or the generic Census one), which 404s and produces a JSON decode error. Calling tables() first happens to mask this because tables flips the endpoint as a side effect. Hitting fields() before anything else is the broken path. Closes #138. Signed-off-by: Charlie Tonneslan --- census/core.py | 12 ++++++++++++ census/tests/test_census.py | 10 ++++++++++ 2 files changed, 22 insertions(+) diff --git a/census/core.py b/census/core.py index 2101e17..a938075 100644 --- a/census/core.py +++ b/census/core.py @@ -324,6 +324,10 @@ def tables(self, *args, **kwargs): self._switch_endpoints(kwargs.get('year', self.default_year)) return super(ACSClient, self).tables(*args, **kwargs) + def fields(self, *args, **kwargs): + self._switch_endpoints(kwargs.get('year', self.default_year)) + return super(ACSClient, self).fields(*args, **kwargs) + def get(self, *args, **kwargs): self._switch_endpoints(kwargs.get('year', self.default_year)) @@ -466,6 +470,10 @@ def tables(self, *args, **kwargs): self._switch_endpoints(kwargs.get('year', self.default_year)) return super(SF1Client, self).tables(*args, **kwargs) + def fields(self, *args, **kwargs): + self._switch_endpoints(kwargs.get('year', self.default_year)) + return super(SF1Client, self).fields(*args, **kwargs) + def get(self, *args, **kwargs): self._switch_endpoints(kwargs.get('year', self.default_year)) @@ -548,6 +556,10 @@ def tables(self, *args, **kwargs): self._switch_endpoints(kwargs.get('year', self.default_year)) return super(PLClient, self).tables(*args, **kwargs) + def fields(self, *args, **kwargs): + self._switch_endpoints(kwargs.get('year', self.default_year)) + return super(PLClient, self).fields(*args, **kwargs) + def get(self, *args, **kwargs): self._switch_endpoints(kwargs.get('year', self.default_year)) diff --git a/census/tests/test_census.py b/census/tests/test_census.py index 2286a28..58c027f 100644 --- a/census/tests/test_census.py +++ b/census/tests/test_census.py @@ -152,6 +152,16 @@ def test_tables(self): self.client('sf1').tables() self.client('pl').tables() + def test_fields(self): + # Regression for #138: fields() needs the same endpoint switch + # that tables() and get() do. Otherwise calling fields() before + # anything else on an ACS/SF1/PL client hits the wrong URL and + # 404s. + self.client('acs5').fields() + self.client('acs5').fields(2010) + self.client('sf1').fields() + self.client('pl').fields() + def test_acs5(self): tests = (