Skip to content

Forcing.from_csv: import importlib.resources explicitly - #61

Merged
CommonClimate merged 1 commit into
mainfrom
fix/forcing-importlib-resources
Nov 19, 2026
Merged

CommonClimate merged 1 commit into
mainfrom
fix/forcing-importlib-resources

Conversation

@CommonClimate

Copy link
Copy Markdown
Contributor

Problem

forcing.py does import importlib but calls importlib.resources.files(...). The resources submodule is only an attribute of importlib once something else has imported it, so in a fresh interpreter:

import climatecritters as cc
cc.Forcing.from_csv(dataset='insolation')
# AttributeError: module 'importlib' has no attribute 'resources'

Found while scripting Fig. 1 of the GMD paper (which currently works around it).

Fix

  • import importlib → import importlib.resources (one line).
  • New tests/test_core_forcing_datasets.py: loads both bundled datasets (insolation, vieira_tsi) in a subprocess. An in-process test can pass by accident when pytest or another package has already loaded importlib.resources. The test fails without the fix and passes with it.

Full suite: 457 passed.

🤖 Generated with Claude Code

forcing.py imported only the top-level `importlib` package but called
`importlib.resources.files(...)`. That attribute exists only once some other
module has imported `importlib.resources`, so `Forcing.from_csv(dataset=...)`
raised AttributeError in a fresh interpreter (e.g. a plain script).

Adds a regression test that loads both bundled datasets in a subprocess,
since an in-process test can pass by accident when the submodule is already
loaded.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@CommonClimate
CommonClimate merged commit 8ae1f07 into main Nov 19, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant