[ty] Don't confuse multiple occurrences of typing.Self when binding bound methods by dcreager · Pull Request #21754 · astral-sh/ruff (original) (raw)
added 2 commits
Multi-file analysis & type inference
label
dcreager added a commit that referenced this pull request
- origin/main: (67 commits)
Move
Token,TokenKindandTokenstoruff-python-ast(#21760) [ty] Don't confuse multiple occurrences oftyping.Selfwhen binding bound methods (#21754) Use our org-wide Renovate preset (#21759) Deletemy-script.py(#21751) [ty] Moveall_members, and related types/routines, out ofide_support.rs(#21695) [ty] Fix find-references for import aliases (#21736) [ty] add tests for workspaces (#21741) [ty] Stop testing the (brittle) constraint set display implementation (#21743) [ty] Use generator over list comprehension to avoid cast (#21748) [ty] Add a diagnostic for prohibitedNamedTupleattribute overrides (#21717) [ty] Fix subtyping withtype[T]and unions (#21740) Usenpm ci --ignore-scriptseverywhere (#21742) [flake8-simplify] Fix truthiness assumption for non-iterable arguments in tuple/list/set calls (SIM222,SIM223) (#21479) [flake8-use-pathlib] Mark fixes unsafe for return type changes (PTH104,PTH105,PTH109,PTH115) (#21440) [ty] Fix auto-import code action to handle pre-existing import Enable PEP 740 attestations when publishing to PyPI (#21735) [ty] Fix find references for type defined in stub (#21732) Use OIDC instead of codspeed token (#21719) [ty] Excludetyping_extensionsfrom completions unless it's really available [ty] Fix false positives forclass F(Generic[*Ts]): ...(#21723) ...
AlexWaygood pushed a commit that referenced this pull request
… bound methods (#21754)
In the following example, there are two occurrences of typing.Self,
one for Foo.foo and one for Bar.bar:
from typing import Self, reveal_type
class Foo[T]:
def foo(self: Self) -> T:
raise NotImplementedError
class Bar:
def bar(self: Self, x: Foo[Self]):
# SHOULD BE: bound method Foo[Self@bar].foo() -> Self@bar
# revealed: bound method Foo[Self@bar].foo() -> Foo[Self@bar]
reveal_type(x.foo)
def f[U: Bar](x: Foo[U]):
# revealed: bound method Foo[U@f].foo() -> U@f
reveal_type(x.foo)When accessing a bound method, we replace any occurrences of Self with
the bound self type.
We were doing this correctly for the second reveal. We would first apply
the specialization, getting (self: Self@foo) -> U@F as the signature
of x.foo. We would then bind the self parameter, substituting
Self@foo with Foo[U@F] as part of that. The return type was already
specialized to U@F, so that substitution had no further affect on the
type that we revealed.
In the first reveal, we would follow the same process, but we confused
the two occurrences of Self. We would first apply the specialization,
getting (self: Self@foo) -> Self@bar as the method signature. We would
then try to bind the self parameter, substituting Self@foo with
Foo[Self@bar]. However, because we didn't distinguish the two separate
Selfs, and applied the substitution to the return type as well as to
the self parameter.
The fix is to track which particular Self we're trying to substitute
when applying the type mapping.
Fixes astral-sh/ty#1713
dcreager added a commit that referenced this pull request
- origin/main:
[ty] Improve
@override,@finaland Liskov checks in cases where there are multiple reachable definitions (#21767) [ty] Extendinvalid-explicit-overrideto also cover properties decorated with@overridethat do not override anything (#21756) [ty] Enable LRU collection for parsed module (#21749) [ty] Support typevar-specialized dynamic types in generic type aliases (#21730) Add token basedparenthesized_rangesimplementation (#21738) [ty] Default-specialization of generic type aliases (#21765) [ty] Suppress false positives whendataclasses.dataclass(...)(cls)is called imperatively (#21729) [syntax-error] Default type parameter followed by non-default type parameter (#21657) new module for parsing ranged suppressions (#21441) [ty]type[T]is assignable to an inferable typevar (#21766) Fix syntax error false positives forawaitoutside functions (#21763) [ty] Improve diagnostics for unsupported comparison operations (#21737) MoveToken,TokenKindandTokenstoruff-python-ast(#21760) [ty] Don't confuse multiple occurrences oftyping.Selfwhen binding bound methods (#21754) Use our org-wide Renovate preset (#21759) Deletemy-script.py(#21751) [ty] Moveall_members, and related types/routines, out ofide_support.rs(#21695)
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
[ Show hidden characters]({{ revealButtonHref }})