This file contains 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
from typing import Literal, reveal_type | |
def sign(x: int) -> Literal[-1, 0, 1]: | |
"""Extracts the sign of x.""" | |
s = -1 if x < 0 else (0 if x == 0 else 1) | |
reveal_type(s) # int | |
match s: | |
case -1 | 0 | 1: | |
reveal_type(s) # narrowed to Literal[-1, 0, 1] | |
return s |
This file contains 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
from typing import Any, cast, Generic, overload, Self, TypeVar | |
T = TypeVar('T') | |
class CustomProperty(property, Generic[T]): | |
@overload | |
def __get__(self, instance: None, owner: type | None = None, /) -> Self: ... | |
@overload | |
def __get__(self, instance: Any, owner: type | None = None, /) -> T: ... |
This file contains 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
from typing import Any, cast, Generic, overload, Self, TypeVar | |
T = TypeVar('T') | |
class CustomProperty(property, Generic[T]): | |
@overload # type: ignore[override] | |
def __get__(self, instance: None, owner: type[Any] | None, /) -> Self: ... | |
@overload | |
def __get__(self, instance: Any, owner: type[Any] | None, /) -> T: ... |
This file contains 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
from typing import Any, cast, Generic, overload, Self, TypeVar | |
T = TypeVar('T') | |
class CustomProperty(property, Generic[T]): | |
@overload # type: ignore[override] | |
def __get__(self, instance: None, owner: type[Any] | None, /) -> Self: ... | |
@overload | |
def __get__(self, instance: Any, owner: type[Any] | None, /) -> T: ... |
This file contains 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
t1 = () | |
t2 = tuple() | |
reveal_type(t1) # tuple[()] | |
reveal_type(t2) # tuple[Any, ...] | |
foo = lambda: list(t1) | |
bar = lambda: list(t2) | |
qux = lambda: list(tuple()) |
This file contains 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
# mypy: no-strict-optional | |
from typing import overload, Protocol | |
class Descriptor: | |
@overload | |
def __get__(self, instance: None, owner: type) -> int: ... | |
@overload | |
def __get__(self, instance: object, owner: type) -> str: ... |
This file contains 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
from typing import Protocol, final | |
@final | |
class Inter(Protocol): | |
x: int | |
def y(self) -> int: | |
... | |
This file contains 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
from typing import Protocol, final | |
@final | |
class Inter(Protocol): | |
x: int | |
def y(self) -> int: | |
... | |
@property |
This file contains 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
class X: | |
val: object | |
class Y(X): | |
@property | |
def val(self) -> object: | |
return object() | |
@val.setter | |
def val(self, value): | |
self.x = True |
This file contains 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
from typing import Protocol, final | |
@final | |
class Inter(Protocol): | |
x: int | |
def y(self) -> int: | |
... | |
@property |
NewerOlder