Skip to content

Commit 7349b47

Browse files
authored
Merge pull request #52 from xpodev/dev/v0.8.6
Fix typings
2 parents 1cb9b3c + 8237620 commit 7349b47

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

136 files changed

+2815
-1278
lines changed

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
prune tests

_auto_html.py

Lines changed: 264 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
1+
import pathlib
2+
3+
HERE = pathlib.Path(__file__).parent
4+
HTML_FILES = HERE / "seamless/html"
5+
6+
# with open(HERE / "__init__.py", "w") as f:
7+
# items = []
8+
# for file in HERE.iterdir():
9+
# if file.name.startswith("_") or file.suffix != ".py":
10+
# continue
11+
12+
# capitalized = file.stem.capitalize()
13+
# items.append(capitalized)
14+
# print(f"from .{file.stem} import {capitalized}", file=f)
15+
16+
# all = "\n".join(f'\t"{item}",' for item in items)
17+
# print(f"\n__all__ = [\n{all}\n]", file=f)
18+
19+
20+
html_map = {
21+
"A": "HTMLAnchorElement",
22+
"Area": "HTMLAreaElement",
23+
"Audio": "HTMLAudioElement",
24+
"Br": "HTMLBRElement",
25+
"Base": "HTMLBaseElement",
26+
"Body": "HTMLBodyElement",
27+
"Button": "HTMLButtonElement",
28+
"Canvas": "HTMLCanvasElement",
29+
"Caption": "HTMLTableCaptionElement",
30+
"Col": "HTMLTableColElement",
31+
"ColGroup": "HTMLTableColElement",
32+
"Data": "HTMLDataElement",
33+
"DataList": "HTMLDataListElement",
34+
"Del": "HTMLModElement",
35+
"Details": "HTMLDetailsElement",
36+
"Dialog": "HTMLDialogElement",
37+
"Div": "HTMLDivElement",
38+
"Embed": "HTMLEmbedElement",
39+
"FieldSet": "HTMLFieldSetElement",
40+
"Form": "HTMLFormElement",
41+
"H1": "HTMLHeadingElement",
42+
"H2": "HTMLHeadingElement",
43+
"H3": "HTMLHeadingElement",
44+
"H4": "HTMLHeadingElement",
45+
"H5": "HTMLHeadingElement",
46+
"H6": "HTMLHeadingElement",
47+
"Hr": "HTMLHRElement",
48+
"Head": "HTMLHeadElement",
49+
"Heading": "HTMLHeadingElement",
50+
"Html": "HTMLHtmlElement",
51+
"IFrame": "HTMLIFrameElement",
52+
"Image": "HTMLImageElement",
53+
"Input": "HTMLInputElement",
54+
"Ins": "HTMLModElement",
55+
"Li": "HTMLListItemElement",
56+
"Label": "HTMLLabelElement",
57+
"Legend": "HTMLLegendElement",
58+
"Link": "HTMLLinkElement",
59+
"Map": "HTMLMapElement",
60+
"Meter": "HTMLMeterElement",
61+
"Object": "HTMLObjectElement",
62+
"Ol": "HTMLOrderedListElement",
63+
"OptGroup": "HTMLOptGroupElement",
64+
"Option": "HTMLOptionElement",
65+
"Output": "HTMLOutputElement",
66+
"P": "HTMLParagraphElement",
67+
"Param": "HTMLParamElement",
68+
"Picture": "HTMLPictureElement",
69+
"Pre": "HTMLPreElement",
70+
"Progress": "HTMLProgressElement",
71+
"Q": "HTMLQuoteElement",
72+
"Script": "HTMLScriptElement",
73+
"Select": "HTMLSelectElement",
74+
"Slot": "HTMLSlotElement",
75+
"Source": "HTMLSourceElement",
76+
"Span": "HTMLSpanElement",
77+
"Style": "HTMLStyleElement",
78+
"Table": "HTMLTableElement",
79+
"TBody": "HTMLTableSectionElement",
80+
"Td": "HTMLTableDataCellElement",
81+
"TFoot": "HTMLTableSectionElement",
82+
"Th": "HTMLTableHeaderCellElement",
83+
"THead": "HTMLTableSectionElement",
84+
"Tr": "HTMLTableRowElement",
85+
"Template": "HTMLTemplateElement",
86+
"TextArea": "HTMLTextAreaElement",
87+
"Time": "HTMLTimeElement",
88+
"Title": "HTMLTitleElement",
89+
"Track": "HTMLTrackElement",
90+
"Ul": "HTMLUnorderedListElement",
91+
"Video": "HTMLVideoElement",
92+
}
93+
94+
html_classes = [
95+
"A",
96+
"Abbr",
97+
"Address",
98+
"Area",
99+
"Article",
100+
"Aside",
101+
"Audio",
102+
"B",
103+
"Base",
104+
"Bdi",
105+
"Bdo",
106+
"BlockQuote",
107+
"Body",
108+
"Br",
109+
"Button",
110+
"Canvas",
111+
"Caption",
112+
"Cite",
113+
"Code",
114+
"Col",
115+
"ColGroup",
116+
"Data",
117+
"DataList",
118+
"Dd",
119+
"Del",
120+
"Details",
121+
"Dfn",
122+
"Dialog",
123+
"Div",
124+
"Dl",
125+
"Dt",
126+
"Em",
127+
"Embed",
128+
"FieldSet",
129+
"FigCaption",
130+
"Figure",
131+
"Footer",
132+
"Form",
133+
"H1",
134+
"H2",
135+
"H3",
136+
"H4",
137+
"H5",
138+
"H6",
139+
"Head",
140+
"Header",
141+
"HGroup",
142+
"Hr",
143+
"Html",
144+
"I",
145+
"IFrame",
146+
"Img",
147+
"Input",
148+
"Ins",
149+
"Kbd",
150+
"Label",
151+
"Legend",
152+
"Li",
153+
"Link",
154+
"Main",
155+
"Map",
156+
"Mark",
157+
"Menu",
158+
"Meta",
159+
"Meter",
160+
"Nav",
161+
"NoScript",
162+
"Object",
163+
"Ol",
164+
"OptGroup",
165+
"Option",
166+
"Output",
167+
"P",
168+
"Param",
169+
"Picture",
170+
"Pre",
171+
"Progress",
172+
"Q",
173+
"Rp",
174+
"Rt",
175+
"Ruby",
176+
"S",
177+
"Samp",
178+
"Script",
179+
"Search",
180+
"Section",
181+
"Select",
182+
"Slot",
183+
"Small",
184+
"Source",
185+
"Span",
186+
"Strong",
187+
"Style",
188+
"Sub",
189+
"Summary",
190+
"Sup",
191+
"Svg",
192+
"Table",
193+
"TBody",
194+
"Td",
195+
"Template",
196+
"TextArea",
197+
"TFoot",
198+
"Th",
199+
"THead",
200+
"Time",
201+
"Title",
202+
"Tr",
203+
"Track",
204+
"U",
205+
"Ul",
206+
"Var",
207+
"Video",
208+
"Wbr",
209+
]
210+
211+
inline_html_classes = [
212+
"Area",
213+
"Base",
214+
"Br",
215+
"Col",
216+
"Embed",
217+
"Hr",
218+
"Img",
219+
"Input",
220+
"Link",
221+
"Meta",
222+
"Param",
223+
"Source",
224+
"Track",
225+
"Wbr",
226+
]
227+
228+
html_map = {key: html_map.get(key, "HTMLElementProps") for key in html_classes}
229+
230+
template = """from typing import TYPE_CHECKING, Unpack
231+
from ..element import Element
232+
233+
if TYPE_CHECKING:
234+
from ..types.html import {props_class_name}
235+
from ..types import ChildType
236+
237+
class {class_name}(Element):
238+
def __init__(self, *children: "ChildType", **kwargs: Unpack["{props_class_name}"]):
239+
super().__init__(*children, **kwargs)
240+
241+
tag_name = "{class_name_lower}"{inline}
242+
"""
243+
244+
inline_string = "\n inline = True"
245+
246+
lower_keys = {key.lower(): key for key in html_map}
247+
248+
for cls, props in html_map.items():
249+
class_name = cls
250+
class_name_lower = class_name.lower()
251+
props_class_name = props
252+
filename = f"{class_name_lower}.py"
253+
inline = inline_string if class_name in inline_html_classes else ""
254+
255+
if class_name == "Meta":
256+
continue
257+
258+
if class_name == "Del":
259+
filename = "del_.py"
260+
261+
with open(HTML_FILES / filename, "w") as f:
262+
f.write(template.format(class_name=class_name, class_name_lower=class_name_lower, props_class_name=props_class_name, inline=inline))
263+
264+

