Skip to content

Commit aa26882

Browse files
committed
misc fixes
1 parent dcc98ec commit aa26882

File tree

9 files changed

+70
-51
lines changed

9 files changed

+70
-51
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ per-file-ignores = [
5151
"docs/*/_examples/*.py:E402",
5252
]
5353
max-line-length = 88
54-
max-complexity = 18
54+
max-complexity = 20
5555
select = ["B", "C", "E", "F", "W", "T4", "B9", "N", "ROH"]
5656
exclude = ["**/node_modules/*", ".eggs/*", ".tox/*"]
5757
# -- flake8-tidy-imports --

src/idom/__main__.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import click
22

3-
from idom import __version__
3+
import idom
44
from idom._console.update_html_usages import update_html_usages
55

66

7-
app = click.Group(
8-
commands=[
9-
update_html_usages,
10-
]
11-
)
7+
@click.group()
8+
@click.version_option(idom.__version__, prog_name=idom.__name__)
9+
def app() -> None:
10+
pass
11+
12+
13+
app.add_command(update_html_usages)
14+
1215

1316
if __name__ == "__main__":
1417
app()

src/idom/_console/update_html_usages.py

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from typing import Iterator
1414

1515
import click
16-
from typing_extensions import TypeGuard
1716

1817
from idom import html
1918

@@ -61,42 +60,47 @@ def update_html_usages(directories: list[str]) -> None:
6160
file.write_text(result)
6261

6362

64-
def generate_rewrite(file: Path, source: str) -> None:
63+
def generate_rewrite(file: Path, source: str) -> str | None:
6564
tree = ast.parse(source)
6665

6766
changed: list[Sequence[ast.AST]] = []
6867
for parents, node in walk_with_parent(tree):
69-
if isinstance(node, ast.Call) and node.args:
70-
func = node.func
71-
if isinstance(func, ast.Attribute):
72-
name = func.attr
73-
elif isinstance(func, ast.Name):
74-
name = func.id
75-
else:
76-
continue
77-
if hasattr(html, name) or name == "vdom":
78-
if name == "vdom":
79-
maybe_attr_dict_node = node.args[1]
80-
# remove attr dict from new args
81-
new_args = node.args[:1] + node.args[2:]
68+
if not (isinstance(node, ast.Call) and node.args):
69+
continue
70+
71+
func = node.func
72+
if isinstance(func, ast.Attribute):
73+
name = func.attr
74+
elif isinstance(func, ast.Name):
75+
name = func.id
76+
else:
77+
continue
78+
79+
if not (hasattr(html, name) or name == "vdom"):
80+
continue
81+
82+
if name == "vdom":
83+
maybe_attr_dict_node = node.args[1]
84+
# remove attr dict from new args
85+
new_args = node.args[:1] + node.args[2:]
86+
else:
87+
maybe_attr_dict_node = node.args[0]
88+
# remove attr dict from new args
89+
new_args = node.args[1:]
90+
91+
if node.args:
92+
new_keyword_info = extract_keywords(maybe_attr_dict_node)
93+
if new_keyword_info is not None:
94+
if new_keyword_info.replace:
95+
node.keywords = new_keyword_info.keywords
8296
else:
83-
maybe_attr_dict_node = node.args[0]
84-
# remove attr dict from new args
85-
new_args = node.args[1:]
86-
87-
if node.args:
88-
new_keyword_info = extract_keywords(maybe_attr_dict_node)
89-
if new_keyword_info is not None:
90-
if new_keyword_info.replace:
91-
node.keywords = new_keyword_info.keywords
92-
else:
93-
node.keywords.extend(new_keyword_info.keywords)
97+
node.keywords.extend(new_keyword_info.keywords)
9498

95-
node.args = new_args
96-
changed.append((node, *parents))
99+
node.args = new_args
100+
changed.append((node, *parents))
97101

98102
if not changed:
99-
return
103+
return None
100104

101105
ast.fix_missing_locations(tree)
102106

@@ -124,7 +128,10 @@ def generate_rewrite(file: Path, source: str) -> None:
124128
)
125129
outermost_nodes_to_unparse = [current_outermost_node]
126130
for node in sorted_nodes_to_unparse:
127-
if node.lineno > current_outermost_node.end_lineno:
131+
if (
132+
not current_outermost_node.end_lineno
133+
or node.lineno > current_outermost_node.end_lineno
134+
):
128135
current_outermost_node = node
129136
outermost_nodes_to_unparse.append(node)
130137

@@ -192,6 +199,7 @@ def extract_keywords(node: ast.AST) -> KeywordInfo | None:
192199
else:
193200
keywords.append(kw)
194201
return KeywordInfo(replace=False, keywords=keywords)
202+
return None
195203

196204

197205
def find_comments(lines: list[str]) -> list[str]:
@@ -220,4 +228,4 @@ def conv_attr_name(name: str) -> str:
220228
@dataclass
221229
class KeywordInfo:
222230
replace: bool
223-
keywords: Sequence[ast.keyword]
231+
keywords: list[ast.keyword]

