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 enum import StrEnum | |
from collections.abc import Set | |
class E(StrEnum): | |
ABC = "ABC" | |
XYZ = "XYZ" | |
def handle(values: Set[E]) -> None: | |
print(values) |
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 functools import wraps | |
def deco(func): | |
@wraps(func) | |
async def wrapper(*args, **kwargs): | |
return await func(*args, **kwargs) | |
return wrapper | |
@deco |
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, Generic | |
class GameNode(Generic['GameNodeType']): | |
pass | |
GameNodeType = TypeVar('GameNodeType', bound=GameNode, default=GameNode) |
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
address = { | |
"address1": data.get("address1"), | |
"address2": data.get("address2"), | |
"address3": data.get("address3"), | |
"town": data.get("town"), | |
"postcode": norm_postcode(data.get("postcode")), | |
} | |
if not any(address.values()): | |
address = 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 * | |
def run_func(func: Callable[..., Any], *args: Any, print_hi: bool = False, **kwargs: Any) -> Any: | |
if print_hi: | |
print("hi") | |
return func(*args, **kwargs) | |
def foo[**P, R](func: Callable[P, R], *args: P.args, **kwargs: P.kwargs) -> R: | |
return run_func(func, *args, **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
# https://github.com/python/mypy/issues/18481 | |
def spam(text: str, dry: bool) -> None: | |
print(text, dry) | |
spam(**{'text': 'spam', 'dry': True}) | |
# main.py:4: error: Argument 1 to "spam" has incompatible type "**dict[str, object]"; expected "str" [arg-type] | |
# main.py:4: error: Argument 1 to "spam" has incompatible type "**dict[str, object]"; expected "bool" [arg-type] |
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 spam(text: str, dry: bool) -> None: | |
print(text, dry) | |
spam(**{'text': 'spam', 'dry': True}) |
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 Any | |
class Array[float, *ShapeTs]: ... | |
type Float2D = Array[float, *tuple[int, int]] # or Array[float, int, int] | |
type FloatND = Array[float, *tuple[Any, ...]] | |
def from_2d(a: Float2D) -> FloatND: | |
return a # mypy: ✅, pyright: ✅ |
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 Any | |
class Array[*ShapeTs](tuple[*ShapeTs]): ... | |
type Float2D = Array[*tuple[int, int]] # or Array[int, int] | |
type FloatND = Array[*tuple[Any, ...]] | |
def from_2d(a: Float2D) -> FloatND: | |
return a # mypy: ✅, pyright: ✅ |
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 Any | |
class Array[DataT, *ShapeTs](tuple[*ShapeTs]): ... | |
type Float2D = Array[float, *tuple[int, int]] # or Array[float, int, int] | |
type FloatND = Array[float, *tuple[Any, ...]] | |
def from_2d(a: Float2D) -> FloatND: | |
return a # mypy: ✅, pyright: ✅ |
NewerOlder