pyproject.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,17 @@ dynamic = ["dependencies", "version"]
1717
Homepage = "https://github.com/xpodev/seamless"
1818
Issues = "https://github.com/xpodev/seamless/issues"
1919

20-
[tool.setuptools]
21-
include-package-data = true
20+
[build-system]
21+
requires = ["setuptools", "wheel"]
22+
build-backend = "setuptools.build_meta"
2223

2324
[tool.setuptools.packages.find]
25+
where = ["."]
2426
include = ["seamless*"]
2527
exclude = ["tests*"]
2628

2729
[tool.setuptools.package-data]
2830
seamless = ["**/*.js"]
2931

3032
[tool.setuptools.dynamic]
31-
dependencies = {file = ["requirements.txt"]}
3233
version = {attr="seamless.__version__"}

seamless/components/base.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
from abc import abstractmethod
2-
from typing import TYPE_CHECKING, Final
2+
from typing import TYPE_CHECKING, ClassVar
33

44

55
if TYPE_CHECKING:
6-
from seamless.types import RenderResult, ChildrenType
6+
from seamless.types import RenderResult, ChildType
77

88

99
class Component:
10-
children: Final[tuple["ChildrenType", ...]]
11-
__seamless_name__: Final[str] = "Component"
10+
children: tuple["ChildType", ...]
11+
__seamless_name__: ClassVar[str] = "Component"
1212

13-
def __init__(self, *children: "ChildrenType") -> None:
13+
def __init__(self, *children: "ChildType") -> None:
1414
if type(self) is Component:
1515
raise TypeError("Cannot instantiate Component directly")
1616

@@ -20,7 +20,7 @@ def __init__(self, *children: "ChildrenType") -> None:
2020
def render(self) -> "RenderResult":
2121
raise NotImplementedError(f"{type(self).__name__}.render() is not implemented")
2222

23-
def __init_subclass__(cls, *, name: str = None, **kwargs) -> None:
23+
def __init_subclass__(cls, *, name: str | None = None, **kwargs) -> None:
2424
super().__init_subclass__(**kwargs)
2525

2626
if cls is not Component:
@@ -40,6 +40,6 @@ def __init__(self, *args, children=None, **kwargs):
4040

4141
cls.__init__ = __init__
4242

43-
def __call__(self, *children: "ChildrenType"):
43+
def __call__(self, *children: "ChildType"):
4444
self.children = children
4545
return self

seamless/components/page.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Iterable, Literal, TypedDict, overload
22

3-
from ..types import Primitive, Renderable
3+
from ..types import Primitive, Renderable, ChildrenType, ChildType
44

55
from .base import Component
66
from ..html import (
@@ -27,10 +27,10 @@ class _BodyProps(TypedDict):
2727

2828
class Page(Component, name="SeamlessBasePage"):
2929
@overload
30-
def __init__(self, *children, title: str = None, html_props: _HtmlProps = None, head_props: _HeadProps = None, body_props: _BodyProps = None): ...
30+
def __init__(self, *children: ChildType, title: str | None = None, html_props: _HtmlProps | None = None, head_props: _HeadProps | None = None, body_props: _BodyProps | None = None): ...
3131
@overload
32-
def __init__(self, *, title: str = None, html_props: _HtmlProps = None, head_props: _HeadProps = None, body_props: _BodyProps = None): ...
33-
def __init__(
32+
def __init__(self, *, children: ChildrenType, title: str | None = None, html_props: _HtmlProps | None = None, head_props: _HeadProps | None = None, body_props: _BodyProps | None = None): ...
33+
def __init__( # type: ignore
3434
self,
3535
*,
3636
title: str | None = None,
@@ -68,7 +68,7 @@ def render(self):
6868
),
6969
)
7070

71-
def __init_subclass__(cls, title: str = None, **kwargs) -> None:
71+
def __init_subclass__(cls, title: str | None = None, **kwargs) -> None:
7272
super().__init_subclass__(**kwargs)
7373

7474
if title is None:

seamless/components/router/route.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ def __init__(self, *, path, component: type[Component]):
77
self.component = component
88

99
def render(self):
10-
return self.component.render()
10+
return None

seamless/components/router/router.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313

1414

1515
class Router(Component):
16-
children: list[Route]
16+
children: tuple[Route, ...] # type: ignore
1717

1818
@overload
19-
def __init__(self, *, loading_component: Renderable = None): ...
19+
def __init__(self, *, loading_component: type[Component] | None = None): ...
2020
@overload
21-
def __init__(self, *routes: Route, loading_component: Renderable = None): ...
22-
def __init__(self, *, loading_component: Renderable | None = None):
21+
def __init__(self, *routes: Route, loading_component: type[Component] | None = None): ...
22+
23+
def __init__(self, *, loading_component: type[Component] | None = None): # type: ignore
2324
self.loading_component = loading_component.__seamless_name__ if loading_component else None
2425
for route in self.children:
2526
if not isinstance(route, Route):

seamless/context/components.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class ComponentsRepository:
88
def __init__(self):
99
self.components = {}
1010

11-
def add_component(self, component: "Component", name: str):
11+
def add_component(self, component: type["Component"], name: str):
1212
self.components[name] = component
1313

1414
def get_component(self, component_name: str) -> "Component":

0 commit comments

Comments
 (0)