src/idom/_option.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
import os
44
from logging import getLogger
5-
from typing import Any, Callable, Generic, Iterator, TypeVar, cast
6-
from warnings import warn
5+
from typing import Any, Callable, Generic, TypeVar, cast
76

87
from idom._warnings import warn
98

src/idom/_warnings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
@wraps(_warn)
99
def warn(*args: Any, **kwargs: Any) -> Any:
1010
# warn at call site outside of IDOM
11-
_warn(*args, stacklevel=_frame_depth_in_module() + 1, **kwargs)
11+
_warn(*args, stacklevel=_frame_depth_in_module() + 1, **kwargs) # type: ignore
1212

1313

1414
def _frame_depth_in_module() -> int:

src/idom/core/types.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import sys
44
from collections import namedtuple
5-
from collections.abc import Sequence
65
from types import TracebackType
76
from typing import (
87
TYPE_CHECKING,
@@ -15,6 +14,7 @@
1514
Mapping,
1615
NamedTuple,
1716
Optional,
17+
Sequence,
1818
Type,
1919
TypeVar,
2020
Union,
@@ -101,7 +101,7 @@ async def __aexit__(
101101
VdomChild = Union[ComponentType, "VdomDict", str]
102102
"""A single child element of a :class:`VdomDict`"""
103103

104-
VdomChildren = "Sequence[VdomChild]"
104+
VdomChildren = Sequence[VdomChild]
105105
"""Describes a series of :class:`VdomChild` elements"""
106106

107107
VdomAttributesAndChildren = Union[
@@ -211,7 +211,7 @@ class VdomDictConstructor(Protocol):
211211

212212
def __call__(
213213
self,
214-
*children: VdomChild,
214+
*children: VdomChild | VdomChildren,
215215
key: Key | None = None,
216216
**attributes: Any,
217217
) -> VdomDict:

src/idom/core/vdom.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from typing import Any, DefaultDict, Mapping, cast
55

66
from fastjsonschema import compile as compile_json_schema
7+
from typing_extensions import TypeGuard
78

89
from idom._warnings import warn
910
from idom.config import IDOM_DEBUG_MODE
@@ -19,6 +20,7 @@
1920
ImportSourceDict,
2021
Key,
2122
VdomChild,
23+
VdomChildren,
2224
VdomDict,
2325
VdomDictConstructor,
2426
VdomJson,
@@ -130,7 +132,10 @@ def is_vdom(value: Any) -> bool:
130132

131133

132134
def vdom(
133-
tag: str, *children: VdomChild, key: Key | None = None, **attributes: Any
135+
tag: str,
136+
*children: VdomChild | VdomChildren,
137+
key: Key | None = None,
138+
**attributes: Any,
134139
) -> VdomDict:
135140
"""A helper function for creating VDOM elements.
136141
@@ -179,7 +184,10 @@ def vdom(
179184
if _is_single_child(child):
180185
flattened_children.append(child)
181186
else:
182-
flattened_children.extend(child)
187+
# FIXME: Types do not narrow in negative case of TypeGaurd
188+
# This cannot be fixed until there is some sort of "StrictTypeGuard".
189+
# See: https://github.com/python/typing/discussions/1013
190+
flattened_children.extend(child) # type: ignore
183191

184192
attributes, event_handlers = separate_attributes_and_event_handlers(attributes)
185193

@@ -199,7 +207,7 @@ def vdom(
199207

200208

201209
def with_import_source(element: VdomDict, import_source: ImportSourceDict) -> VdomDict:
202-
return {**element, "importSource": import_source}
210+
return {**element, "importSource": import_source} # type: ignore
203211

204212

205213
def make_vdom_constructor(
@@ -214,7 +222,9 @@ def make_vdom_constructor(
214222
"""
215223

216224
def constructor(
217-
*children: VdomChild, key: Key | None = None, **attributes: Any
225+
*children: VdomChild | VdomChildren,
226+
key: Key | None = None,
227+
**attributes: Any,
218228
) -> VdomDict:
219229
if not allow_children and children:
220230
raise TypeError(f"{tag!r} nodes cannot have children.")
@@ -273,7 +283,7 @@ def separate_attributes_and_event_handlers(
273283
return separated_attributes, flat_event_handlers_dict
274284

275285

276-
def _is_single_child(value: Any) -> bool:
286+
def _is_single_child(value: Any) -> TypeGuard[VdomChild]:
277287
if isinstance(value, (str, Mapping)) or not hasattr(value, "__iter__"):
278288
return True
279289
if IDOM_DEBUG_MODE.current:

src/idom/html.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@
157157

158158
from __future__ import annotations
159159

160-
from typing import Any, Mapping
160+
from typing import Any
161161

162162
from idom.core.types import Key, VdomDict
163163
from idom.core.vdom import make_vdom_constructor, vdom

src/idom/utils.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
import re
43
from itertools import chain
54
from typing import Any, Callable, Generic, Iterable, TypeVar, cast
65

0 commit comments

Comments
 (0)