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
| import dataclasses | |
| from functools import partial | |
| from typing import TYPE_CHECKING, TypeAlias | |
| if TYPE_CHECKING: | |
| from dataclasses import dataclass as frozen_nan_safe | |
| else: | |
| def frozen_nan_safe(**kwargs): | |
| assert kwargs['frozen'] | |
| return dataclasses.dataclass(**kwargs) |
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
| import dataclasses | |
| from functools import partial | |
| from typing import TYPE_CHECKING | |
| if TYPE_CHECKING: | |
| frozen_nan_safe = dataclasses.dataclass | |
| else: | |
| frozen_nan_safe = partial(dataclasses.dataclass, frozen=True) | |
| def test_frozen_decorator() -> None: |
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
| from typing import Callable, Protocol | |
| ### Example 1: Works as intended. The method is decorated and is | |
| ### callable as usual. | |
| def decorate_method[T](method: Callable[[T], None]) -> Callable[[T], None]: | |
| def _inner(_self: T) -> None: | |
| print('Decorated!') | |
| method(_self) |
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
| class A: | |
| def __new__(cls) -> "A": | |
| return super().__new__(cls) | |
| class B(A): | |
| pass | |
| reveal_type(B()) |
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
| from typing import Literal | |
| Address = int | |
| Data = int | |
| Operation = Literal["read", "write"] | |
| class AddressBus: | |
| def __init__(self) -> None: | |
| self._address: Address = 0x0000 | |
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
| from typing import Literal | |
| Address = int | |
| Data = int | |
| Operation = Literal["read", "write"] | |
| class AddressBus: | |
| def __init__(self) -> None: | |
| self._address: Address = 0x0000 | |
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
| from collections.abc import Callable | |
| from functools import wraps | |
| from inspect import signature | |
| from types import FunctionType | |
| def func() -> None: | |
| pass | |
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
| import asyncio | |
| from typing import overload, Literal | |
| class Redis: | |
| def get(self): | |
| pass | |
| class RedisAsync: | |
| async def get(self): | |
| pass |
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
| from typing import Iterator | |
| from warnings import deprecated | |
| @deprecated("...") | |
| def fib(n: int) -> Iterator[int]: | |
| a, b = 0, 1 | |
| while a < n: | |
| yield a | |
| a, b = b, a + b |
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
| from typing import TypeIs | |
| def check(x: str | int | list[int]) -> TypeIs[int]: | |
| return isinstance(x, int) | |
| def foo(x: str | int | list[int]) -> None: | |
| if not check(x): | |
| reveal_type(x) |
NewerOlder