Update Mypy so I can use PEP 695 type param syntax
This commit is contained in:
parent
3ef3112014
commit
452867699e
10 changed files with 53 additions and 52 deletions
|
|
@ -1,6 +1,6 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Generic, Optional, TypeVar
|
||||
from typing import Any, Optional
|
||||
|
||||
import ormsgpack
|
||||
from aiocache import Cache
|
||||
|
|
@ -8,10 +8,8 @@ from aiocache.serializers import BaseSerializer
|
|||
from pydantic.type_adapter import TypeAdapter
|
||||
from yarl import URL
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
|
||||
class OrmsgpackSerializer(BaseSerializer, Generic[T]):
|
||||
class OrmsgpackSerializer[T](BaseSerializer):
|
||||
DEFAULT_ENCODING = None
|
||||
|
||||
def __init__(self, schema: TypeAdapter[T]):
|
||||
|
|
@ -28,7 +26,7 @@ class OrmsgpackSerializer(BaseSerializer, Generic[T]):
|
|||
return self.schema.validate_python(data)
|
||||
|
||||
|
||||
def make_cache(schema: TypeAdapter[T], url: URL, namespace: str = "") -> Cache[T]:
|
||||
def make_cache[T](schema: TypeAdapter[T], url: URL, namespace: str = "") -> Cache[T]:
|
||||
backend = Cache.get_scheme_class(url.scheme)
|
||||
if backend == Cache.MEMORY:
|
||||
return Cache(backend)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
from collections.abc import Mapping
|
||||
from os import environ
|
||||
from typing import TypeVar
|
||||
|
||||
from boltons.iterutils import remap
|
||||
from pytomlpp import load
|
||||
|
|
@ -9,8 +8,6 @@ from xdg_base_dirs import xdg_config_home
|
|||
from .model import Config
|
||||
|
||||
__all__ = ("loadConfig",)
|
||||
K = TypeVar("K")
|
||||
V = TypeVar("V")
|
||||
|
||||
|
||||
# Sadly this is the kind of function that's incredibly easy to type statically
|
||||
|
|
@ -24,7 +21,7 @@ V = TypeVar("V")
|
|||
# type like NonNullable<T>. Python's type system also doesn't infer a
|
||||
# dictionary literal as having a structural type by default in the way
|
||||
# TypeScript does, of course, so that part wouldn't work anyway.
|
||||
def withoutNones(data: Mapping[K, V | None]) -> Mapping[K, V]:
|
||||
def withoutNones[K, V](data: Mapping[K, V | None]) -> Mapping[K, V]:
|
||||
return remap(data, lambda p, k, v: v is not None)
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,17 +1,15 @@
|
|||
from asyncio import AbstractEventLoop, new_event_loop
|
||||
from dataclasses import dataclass
|
||||
from importlib import import_module
|
||||
from typing import Generic, Iterable, Literal, Protocol, TypeVar, cast
|
||||
from typing import Iterable, Literal, Protocol, cast
|
||||
|
||||
from .config.model import BaseReceiverConfig
|
||||
from .playback import Playback
|
||||
from .player import Player
|
||||
from .tools.types import not_none
|
||||
|
||||
T = TypeVar("T", bound=AbstractEventLoop, covariant=True)
|
||||
|
||||
|
||||
class LoopFactory(Generic[T], Protocol):
|
||||
class LoopFactory[T: AbstractEventLoop](Protocol):
|
||||
@property
|
||||
def is_replaceable(self) -> bool: ...
|
||||
|
||||
|
|
|
|||
|
|
@ -1,10 +1,8 @@
|
|||
from typing import Callable, Protocol, Self, TypeVar
|
||||
from typing import Callable, Protocol, Self
|
||||
|
||||
from pydantic.type_adapter import TypeAdapter
|
||||
from yarl import URL
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
|
||||
class ModelWithSchema(Protocol):
|
||||
@property
|
||||
|
|
@ -13,7 +11,7 @@ class ModelWithSchema(Protocol):
|
|||
def schema(self) -> TypeAdapter[Self]: ...
|
||||
|
||||
|
||||
def schema(schema_id: str) -> Callable[[type[T]], type[T]]:
|
||||
def schema[T](schema_id: str) -> Callable[[type[T]], type[T]]:
|
||||
def decorate(clazz: type[T]) -> type[T]:
|
||||
type.__setattr__(clazz, "id", URL(schema_id))
|
||||
type.__setattr__(clazz, "schema", TypeAdapter(clazz))
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
from collections.abc import Callable
|
||||
from typing import Any, TypeAlias, TypeVar
|
||||
from typing import Any
|
||||
|
||||
__all__ = (
|
||||
"AnyExceptList",
|
||||
|
|
@ -28,27 +28,22 @@ AnyExceptList = (
|
|||
)
|
||||
|
||||
|
||||
U = TypeVar("U")
|
||||
V = TypeVar("V")
|
||||
|
||||
|
||||
def not_none(value: U | None) -> U:
|
||||
def not_none[U](value: U | None) -> U:
|
||||
if value is None:
|
||||
raise ValueError("None should not be possible here.")
|
||||
return value
|
||||
|
||||
|
||||
def option_fmap(f: Callable[[U], V], value: U | None) -> V | None:
|
||||
def option_fmap[U, V](f: Callable[[U], V], value: U | None) -> V | None:
|
||||
if value is None:
|
||||
return None
|
||||
return f(value)
|
||||
|
||||
|
||||
T = TypeVar("T", bound=AnyExceptList)
|
||||
MaybePlural: TypeAlias = list[T] | T
|
||||
type MaybePlural[T: AnyExceptList] = list[T] | T
|
||||
|
||||
|
||||
def un_maybe_plural(value: MaybePlural[T] | None) -> list[T]:
|
||||
def un_maybe_plural[T: AnyExceptList](value: MaybePlural[T] | None) -> list[T]:
|
||||
match value:
|
||||
case None:
|
||||
return []
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue