mirror of
https://github.com/microsoft/pyright.git
synced 2024-08-16 11:20:22 +03:00
Changed text representation of inferred type of self
and cls
parameters to Self@ClassName
. This is more consistent with the emerging standard for an explicit Self
type.
This commit is contained in:
parent
fbd8cfe991
commit
cc7216e66e
@ -304,17 +304,17 @@ Notice that the type of variable `sum` is reported with asterisks (`*`). This in
|
||||
|
||||
When a type annotation for a method’s `self` or `cls` parameter is omitted, pyright will infer its type based on the class that contains the method. The inferred type is internally represented as a type variable that is bound to the class.
|
||||
|
||||
Within the function, the type of `self` is printed with a tilde preceding the class name. This indicates that the type is a TypeVar bound to the class rather than the class itself. Outside of the function, this TypeVar is resolved based on the usage.
|
||||
The type of `self` is represented as `Self@ClassName` where `ClassName` is the class that contains the method. Likewise, the `cls` parameter in a class method will have the type `Type[Self@ClassName]`.
|
||||
|
||||
```python
|
||||
class Parent:
|
||||
def method1(self):
|
||||
reveal_type(self) # ~Parent
|
||||
reveal_type(self) # Self@Parent
|
||||
return self
|
||||
|
||||
@classmethod
|
||||
def method2(cls):
|
||||
reveal_type(cls) # Type[~Parent]
|
||||
reveal_type(cls) # Type[Self@Parent]
|
||||
return cls
|
||||
|
||||
class Child(Parent):
|
||||
|
@ -398,7 +398,7 @@ export function printType(
|
||||
(printTypeFlags & PrintTypeFlags.OmitSelfClsTypeIndicator) === 0 &&
|
||||
!isAnyOrUnknown(type.details.boundType)
|
||||
) {
|
||||
boundTypeString = `~${boundTypeString}`;
|
||||
boundTypeString = `Self@${boundTypeString}`;
|
||||
}
|
||||
|
||||
if (TypeBase.isInstantiable(type)) {
|
||||
|
@ -8,14 +8,14 @@ class Foo:
|
||||
@classmethod
|
||||
def bar(cls, other: type):
|
||||
if issubclass(other, cls):
|
||||
t1: Literal["Type[~Foo]"] = reveal_type(other)
|
||||
t1: Literal["Type[Self@Foo]"] = reveal_type(other)
|
||||
|
||||
if issubclass(other, (int, cls)):
|
||||
t2: Literal["Type[~Foo] | Type[int]"] = reveal_type(other)
|
||||
t2: Literal["Type[Self@Foo] | Type[int]"] = reveal_type(other)
|
||||
|
||||
def baz(self, other: object):
|
||||
if isinstance(other, self.__class__):
|
||||
t1: Literal["~Foo"] = reveal_type(other)
|
||||
t1: Literal["Self@Foo"] = reveal_type(other)
|
||||
|
||||
if isinstance(other, (int, self.__class__)):
|
||||
t2: Literal["~Foo | int"] = reveal_type(other)
|
||||
t2: Literal["Self@Foo | int"] = reveal_type(other)
|
||||
|
@ -72,4 +72,4 @@ class B:
|
||||
|
||||
b = B()
|
||||
t_b1: Literal["str"] = reveal_type(b.foo)
|
||||
t_b2: Literal["Minimal[~B, str]"] = reveal_type(B.foo)
|
||||
t_b2: Literal["Minimal[Self@B, str]"] = reveal_type(B.foo)
|
||||
|
@ -9,7 +9,7 @@ FooBase = NamedTuple("FooBase", [("x", int)])
|
||||
class Foo(FooBase):
|
||||
def __new__(cls):
|
||||
obj = super().__new__(cls, x=1)
|
||||
t1: Literal["~Foo"] = reveal_type(obj)
|
||||
t1: Literal["Self@Foo"] = reveal_type(obj)
|
||||
return obj
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user