ruff/crates
Shaygan Hooshyari 05622ae757
[ty] Bind Self typevar to method context (#20366)
Fixes: https://github.com/astral-sh/ty/issues/1173

<!--
Thank you for contributing to Ruff/ty! To help us out with reviewing,
please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title? (Please prefix
with `[ty]` for ty pull
  requests.)
- Does this pull request include references to any relevant issues?
-->

## Summary

This PR will change the logic of binding Self type variables to bind
self to the immediate function that it's used on.
Since we are binding `self` to methods and not the class itself we need
to ensure that we bind self consistently.

The fix is to traverse scopes containing the self and find the first
function inside a class and use that function to bind the typevar for
self.

If no such scope is found we fallback to the normal behavior. Using Self
outside of a class scope is not legal anyway.

## Test Plan

Added a new mdtest.

Checked the diagnostics that are not emitted anymore in [primer
results](https://github.com/astral-sh/ruff/pull/20366#issuecomment-3289411424).
It looks good altough I don't completely understand what was wrong
before.

---------

Co-authored-by: Douglas Creager <dcreager@dcreager.net>
2025-09-17 14:58:54 -04:00
..
ruff [ty] Add GitHub output format (#20358) 2025-09-17 09:50:25 -04:00
ruff_annotate_snippets Move full diagnostic rendering to ruff_db (#19415) 2025-08-08 12:56:23 -04:00
ruff_benchmark Revert "[ruff]: Build loongarch64 binaries in CI (#20361)" (#20372) 2025-09-12 17:21:04 -04:00
ruff_cache Switch to Rust 2024 edition (#18129) 2025-05-16 13:25:28 +02:00
ruff_db [ty] Add GitHub output format (#20358) 2025-09-17 09:50:25 -04:00
ruff_dev [ty] Remove duplicate global lint registry (#20053) 2025-08-22 19:43:12 -04:00
ruff_diagnostics Fix rust feature activation (#20012) 2025-08-21 09:26:06 +02:00
ruff_formatter Add support for using uv as an alternative formatter backend (#19665) 2025-09-09 20:39:53 +05:30
ruff_graph [ty] Remove KnownModule::is_enum (#19681) 2025-08-01 10:31:12 +02:00
ruff_index Update Rust toolchain to 1.88 and MSRV to 1.86 (#19011) 2025-06-28 20:24:00 +02:00
ruff_linter [ruff] Remove Locator from Importer 2025-09-17 13:59:28 -04:00
ruff_macros Don't cache files with diagnostics (#19869) 2025-08-12 15:28:44 -04:00
ruff_memory_usage [ty] Track heap usage of salsa structs (#19790) 2025-08-12 13:28:44 +02:00
ruff_notebook Improve diff rendering for notebooks (#20036) 2025-08-25 09:20:42 -04:00
ruff_options_metadata Update Rust toolchain to 1.89 (#19807) 2025-08-07 18:21:50 +02:00
ruff_python_ast Track t-strings and f-strings for token-based rules and suppression comments (#20357) 2025-09-12 13:00:12 -05:00
ruff_python_ast_integration_tests Disallow implicit concatenation of t-strings and other string types (#19485) 2025-07-27 12:41:03 +00:00
ruff_python_codegen [ruff] Provide a way to get an owned Stylist 2025-09-17 13:59:28 -04:00
ruff_python_formatter Add support for using uv as an alternative formatter backend (#19665) 2025-09-09 20:39:53 +05:30
ruff_python_importer [ruff] Add API for splicing into an existing import statement 2025-09-17 13:59:28 -04:00
ruff_python_index Track t-strings and f-strings for token-based rules and suppression comments (#20357) 2025-09-12 13:00:12 -05:00
ruff_python_literal Switch to Rust 2024 edition (#18129) 2025-05-16 13:25:28 +02:00
ruff_python_parser [syntax-errors]: import from * only allowed at module scope (F406) (#20166) 2025-09-16 15:53:28 -04:00
ruff_python_semantic [ruff] Allow dataclass attribute value instantiation from nested frozen dataclass (RUF009) (#20352) 2025-09-12 16:46:49 -04:00
ruff_python_stdlib Switch to Rust 2024 edition (#18129) 2025-05-16 13:25:28 +02:00
ruff_python_trivia [ruff] Preserve relative whitespace in multi-line expressions (RUF033) (#19647) 2025-08-27 19:15:44 +00:00
ruff_python_trivia_integration_tests Switch to Rust 2024 edition (#18129) 2025-05-16 13:25:28 +02:00
ruff_server Remove Diagnostic::expect_range and all consumers (#20322) 2025-09-10 17:19:20 -07:00
ruff_source_file Move diff rendering to ruff_db (#20006) 2025-08-21 09:47:00 -04:00
ruff_text_size [ruff] Add TextRange::to_std_range 2025-09-17 13:59:28 -04:00
ruff_wasm [playground] Enable inline noqa for multiline strings in playground (#20442) 2025-09-17 09:29:40 +02:00
ruff_workspace [ruff] Add analyze.string-imports-min-dots to settings documentation (#20375) 2025-09-16 13:19:34 +02:00
ty [ty] Fix a bug with "all_submodule_names_for_package" API 2025-09-17 13:59:28 -04:00
ty_combine [ty] Disallow std::env and io methods in most ty crates (#20046) 2025-08-22 11:13:47 -07:00
ty_ide [ruff] Remove Locator from Importer 2025-09-17 13:59:28 -04:00
ty_project [ty] Add GitHub output format (#20358) 2025-09-17 09:50:25 -04:00
ty_python_semantic [ty] Bind Self typevar to method context (#20366) 2025-09-17 14:58:54 -04:00
ty_server [ty] Refactor to handle unimported completions 2025-09-17 13:59:28 -04:00
ty_static [ty] Disallow std::env and io methods in most ty crates (#20046) 2025-08-22 11:13:47 -07:00
ty_test [ty] Add functions for revealing assignability/subtyping constraints (#20217) 2025-09-03 16:44:35 -04:00
ty_vendored [ty] More constraint set simplifications via simpler constraint representation (#20423) 2025-09-16 10:05:01 -04:00
ty_wasm [ty] Add new completion data to wasm bridge 2025-09-17 13:59:28 -04:00