-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ty] Infer type of self as typing.Self in method body #18473
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
sharkdp
merged 56 commits into
astral-sh:david/type-of-self-in-methods-integration
from
Glyphack:typing-self-function-scope
Oct 16, 2025
Merged
Changes from 37 commits
Commits
Show all changes
56 commits
Select commit
Hold shift + click to select a range
0e3d511
Infer type of self as typing.Self in method body
Glyphack 6913d99
Handle type var in SuperOwnerKind::try_from_type
Glyphack 466c93e
Update class super test
Glyphack 3f2c487
Mark failing tests
Glyphack fd732c5
Don't emit diagnostic for assigning to self
Glyphack b6449d3
Don't unwrap the self type carelessly
Glyphack deb7a96
Mark attributes failing tests as todo
Glyphack 3586737
Fix trait upcasting coercion is experimental
Glyphack c21827d
Fix too many iterations on comtypes
Glyphack 495278d
Apply comments
Glyphack 854ddee
Temp fix completions
Glyphack 6bf8513
Add apprise to bad.txt
Glyphack 2c24356
Remove apprise from good.txt
Glyphack 9a6ae4f
Link issue
Glyphack ce1d951
Remove upcast
Glyphack e9aec22
Update TODO mdtests
Glyphack 49c310a
Update snapshots
Glyphack c3d7304
Move discord.py to bad.txt
Glyphack 053f651
Increase max_diagnostics
Glyphack 1f71b74
Disallow complex exprs in constraint builder
Glyphack 2acdbc7
Add test for cycle count
Glyphack 4818f33
Revert falling back to Ambiguous for complex expressions
Glyphack ae79edf
Use infer_expression_types instead of infer_expression_types in Reach…
Glyphack 08e8695
Remove gridout test case
Glyphack 241bf63
Set correct typevar_binding_context
Glyphack d0acd44
Decrease MAX_UNION_LITERALS to avoid panic
Glyphack f67cc04
Handle both NonInferableTypeVar in super args
Glyphack 5054fda
Update snapshots
Glyphack e877537
Update mdtest assertion
Glyphack 58b02a2
Increase diagnostic count
Glyphack 4205dbf
Merge remote-tracking branch 'origin/main' into typing-self-function-…
sharkdp 0df523f
Minor review comments
sharkdp f24f495
Move failing projects to bad.txt
sharkdp d08f955
Treat __new__ as a static method
sharkdp a37ee79
Disable sympy in walltime benchmarks
sharkdp 8305105
Disable prefect in memory statistics
sharkdp 51640f2
Bring back TODO comment
sharkdp 45421b6
Remove comment
sharkdp 347892f
Add comment why benchmark was disabled
sharkdp 5be3a5a
Disable pandas as a walltime benchmark
sharkdp baf30b4
Minor adjustments to special_first_method_parameter_type
sharkdp 2a62a7d
Increase another threshold
sharkdp 921119a
Merge branch 'main' into typing-self-function-scope
MichaReiser a02b03f
Discard changes to playground/ty/src/Editor/Editor.tsx
MichaReiser 9caa105
Revert mypy primer changes
MichaReiser c0043d2
Merge remote-tracking branch 'origin/main' into typing-self-function-…
sharkdp 32be31d
Update Salsa
MichaReiser 56db0b9
Revert "Update Salsa"
sharkdp 0056416
Remove FIXME comment
sharkdp c1cab1f
Minor rewording/renaming
sharkdp da56e21
Use new typing_self helper
sharkdp fa83f8f
Remove confusing TODO annotations
sharkdp b696145
Minor comment cleanup
sharkdp 13457d2
Merge remote-tracking branch 'origin/main' into typing-self-function-…
sharkdp 1b36000
Merge remote-tracking branch 'origin/main' into typing-self-function-…
sharkdp a4d6a2a
Update tests
sharkdp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,9 +26,7 @@ class C: | |
| c_instance = C(1) | ||
|
|
||
| reveal_type(c_instance.inferred_from_value) # revealed: Unknown | Literal[1, "a"] | ||
|
|
||
| # TODO: Same here. This should be `Unknown | Literal[1, "a"]` | ||
| reveal_type(c_instance.inferred_from_other_attribute) # revealed: Unknown | ||
| reveal_type(c_instance.inferred_from_other_attribute) # revealed: Unknown | Literal[1, "a"] | ||
|
|
||
| # There is no special handling of attributes that are (directly) assigned to a declared parameter, | ||
| # which means we union with `Unknown` here, since the attribute itself is not declared. This is | ||
|
|
@@ -177,8 +175,7 @@ c_instance = C(1) | |
|
|
||
| reveal_type(c_instance.inferred_from_value) # revealed: Unknown | Literal[1, "a"] | ||
|
|
||
| # TODO: Should be `Unknown | Literal[1, "a"]` | ||
| reveal_type(c_instance.inferred_from_other_attribute) # revealed: Unknown | ||
| reveal_type(c_instance.inferred_from_other_attribute) # revealed: Unknown | Literal[1, "a"] | ||
|
|
||
| reveal_type(c_instance.inferred_from_param) # revealed: Unknown | int | None | ||
|
|
||
|
|
@@ -399,9 +396,19 @@ class TupleIterable: | |
|
|
||
| class C: | ||
| def __init__(self) -> None: | ||
| # TODO: Should not emit this diagnostic | ||
| # error: [unresolved-attribute] | ||
| [... for self.a in IntIterable()] | ||
| # TODO: Should not emit this diagnostic | ||
| # error: [unresolved-attribute] | ||
| # error: [unresolved-attribute] | ||
| [... for (self.b, self.c) in TupleIterable()] | ||
| # TODO: Should not emit this diagnostic | ||
| # error: [unresolved-attribute] | ||
| # error: [unresolved-attribute] | ||
| [... for self.d in IntIterable() for self.e in IntIterable()] | ||
| # TODO: Should not emit this diagnostic | ||
| # error: [unresolved-attribute] | ||
| [[... for self.f in IntIterable()] for _ in IntIterable()] | ||
| [[... for self.g in IntIterable()] for self in [D()]] | ||
|
|
||
|
|
@@ -598,6 +605,8 @@ class C: | |
| self.c = c | ||
| if False: | ||
| def set_e(self, e: str) -> None: | ||
| # TODO: Should not emit this diagnostic | ||
| # error: [unresolved-attribute] | ||
sharkdp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| self.e = e | ||
|
|
||
| # TODO: this would ideally be `Unknown | Literal[1]` | ||
|
|
@@ -685,7 +694,7 @@ class C: | |
| pure_class_variable2: ClassVar = 1 | ||
|
|
||
| def method(self): | ||
| # TODO: this should be an error | ||
| # error: [invalid-attribute-access] "Cannot assign to ClassVar `pure_class_variable1` from an instance of type `Self@method`" | ||
| self.pure_class_variable1 = "value set through instance" | ||
|
|
||
| reveal_type(C.pure_class_variable1) # revealed: str | ||
|
|
@@ -1759,6 +1768,7 @@ def external_getattribute(name) -> int: | |
|
|
||
| class ThisFails: | ||
| def __init__(self): | ||
| # error: [invalid-assignment] "Implicit shadowing of function `__getattribute__`" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are a few new "implicit shadowing of function …" diagnostics in the diff. This seems sort-of fine, but maybe worth looking into. |
||
| self.__getattribute__ = external_getattribute | ||
|
|
||
| # error: [unresolved-attribute] | ||
|
|
||
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.