ruff/crates/ty_python_semantic/resources/mdtest/annotations
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
..
annotated.md [ty] Add hints to invalid-type-form for common mistakes (#18543) 2025-06-09 00:40:05 +01:00
any.md [ty] Completely ignore typeshed's stub for Any (#20079) 2025-08-25 15:27:55 +01:00
callable.md [ty] Fall back to object for attribute access on synthesized protocols (#20286) 2025-09-08 13:04:37 +01:00
deferred.md Rename Red Knot (#17820) 2025-05-03 19:49:15 +02:00
int_float_complex.md [ty] Improve disjointness inference for NominalInstanceTypes and SubclassOfTypes (#18864) 2025-06-24 20:27:37 +00:00
invalid.md [syntax-errors] Detect yield from inside async function (#20051) 2025-09-03 10:13:05 -04:00
literal.md Fix panic for illegal Literal[…] annotations with inner subscript expressions (#19489) 2025-07-22 14:07:20 +00:00
literal_string.md Rename Red Knot (#17820) 2025-05-03 19:49:15 +02:00
never.md Rename Red Knot (#17820) 2025-05-03 19:49:15 +02:00
new_types.md ty_python_semantic: add union type context to function call type errors 2025-05-09 13:40:51 -04:00
optional.md Rename Red Knot (#17820) 2025-05-03 19:49:15 +02:00
self.md [ty] Bind Self typevar to method context (#20366) 2025-09-17 14:58:54 -04:00
starred.md [ty] Return Option<TupleType> from infer_tuple_type_expression (#19735) 2025-08-04 13:48:19 +01:00
stdlib_typing_aliases.md [ty] Support using legacy typing aliases for generic classes in type annotations (#18404) 2025-06-03 12:09:51 +01:00
string.md [ty] Emit error for invalid binary operations in type expressions (#18991) 2025-06-30 10:06:01 +02:00
union.md [ty] Consistent use of American english (in rules) (#19488) 2025-07-22 16:10:38 +02:00
unsupported_special_forms.md [ty] Reduce false positives for ParamSpecs and TypeVarTuples (#20239) 2025-09-04 23:34:37 +01:00
unsupported_special_types.md Generalize special-casing for enums constructed with the functional syntax (#17885) 2025-05-06 11:02:55 +01:00
unsupported_type_qualifiers.md [ty] validate constructor call of TypedDict (#19810) 2025-08-25 14:45:52 +02:00