Skip to content

Commit ec924c4

Browse files
committed
Fix type checker errors
1 parent 1fd9b66 commit ec924c4

File tree

4 files changed

+29
-31
lines changed

4 files changed

+29
-31
lines changed

src/reactpy/core/vdom.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,11 @@ def wrapper(*attributes_and_children: Any) -> VdomDict:
220220

221221
def separate_attributes_and_children(
222222
values: Sequence[Any],
223-
) -> tuple[dict[str, Any], list[Any]]:
223+
) -> tuple[VdomAttributes, list[Any]]:
224224
if not values:
225225
return {}, []
226226

227-
attributes: dict[str, Any]
227+
attributes: VdomAttributes
228228
children_or_iterables: Sequence[Any]
229229
if _is_attributes(values[0]):
230230
attributes, *children_or_iterables = values
@@ -244,8 +244,8 @@ def separate_attributes_and_children(
244244

245245
def separate_attributes_and_event_handlers(
246246
attributes: Mapping[str, Any],
247-
) -> tuple[dict[str, Any], EventHandlerDict]:
248-
separated_attributes = {}
247+
) -> tuple[VdomAttributes, EventHandlerDict]:
248+
separated_attributes: VdomAttributes = {}
249249
separated_event_handlers: dict[str, EventHandlerType] = {}
250250

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

266266
separated_event_handlers[k] = handler
267267

268-
return separated_attributes, dict(separated_event_handlers.items())
268+
return separated_attributes, separated_event_handlers
269269

270270

271271
def _is_attributes(value: Any) -> bool:

src/reactpy/transforms.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from __future__ import annotations
22

3-
from typing import Any
3+
from typing import Any, cast
44

55
from reactpy.core.events import EventHandler, to_event_handler_function
6-
from reactpy.types import VdomDict
6+
from reactpy.types import VdomAttributes, VdomDict
77

88

99
class RequiredTransforms:
@@ -40,10 +40,11 @@ def normalize_style_attributes(self, vdom: VdomDict) -> None:
4040
def html_props_to_reactjs(vdom: VdomDict) -> None:
4141
"""Convert HTML prop names to their ReactJS equivalents."""
4242
if "attributes" in vdom:
43-
vdom["attributes"] = {
44-
REACT_PROP_SUBSTITUTIONS.get(k, k): v
45-
for k, v in vdom["attributes"].items()
46-
}
43+
items = cast(VdomAttributes, vdom["attributes"].items())
44+
vdom["attributes"] = cast(
45+
VdomAttributes,
46+
{REACT_PROP_SUBSTITUTIONS.get(k, k): v for k, v in items},
47+
)
4748

4849
@staticmethod
4950
def textarea_children_to_prop(vdom: VdomDict) -> None:

src/reactpy/types.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22

33
import sys
44
from collections import namedtuple
5-
from collections.abc import Mapping, Sequence
5+
from collections.abc import Awaitable, Mapping, Sequence
66
from dataclasses import dataclass
77
from pathlib import Path
88
from types import TracebackType
99
from typing import (
1010
TYPE_CHECKING,
1111
Any,
12-
Awaitable,
1312
Callable,
1413
Generic,
1514
Literal,
1615
NamedTuple,
16+
NotRequired,
1717
Protocol,
1818
TypeVar,
1919
overload,
@@ -101,6 +101,9 @@ async def __aexit__(
101101
VdomAttributes = TypedDict(
102102
"VdomAttributes",
103103
{
104+
"key": Key,
105+
"value": Any,
106+
"defaultValue": Any,
104107
"dangerouslySetInnerHTML": dict[str, str],
105108
"suppressContentEditableWarning": bool,
106109
"suppressHydrationWarning": bool,
@@ -225,9 +228,7 @@ async def __aexit__(
225228
"onSubmitCapture": EventFunc,
226229
"formAction": str | Callable,
227230
"checked": bool,
228-
"value": str,
229231
"defaultChecked": bool,
230-
"defaultValue": str,
231232
"accept": str,
232233
"alt": str,
233234
"capture": str,
@@ -340,24 +341,20 @@ async def __aexit__(
340341
"onWaitingCapture": EventFunc,
341342
},
342343
total=False,
343-
extra_items=Any,
344+
# TODO: Enable this when Python 3.14 typing extensions are released
345+
# extra_items=Any,
344346
)
345347

346348

347-
class _VdomDictOptional(TypedDict, total=False):
348-
key: Key | None
349-
children: Sequence[ComponentType | VdomChild]
350-
attributes: VdomAttributes
351-
eventHandlers: EventHandlerDict
352-
importSource: ImportSourceDict
353-
349+
class VdomDict(TypedDict):
350+
"""A :ref:`VDOM` dictionary"""
354351

355-
class _VdomDictRequired(TypedDict, total=True):
356352
tagName: str
357-
358-
359-
class VdomDict(_VdomDictRequired, _VdomDictOptional):
360-
"""A :ref:`VDOM` dictionary"""
353+
key: NotRequired[Key | None]
354+
children: NotRequired[Sequence[ComponentType | VdomChild]]
355+
attributes: NotRequired[VdomAttributes]
356+
eventHandlers: NotRequired[EventHandlerDict]
357+
importSource: NotRequired[ImportSourceDict]
361358

362359

363360
VdomChild: TypeAlias = "ComponentType | VdomDict | str | None | Any"
@@ -378,7 +375,7 @@ class _OptionalVdomJson(TypedDict, total=False):
378375
key: Key
379376
error: str
380377
children: list[Any]
381-
attributes: dict[str, Any]
378+
attributes: VdomAttributes
382379
eventHandlers: dict[str, _JsonEventTarget]
383380
importSource: _JsonImportSource
384381

src/reactpy/widgets.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
import reactpy
88
from reactpy._html import html
99
from reactpy._warnings import warn
10-
from reactpy.types import ComponentConstructor, VdomDict
10+
from reactpy.types import ComponentConstructor, VdomAttributes, VdomDict
1111

1212

1313
def image(
1414
format: str,
1515
value: str | bytes = "",
16-
attributes: dict[str, Any] | None = None,
16+
attributes: VdomAttributes | None = None,
1717
) -> VdomDict:
1818
"""Utility for constructing an image from a string or bytes
1919

0 commit comments

Comments
 (0)