Track imports so we can disable ASYNC106#450
Merged
Zac-HD merged 6 commits intopython-trio:mainfrom Apr 25, 2026
Merged
Conversation
Previously the linter only recognised trio/anyio/asyncio-related calls when they appeared exactly as `trio.open_nursery`, `anyio.create_task_group`, etc. Aliased imports (`import trio as t`), `from` imports (`from trio import open_nursery`), and aliased-from imports (`from trio import open_nursery as on`) silently escaped detection. This adds a pair of utility visitors (VisitorImportTracker / _cst) that build a local-name -> canonical-dotted-qualname map, a pair of helpers (resolve_canonical_ast / _cst) and base-class shortcut `canonical_name()`, and threads an `imports=` keyword through the existing matcher helpers (get_matching_call[_cst], fnmatch_qualified_name[_cst], with_has_call, calls_any_of, critical_except). The tracker only records module-level imports, so function-local imports don't leak into sibling scopes. Existing visitors are updated to pass `self.imports` to those helpers, and ASYNC105/ASYNC115/ASYNC118/ASYNC2xx/ASYNC300 etc. now match via canonical qualname instead of the literal spelling. ASYNC106 was a workaround for the old limitation; it's now disabled by default but left in place for projects that still want to enforce the `import trio` style. Closes python-trio#132. https://claude.ai/code/session_018Hc9rcA31SnXcN8Ee5vVwH
- Drop redundant canonical_name() docstrings. - Simplify fnmatch_qualified_name[_cst] to build a candidate set inline. - Fold the resolve_canonical_ast recursive arm into one-liners. - Drop ASYNC21X's bespoke urllib3-import set -- consult the shared imports map. - Collapse ASYNC22X's raw_name/canonical/func_name triplet into two locals. - Simplify with_has_call's canonical fallback to a startswith + suffix check. - Consolidate the CST scope-tracker's three visit/leave pairs into shared helpers. - Rewrite the narrative "this change" comments as reader-facing rationale, both in the code and in the eval-file annotations. https://claude.ai/code/session_018Hc9rcA31SnXcN8Ee5vVwH
for more information, see https://pre-commit.ci
- Move resolve_canonical_ast/cst into a dedicated _canonical module so the base-class methods can import them at the top level (PLC0415). - Flatten nested isinstance chain in get_matching_call_cst (SIM102). - Reformat the import-tracker example table so ruff stops flagging the continuation line as commented-out code (ERA001). - Inline the isinstance(ast.Call) check in critical_except so mypy's narrowing kicks in (attr-defined on "expr"). - Drop the now-unused identifier_to_string import from visitor91x. https://claude.ai/code/session_018Hc9rcA31SnXcN8Ee5vVwH
for more information, see https://pre-commit.ci
The changelog entry `:ref:`ASYNC106 <async106>`` targets a rule that didn't have a Sphinx label, which made readthedocs fail with `undefined label: 'async106'` under `-W`. https://claude.ai/code/session_018Hc9rcA31SnXcN8Ee5vVwH
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
ASYNC106rule existed because it was easier to require users to write exactlyimport triorather than track all the various ways that things could be imported and pass that through to all the rules. Happily, Claude has now done that for us 😁Closes #132.