From 197c6b43d7fa97e18a33d0802b3ccad3cdf37d7a Mon Sep 17 00:00:00 2001 From: Masahiro Wada Date: Thu, 28 Apr 2022 14:53:08 +0000 Subject: [PATCH] chore: Simplify type hints --- alfort/app.py | 10 +++++----- alfort/vdom.py | 19 +++++-------------- tests/test_app.py | 4 ++-- 3 files changed, 12 insertions(+), 21 deletions(-) diff --git a/alfort/app.py b/alfort/app.py index a96f68d..8904ae1 100644 --- a/alfort/app.py +++ b/alfort/app.py @@ -1,7 +1,7 @@ from abc import abstractmethod from dataclasses import dataclass, replace from itertools import zip_longest -from typing import Callable, Generic, List, Optional, Tuple, TypeAlias, TypeVar, Union +from typing import Callable, Generic, TypeAlias, TypeVar from alfort.vdom import ( Element, @@ -23,9 +23,9 @@ Dispatch: TypeAlias = Callable[[M], None] Effect: TypeAlias = Callable[[Dispatch[M]], None] -View: TypeAlias = Callable[[S], Optional[VDom]] -Update: TypeAlias = Callable[[M, S], Tuple[S, List[Effect[M]]]] -Init: TypeAlias = Callable[[], Tuple[S, List[Effect[M]]]] +View: TypeAlias = Callable[[S], VDom | None] +Update: TypeAlias = Callable[[M, S], tuple[S, list[Effect[M]]]] +Init: TypeAlias = Callable[[], tuple[S, list[Effect[M]]]] Mount: TypeAlias = Callable[[N], None] @@ -40,7 +40,7 @@ class NodeDomText: node: Node -NodeDom = Union[NodeDomElement, NodeDomText] +NodeDom = NodeDomElement | NodeDomText class Alfort(Generic[S, M, N]): diff --git a/alfort/vdom.py b/alfort/vdom.py index 9f10d37..63e8c0a 100644 --- a/alfort/vdom.py +++ b/alfort/vdom.py @@ -1,21 +1,12 @@ from dataclasses import dataclass -from typing import ( - Any, - Generic, - List, - MutableMapping, - Protocol, - TypeAlias, - TypeVar, - Union, -) +from typing import Any, Generic, MutableMapping, Protocol, TypeAlias, TypeVar T = TypeVar("T") @dataclass(slots=True, frozen=True) class PatchProps: - remove_keys: List[str] + remove_keys: list[str] add_props: "Props" @@ -35,7 +26,7 @@ class PatchText: value: str -Patch = Union[PatchProps, PatchInsertChild, PatchRemoveChild, PatchText] +Patch = PatchProps | PatchInsertChild | PatchRemoveChild | PatchText class Node(Protocol): @@ -47,7 +38,7 @@ def apply(self, patch: Patch) -> None: class Element(Generic[T]): tag: str props: "Props" - children: List[T] + children: list[T] @dataclass(slots=True, frozen=True) @@ -55,7 +46,7 @@ class VDomElement(Element["VDom"]): ... -VDom = Union[VDomElement, str] +VDom = VDomElement | str Props: TypeAlias = MutableMapping[str, Any] diff --git a/tests/test_app.py b/tests/test_app.py index 497c211..f1cf34d 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -1,5 +1,5 @@ from dataclasses import dataclass -from typing import Any, Union +from typing import Any import pytest @@ -175,7 +175,7 @@ class CountDown: value: int = 1 -Msg = Union[CountUp, CountDown] +Msg = CountUp | CountDown class TextNode(Node):