Skip to content
Merged
Prev Previous commit
Next Next commit
Fix type checker errors
  • Loading branch information
Archmonger committed Feb 14, 2025
commit ec924c4b7b07d5acf8d352d44acdaaf0a6f7913b
10 changes: 5 additions & 5 deletions src/reactpy/core/vdom.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,11 @@ def wrapper(*attributes_and_children: Any) -> VdomDict:

def separate_attributes_and_children(
values: Sequence[Any],
) -> tuple[dict[str, Any], list[Any]]:
) -> tuple[VdomAttributes, list[Any]]:
if not values:
return {}, []

attributes: dict[str, Any]
attributes: VdomAttributes
children_or_iterables: Sequence[Any]
if _is_attributes(values[0]):
attributes, *children_or_iterables = values
Expand All @@ -244,8 +244,8 @@ def separate_attributes_and_children(

def separate_attributes_and_event_handlers(
attributes: Mapping[str, Any],
) -> tuple[dict[str, Any], EventHandlerDict]:
separated_attributes = {}
) -> tuple[VdomAttributes, EventHandlerDict]:
separated_attributes: VdomAttributes = {}
separated_event_handlers: dict[str, EventHandlerType] = {}

for k, v in attributes.items():
Expand All @@ -265,7 +265,7 @@ def separate_attributes_and_event_handlers(

separated_event_handlers[k] = handler

return separated_attributes, dict(separated_event_handlers.items())
return separated_attributes, separated_event_handlers


def _is_attributes(value: Any) -> bool:
Expand Down
13 changes: 7 additions & 6 deletions src/reactpy/transforms.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from __future__ import annotations

from typing import Any
from typing import Any, cast

from reactpy.core.events import EventHandler, to_event_handler_function
from reactpy.types import VdomDict
from reactpy.types import VdomAttributes, VdomDict


class RequiredTransforms:
Expand Down Expand Up @@ -40,10 +40,11 @@ def normalize_style_attributes(self, vdom: VdomDict) -> None:
def html_props_to_reactjs(vdom: VdomDict) -> None:
"""Convert HTML prop names to their ReactJS equivalents."""
if "attributes" in vdom:
vdom["attributes"] = {
REACT_PROP_SUBSTITUTIONS.get(k, k): v
for k, v in vdom["attributes"].items()
}
items = cast(VdomAttributes, vdom["attributes"].items())
vdom["attributes"] = cast(
VdomAttributes,
{REACT_PROP_SUBSTITUTIONS.get(k, k): v for k, v in items},
)

@staticmethod
def textarea_children_to_prop(vdom: VdomDict) -> None:
Expand Down
33 changes: 15 additions & 18 deletions src/reactpy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

import sys
from collections import namedtuple
from collections.abc import Mapping, Sequence
from collections.abc import Awaitable, Mapping, Sequence
from dataclasses import dataclass
from pathlib import Path
from types import TracebackType
from typing import (
TYPE_CHECKING,
Any,
Awaitable,
Callable,
Generic,
Literal,
NamedTuple,
NotRequired,
Protocol,
TypeVar,
overload,
Expand Down Expand Up @@ -101,6 +101,9 @@ async def __aexit__(
VdomAttributes = TypedDict(
"VdomAttributes",
{
"key": Key,
"value": Any,
"defaultValue": Any,
"dangerouslySetInnerHTML": dict[str, str],
"suppressContentEditableWarning": bool,
"suppressHydrationWarning": bool,
Expand Down Expand Up @@ -225,9 +228,7 @@ async def __aexit__(
"onSubmitCapture": EventFunc,
"formAction": str | Callable,
"checked": bool,
"value": str,
"defaultChecked": bool,
"defaultValue": str,
"accept": str,
"alt": str,
"capture": str,
Expand Down Expand Up @@ -340,24 +341,20 @@ async def __aexit__(
"onWaitingCapture": EventFunc,
},
total=False,
extra_items=Any,
# TODO: Enable this when Python 3.14 typing extensions are released
# extra_items=Any,
)


class _VdomDictOptional(TypedDict, total=False):
key: Key | None
children: Sequence[ComponentType | VdomChild]
attributes: VdomAttributes
eventHandlers: EventHandlerDict
importSource: ImportSourceDict

class VdomDict(TypedDict):
"""A :ref:`VDOM` dictionary"""

class _VdomDictRequired(TypedDict, total=True):
tagName: str


class VdomDict(_VdomDictRequired, _VdomDictOptional):
"""A :ref:`VDOM` dictionary"""
key: NotRequired[Key | None]
children: NotRequired[Sequence[ComponentType | VdomChild]]
attributes: NotRequired[VdomAttributes]
eventHandlers: NotRequired[EventHandlerDict]
importSource: NotRequired[ImportSourceDict]


VdomChild: TypeAlias = "ComponentType | VdomDict | str | None | Any"
Expand All @@ -378,7 +375,7 @@ class _OptionalVdomJson(TypedDict, total=False):
key: Key
error: str
children: list[Any]
attributes: dict[str, Any]
attributes: VdomAttributes
eventHandlers: dict[str, _JsonEventTarget]
importSource: _JsonImportSource

Expand Down
4 changes: 2 additions & 2 deletions src/reactpy/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
import reactpy
from reactpy._html import html
from reactpy._warnings import warn
from reactpy.types import ComponentConstructor, VdomDict
from reactpy.types import ComponentConstructor, VdomAttributes, VdomDict


def image(
format: str,
value: str | bytes = "",
attributes: dict[str, Any] | None = None,
attributes: VdomAttributes | None = None,
) -> VdomDict:
"""Utility for constructing an image from a string or bytes

Expand Down