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 Generic | |
| from typing import NamedTuple | |
| from typing import TypeVar | |
| T = TypeVar("T") | |
| class Missing: | |
| def __repr__(self): | |
| return "MISSING" |
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 TypeVar, Literal, Generic, overload, Any, reveal_type, Self, Never | |
| import uuid | |
| _ST = TypeVar("_ST", contravariant=True) | |
| _GT = TypeVar("_GT", covariant=True) | |
| _NT = TypeVar("_NT", Literal[True], Literal[False], default=Literal[False]) | |
| class Model: | |
| 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
| def foo() -> list[bool, int]: | |
| return [False, 0] | |
| def bar() -> list[bool | int]: | |
| return [False, 0] | |
| def baz() -> tuple[bool, int]: | |
| return (False, 0) |
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 TypeVar | |
| T = TypeVar("T") | |
| def create_store(initial: T, fallback: T) -> T: | |
| return initial if initial is not None else fallback | |
| # mypy/pyright NIE zgłosi błędu — zachowuje się podobnie do TS bez NoInfer | |
| store = create_store("hello", 42) # T = str | int, brak błędu |
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
| def checking_tyoe(a): | |
| return a | |
| print(checking_tyoe(12)) |
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
| def what_now(x=1.23): | |
| reveal_type(x) |
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 typing import NamedTuple | |
| class MyBaseClass: ... | |
| class NameBox[T: MyBaseClass]: ... | |
| class InfoHolder[T: MyBaseClass, U: NameBox](NamedTuple): | |
| box_cls: type[U] # This should be `type[U[T]]` | |
| human_name: str |
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 Iterable | |
| def foo(args: Iterable[str | int] | str | int) -> Iterable[str]: | |
| if isinstance(args, (str, int)): | |
| args = (args,) | |
| args = ( | |
| arg if isinstance(arg, str) else str(arg) | |
| for arg in args | |
| ) | |
| return args |
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 enum import Enum | |
| from fractions import Fraction | |
| from math import ceil, floor | |
| RoundingCallType = Callable[[Fraction], int] | |
| def floor_method(number: Fraction) -> int: | |
| return floor(number) |
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 overload, Unpack | |
| @overload | |
| def f(x: tuple[int]) -> int: ... | |
| @overload | |
| def f(x: tuple[int, int, Unpack[tuple[int, ...]]]) -> list[int]: ... | |
| def f(x: tuple[int, ...]) -> int | list[int]: | |
| if len(x) == 1: | |
| return x[0] | |
| return list(x) |
NewerOlder