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
import typing | |
class typed_dict(typing.TypedDict, total=False): | |
user_id: int | |
class HasTypedDict: | |
TypedDict = typed_dict | |
TypedDict_TypeAlias: typing.TypeAlias = typed_dict |
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
def fun(a: int, b: str) -> None: | |
pass | |
call_args = {"a": 1, "b": "bbb"} | |
fun(**call_args) |
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 Annotated, reveal_type | |
def hello(code: Annotated[str, "html"]): | |
reveal_type(code) | |
hello() |
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 Dict, Iterator, TypeVar, Union | |
class A: | |
name: str = "" | |
K = TypeVar('K', bound=Union[str, A]) | |
def a(arg: Dict[K, int]) -> None: | |
d = dict( | |
(k.name if isinstance(k, A) else k, v) |
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
i: int | |
s: str | |
if i == s: | |
print("hi") | |
a: int | str | |
b: str | range | |
if a == b: | |
print("hi") | |
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 Iterator | |
def fib(n: int) -> Iterator[int]: | |
a, b = 0, 1 | |
while a < n: | |
yield a | |
a, b = b, a + b |
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 contextlib import ExitStack | |
filenames = ("test", "test2") | |
with ExitStack() as stack: | |
for x in filenames: | |
stack.enter_context(open(x)) | |
y = 2 | |
print(y) |
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 Final | |
# Sentinel to denote unset values | |
UNSET: Final[object] = object() | |
def foo(timezone: str | object | None = UNSET) -> None: | |
pass | |
if __name__ == "__main__": | |
foo() |
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
# Sentinel to denote unset values | |
UNSET = object() | |
def foo(timezone: str | object | None = UNSET) -> None: | |
pass | |
if __name__ == "__main__": | |
foo() |
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 TypeVar | |
from typing_extensions import TypeIs | |
_T = TypeVar('_T', str, int) | |
def is_str(val: str | int) -> TypeIs[str]: | |
return isinstance(val, str) | |
NewerOlder