Skip to content

Commit 0e507b4

Browse files
committed
fix mypy
1 parent 1c0deac commit 0e507b4

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

idom/client/build_config.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,18 @@ def find_python_packages_build_config_items(
125125
Returns:
126126
Mapping of module names to their corresponding list of discovered dependencies.
127127
"""
128-
failures: List[Tuple[str, Exception]] = []
128+
failures: List[Exception] = []
129129
build_configs: List[ConfigItem] = []
130130
for module_info in iter_modules(paths):
131131
module_name = module_info.name
132132
module_loader = module_info.module_finder.find_module(module_name)
133133
if isinstance(module_loader, SourceFileLoader):
134134
try:
135+
module_filename = module_loader.get_filename(module_name)
136+
if isinstance(module_filename, bytes): # pragma: no cover
137+
module_filename = module_filename.decode()
135138
conf = find_build_config_item_in_python_file(
136-
module_name, Path(module_loader.get_filename(module_name))
139+
module_name, Path(module_filename)
137140
)
138141
except Exception as cause:
139142
error = RuntimeError(
@@ -191,7 +194,8 @@ def split_package_name_and_version(pkg: str) -> Tuple[str, str]:
191194
name, version = pkg[1:].split("@", 1)
192195
return ("@" + name), version
193196
elif at_count:
194-
return tuple(pkg.split("@", 1))
197+
name, version = pkg.split("@", 1)
198+
return name, version
195199
else:
196200
return pkg, ""
197201

@@ -218,6 +222,7 @@ def derive_config_item_info(config_item: Dict[str, Any]) -> ConfigItemInfo:
218222
aliases[dep_name] = dep_alias
219223
aliased_js_deps.append(f"{dep_alias}@npm:{dep}")
220224

225+
js_pkg_def: Optional[JsPackageDef]
221226
if "js_package" in config_item:
222227
js_pkg_path = Path(config_item["js_package"])
223228
js_pkg_json = js_pkg_path / "package.json"
@@ -230,14 +235,14 @@ def derive_config_item_info(config_item: Dict[str, Any]) -> ConfigItemInfo:
230235
f"{config_item['source_name']!r} does not exist"
231236
) from error
232237
else:
233-
js_pkg_info = JsPackageDef(path=js_pkg_path, name=js_pkg_name)
238+
js_pkg_def = JsPackageDef(path=js_pkg_path, name=js_pkg_name)
234239
else:
235-
js_pkg_info = None
240+
js_pkg_def = None
236241

237242
return ConfigItemInfo(
238243
js_dependency_aliases=aliases,
239244
aliased_js_dependencies=aliased_js_deps,
240-
js_package_def=js_pkg_info,
245+
js_package_def=js_pkg_def,
241246
)
242247

243248

@@ -251,7 +256,7 @@ def _hash_config_item(config_item: Dict[str, Any]) -> str:
251256
return format(short_hash_int, "x")
252257

253258

254-
_CONFIG_SCHEMA = {
259+
_CONFIG_SCHEMA: Any = {
255260
"type": "object",
256261
"properties": {
257262
"version": {"type": "string"},
@@ -283,6 +288,7 @@ def _hash_config_item(config_item: Dict[str, Any]) -> str:
283288
}
284289
},
285290
}
291+
_CONFIG_ITEM_SCHEMA = _CONFIG_SCHEMA["definitions"]["ConfigItem"]
286292

287293

288294
_V = TypeVar("_V", bound=Any)
@@ -294,5 +300,5 @@ def validate_config(value: _V) -> _V:
294300

295301

296302
def validate_config_item(value: _V) -> _V:
297-
validate_schema(value, _CONFIG_SCHEMA["definitions"]["ConfigItem"])
303+
validate_schema(value, _CONFIG_ITEM_SCHEMA)
298304
return value

idom/client/manage.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import subprocess
33
from pathlib import Path
44
from tempfile import TemporaryDirectory
5-
from typing import Optional, Iterable, Sequence, List, Tuple
5+
from typing import Optional, Iterable, List, Tuple
66

77
from .build_config import (
88
BuildConfig,
@@ -124,7 +124,7 @@ def _get_web_module_name_and_file_path(
124124
return pkg_name, build_path
125125

126126

127-
def _npm_install(packages: Sequence[str], cwd: Path) -> None:
127+
def _npm_install(packages: List[str], cwd: Path) -> None:
128128
_run_subprocess(["npm", "install"] + packages, cwd)
129129

130130

idom/client/module.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import inspect
2-
from typing import Any, Optional, List
2+
from types import FrameType
3+
from typing import Any, Optional, List, cast
34
from urllib.parse import urlparse
45

56
from idom.core.vdom import VdomDict, ImportSourceDict, make_vdom_constructor
@@ -51,8 +52,10 @@ def __init__(
5152
self.exports = None
5253
else:
5354
if source_name is None:
54-
module_name: str = inspect.currentframe().f_back.f_globals["__name__"]
55-
source_name = module_name.split(".", 1)[0]
55+
frame = _get_frame_back(1)
56+
if frame is None: # pragma: no cover
57+
raise TypeError("Provide 'source_name' explicitely")
58+
source_name = cast(str, frame.f_globals["__name__"].split(".", 1)[0])
5659
self.url = client.current.web_module_url(source_name, url_or_name)
5760
self.installed = True
5861
self.exports = (
@@ -139,3 +142,12 @@ def _is_url(string: str) -> bool:
139142
else:
140143
parsed = urlparse(string)
141144
return bool(parsed.scheme and parsed.netloc)
145+
146+
147+
def _get_frame_back(index: int) -> Optional[FrameType]:
148+
frame = inspect.currentframe()
149+
for _ in range(index + 1):
150+
if frame is None: # pragma: no cover
151+
return None
152+
frame = frame.f_back
153+
return frame

0 commit comments

Comments
 (0)