Skip to content

🔧 Type the BuildEnvironment attributes used by SphinxNeedsData (Tier 2)#1729

Open
chrisjsewell wants to merge 1 commit into
masterfrom
claude/type-data-env-attrs
Open

🔧 Type the BuildEnvironment attributes used by SphinxNeedsData (Tier 2)#1729
chrisjsewell wants to merge 1 commit into
masterfrom
claude/type-data-env-attrs

Conversation

@chrisjsewell

Copy link
Copy Markdown
Member

Tier 2 of #1725 (first item).

Problem

SphinxNeedsData stashes all of its data as private attributes on the Sphinx BuildEnvironment (e.g. env._needs_all_needs, env._needs_view, env._needs_schema, …) and _needs_services on the Sphinx app. mypy can't see these, so data.py carried an override masking the fallout:

[[tool.mypy.overrides]]
module = ["sphinx_needs.data"]
disable_error_code = ["attr-defined", "no-any-return"]

That override hid 45 errors (9 dynamic env attributes + 1 app attribute, each generating an attr-defined and, where returned, a no-any-return).

Fix

Declare the stashed attributes on TYPE_CHECKING-only subclasses — _NeedsEnv(BuildEnvironment) and _NeedsApp(Sphinx) — and cast self.env to _NeedsEnv once in __init__. Every accessor body is left untouched.

  • The two helper classes exist only under TYPE_CHECKING, so there's no runtime footprint.
  • cast("_NeedsEnv", env) is a runtime no-op — it returns env unchanged.
  • _NeedsEnv is-a BuildEnvironment, so nothing that consumes .env is affected.

This removes the last per-module disable_error_code override from the mypy config — data.py is now type-checked under full --strict with no suppressions.

Verification

  • mypy --strictclean (78 files); all 45 previously-suppressed errors resolved with the override deleted
  • ruff check / ruff format — clean
  • 56 tests pass across test_basic_doc, test_api_usage, test_need_item_api, test_needpie_with_zero_needs, test_service_github, test_needs_builder (exercise need creation, views, services, umls, and the github app-attribute path)

No changelog entry: internal type-checking change, no runtime behaviour difference (consistent with the Tier 1 PR #1726).

🤖 Generated with Claude Code

https://claude.ai/code/session_016hgF7RDxmXvBnFhCkP3sNh


Generated by Claude Code

`SphinxNeedsData` stores its data as private attributes on the Sphinx
`BuildEnvironment` (and `_needs_services` on the `Sphinx` app), which mypy
cannot see — so the module carried a `disable_error_code = ["attr-defined",
"no-any-return"]` override masking 45 errors.

Declare those attributes on `TYPE_CHECKING`-only `BuildEnvironment`/`Sphinx`
subclasses and `cast` `self.env` to the typed env in `__init__`. The accessor
bodies are unchanged and the `cast` is a runtime no-op. This removes the last
per-module `disable_error_code` override from the mypy config.

Tier 2 of #1725.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016hgF7RDxmXvBnFhCkP3sNh
@codecov

codecov Bot commented Jun 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.42%. Comparing base (4e10030) to head (424c13c).
⚠️ Report is 295 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1729      +/-   ##
==========================================
+ Coverage   86.87%   89.42%   +2.54%     
==========================================
  Files          56       74      +18     
  Lines        6532    10682    +4150     
==========================================
+ Hits         5675     9552    +3877     
- Misses        857     1130     +273     
Flag Coverage Δ
pytests 89.42% <100.00%> (+2.54%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

2 participants