File tree Expand file tree Collapse file tree 2 files changed +19
-4
lines changed
crates/ty_python_semantic
resources/mdtest/annotations Expand file tree Collapse file tree 2 files changed +19
-4
lines changed Original file line number Diff line number Diff line change 2
2
3
3
``` toml
4
4
[environment ]
5
- python-version = " 3.11 "
5
+ python-version = " 3.13 "
6
6
```
7
7
8
8
` Self ` is treated as if it were a ` TypeVar ` bound to the class it's being used on.
@@ -147,6 +147,23 @@ class Shape:
147
147
return self
148
148
```
149
149
150
+ ## ` Self ` for classes with a default value for their generic parameter
151
+
152
+ This is a regression test for < https://github.com/astral-sh/ty/issues/1156 > .
153
+
154
+ ``` py
155
+ from typing import Self
156
+
157
+ class Container[T = bytes ]:
158
+ def __init__ (self : Self, data : T | None = None ) -> None :
159
+ self .data = data
160
+
161
+ reveal_type(Container()) # revealed: Container[bytes]
162
+ reveal_type(Container(1 )) # revealed: Container[int]
163
+ reveal_type(Container(" a" )) # revealed: Container[str]
164
+ reveal_type(Container(b " a" )) # revealed: Container[bytes]
165
+ ```
166
+
150
167
## Invalid Usage
151
168
152
169
` Self ` cannot be used in the signature of a function or variable.
Original file line number Diff line number Diff line change @@ -5711,9 +5711,7 @@ impl<'db> Type<'db> {
5711
5711
] ,
5712
5712
} ) ;
5713
5713
} ;
5714
- let instance = Type :: ClassLiteral ( class) . to_instance ( db) . expect (
5715
- "nearest_enclosing_class must return type that can be instantiated" ,
5716
- ) ;
5714
+ let instance = Type :: instance ( db, class. unknown_specialization ( db) ) ;
5717
5715
let class_definition = class. definition ( db) ;
5718
5716
let typevar = TypeVarInstance :: new (
5719
5717
db,
You can’t perform that action at this time.
0 commit comments