Skip to content

[ty] Handle Definitions in SemanticModel::scope#21919

Merged
MichaReiser merged 2 commits intomainfrom
micha/model-scope-definitions
Dec 11, 2025
Merged

[ty] Handle Definitions in SemanticModel::scope#21919
MichaReiser merged 2 commits intomainfrom
micha/model-scope-definitions

Conversation

@MichaReiser
Copy link
Member

@MichaReiser MichaReiser commented Dec 11, 2025

Summary

I called definitions_for_name, where the node was a function definition, and I was really
surprised that it returned no results, ever...

The issue was that SemanticModel::scope currently only handles expressions and identifiers.

This PR adds the necessary handling to lookup the scope in which the function, class, ... is defined.

Test Plan

I don't think this is used anywhere today. But seems a footgun not to have from an API standpoint.

@MichaReiser MichaReiser requested a review from carljm as a code owner December 11, 2025 15:15
@MichaReiser MichaReiser added the internal An internal refactor or improvement label Dec 11, 2025
@MichaReiser MichaReiser added the ty Multi-file analysis & type inference label Dec 11, 2025
@MichaReiser MichaReiser added internal An internal refactor or improvement ty Multi-file analysis & type inference labels Dec 11, 2025
@MichaReiser MichaReiser force-pushed the micha/model-scope-definitions branch from a3aa3c4 to c4ba7e6 Compare December 11, 2025 15:17
@MichaReiser MichaReiser changed the title [ty] Handle definitions in SemanticModel::scope [ty] Handle Definitions in SemanticModel::scope Dec 11, 2025
@astral-sh-bot
Copy link

astral-sh-bot bot commented Dec 11, 2025

Diagnostic diff on typing conformance tests

No changes detected when running ty on typing conformance tests ✅

@AlexWaygood
Copy link
Member

AlexWaygood commented Dec 11, 2025

There are other nodes that can create definitions: for loops, match patterns, with statements, etc. But idk if you were trying to be exhaustive :-)

@astral-sh-bot
Copy link

astral-sh-bot bot commented Dec 11, 2025

mypy_primer results

Changes were detected when running on open source projects
pandas-stubs (https://github.com/pandas-dev/pandas-stubs)
- pandas-stubs/_typing.pyi:1217:16: warning[unused-ignore-comment] Unused blanket `type: ignore` directive
- Found 5123 diagnostics
+ Found 5122 diagnostics

No memory usage changes detected ✅

@MichaReiser
Copy link
Member Author

There are other nodes that can create definitions: for loops, match patterns, with statements, etc. But idk if you were trying to be exhaustive :-)

I added the nodes that support inferred_type. I don't think we need special handling for for because the target is an ExprName. Same for match where the target is an Identifier (I think).

@AlexWaygood
Copy link
Member

Makes sense, thanks. It might be worth adding a comment to the match statement that explains why some definition nodes have their own branches while many others don't

Copy link
Contributor

@Gankra Gankra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IDE's always happy for more of these kinds of lookups to work.

@MichaReiser MichaReiser enabled auto-merge (squash) December 11, 2025 17:59
@MichaReiser MichaReiser merged commit 34f7a04 into main Dec 11, 2025
35 of 41 checks passed
@MichaReiser MichaReiser deleted the micha/model-scope-definitions branch December 11, 2025 18:04
dcreager added a commit that referenced this pull request Dec 11, 2025
* origin/main: (29 commits)
  Document range suppressions, reorganize suppression docs (#21884)
  Ignore ruff:isort like ruff:noqa in new suppressions (#21922)
  [ty] Handle `Definition`s in `SemanticModel::scope` (#21919)
  [ty] Attach salsa db when running ide tests for easier debugging (#21917)
  [ty] Don't show hover for expressions with no inferred type (#21924)
  [ty] avoid unions of generic aliases of the same class in fixpoint (#21909)
  [ty] Squash false positive logs for failing to find `builtins` as a real module
  [ty] Uniformly use "not supported" in diagnostics (#21916)
  [ty] Reduce size of ty-ide snapshots (#21915)
  [ty] Adjust scope completions to use all reachable symbols
  [ty] Rename `all_members_of_scope` to `all_end_of_scope_members`
  [ty] Remove `all_` prefix from some routines on UseDefMap
  Enable `--document-private-items` for `ruff_python_formatter` (#21903)
  Remove `BackwardsTokenizer` based `parenthesized_range` references in `ruff_linter` (#21836)
  [ty] Revert "Do not infer types for invalid binary expressions in annotations" (#21914)
  Skip over trivia tokens after re-lexing (#21895)
  [ty] Avoid inferring types for invalid binary expressions in string annotations (#21911)
  [ty] Improve overload call resolution tracing (#21913)
  [ty] fix missing heap_size on Salsa query (#21912)
  [ty] Support implicit type of `cls` in signatures (#21771)
  ...
dcreager added a commit that referenced this pull request Dec 11, 2025
* origin/main: (36 commits)
  [ty] Defer all parameter and return type annotations (#21906)
  [ty] Fix workspace symbols to return members too (#21926)
  Document range suppressions, reorganize suppression docs (#21884)
  Ignore ruff:isort like ruff:noqa in new suppressions (#21922)
  [ty] Handle `Definition`s in `SemanticModel::scope` (#21919)
  [ty] Attach salsa db when running ide tests for easier debugging (#21917)
  [ty] Don't show hover for expressions with no inferred type (#21924)
  [ty] avoid unions of generic aliases of the same class in fixpoint (#21909)
  [ty] Squash false positive logs for failing to find `builtins` as a real module
  [ty] Uniformly use "not supported" in diagnostics (#21916)
  [ty] Reduce size of ty-ide snapshots (#21915)
  [ty] Adjust scope completions to use all reachable symbols
  [ty] Rename `all_members_of_scope` to `all_end_of_scope_members`
  [ty] Remove `all_` prefix from some routines on UseDefMap
  Enable `--document-private-items` for `ruff_python_formatter` (#21903)
  Remove `BackwardsTokenizer` based `parenthesized_range` references in `ruff_linter` (#21836)
  [ty] Revert "Do not infer types for invalid binary expressions in annotations" (#21914)
  Skip over trivia tokens after re-lexing (#21895)
  [ty] Avoid inferring types for invalid binary expressions in string annotations (#21911)
  [ty] Improve overload call resolution tracing (#21913)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

internal An internal refactor or improvement ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants