Skip to content

More robust optional import linting for scoped imports - #1765

Merged
cmdupuis3 merged 3 commits into
sevans/_raise_hint_if_optional_deps_missingfrom
cmd/linting_robustness
Sep 16, 2026
Merged

cmdupuis3 merged 3 commits into
sevans/_raise_hint_if_optional_deps_missingfrom
cmd/linting_robustness

Conversation

@cmdupuis3

Copy link
Copy Markdown
Collaborator

A lot of possible cases where optional import linting is needed are missed because it isn't fully walking the block scopes in the AST. These changes should help with that.

@cmdupuis3
cmdupuis3 requested a review from Sevans711 September 15, 2026 20:05
make module-level optional deps checking get its own test with clear instructions about how to proceed and clear pointers to relevant line numbers, instead of bundling it with the function-level OptionalImportCheckResult, because module-level should never import optional deps (outside of "if TYPE_CHECKING" of course)
@Sevans711

Copy link
Copy Markdown
Collaborator

Thank you for these improvements! They look good, I did a few different sanity checks that things work well. While doing the checks I noticed that the error message from pytest was somewhat confusing when detecting module-level imports of optional dependencies. I added a commit to split the module-level import detection into its own functions and test, with a clearer message that module names and line numbers. I'm happy for this to merge into #1656 if it looks good to you!

@cmdupuis3
cmdupuis3 merged commit ba29789 into sevans/_raise_hint_if_optional_deps_missing Sep 16, 2026
17 checks passed
Sevans711 added a commit that referenced this pull request Sep 17, 2026
…lled. (#1656)

* add _raise_hint_if_optional_deps_missing

* optional deps test ensure helpful hint gets raised

* forgot pre-commit ruff formatting

* fix opt deps error hint tests typos

also add string matching to test_check_requires_viz_and_geo in both the installed_with_geo and the installed_with_viz cases, and left a comment about why it wasn't added in the installed_with_no_opts case.

* add test_optional_deps files & ci commands

* forgot pre-commit ruff formatting

* fix ruff complaint about unused import

* add healpix-sensitive optional deps test

(maybe not necessary… but also trying to re-trigger CI jobs here, due to github actions downtime yesterday causing stalled jobs with no "rerun jobs" button available.)

* fix optional deps test: cannot plot UxDataset

* add _raise_hint_if_optional_deps_missing

* optional deps test ensure helpful hint gets raised

* forgot pre-commit ruff formatting

* fix opt deps error hint tests typos

also add string matching to test_check_requires_viz_and_geo in both the installed_with_geo and the installed_with_viz cases, and left a comment about why it wasn't added in the installed_with_no_opts case.

* fix and test messages of missing opt deps hints

* forgot pre-commit ruff formatting

* test usage of _raise_hint_if_optional_deps_missing

Ensures it is being used properly throughout the codebase. (Claude helped me write this but I read through it all and poked around slightly to improve function names and add more documentation.)

* forgot pre-commit ruff formatting

* fix: specify utf-8 encoding to avoid windows crash

* improve installation docs page

* Revert "improve installation docs page"

This reverts commit 686d1f8.

* More robust optional import linting for scoped imports (#1765)

* More robust optional import linting for scoped imports

* module-level optional deps gets its own test

make module-level optional deps checking get its own test with clear instructions about how to proceed and clear pointers to relevant line numbers, instead of bundling it with the function-level OptionalImportCheckResult, because module-level should never import optional deps (outside of "if TYPE_CHECKING" of course)

* (forgot pre-commit ruff formatting)

---------

Co-authored-by: Sam Evans <47793072+Sevans711@users.noreply.github.com>

* pytest monkeypatch when editing global dict

---------

Co-authored-by: Christopher Dupuis <45972964+cmdupuis3@users.noreply.github.com>
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