Skip to content

Commit 29959ea

Browse files
committed
refactor(robotlangserver): Better error messages if converting from json to dataclasses
1 parent c82b60b commit 29959ea

File tree

3 files changed

+42
-11
lines changed

3 files changed

+42
-11
lines changed

robotcode/language_server/robotframework/diagnostics/imports_manager.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -910,14 +910,21 @@ async def _get_libdoc(name: str, args: Tuple[Any, ...], working_dir: str, base_d
910910
meta_file = Path(self.lib_doc_cache_path, meta.filepath_base.with_suffix(".meta.json"))
911911
if meta_file.exists():
912912
try:
913-
saved_meta = from_json(meta_file.read_text("utf-8"), LibraryMetaData)
914-
if saved_meta == meta:
915-
return from_json(
916-
Path(self.lib_doc_cache_path, meta.filepath_base.with_suffix(".spec.json")).read_text(
917-
"utf-8"
918-
),
919-
LibraryDoc,
920-
)
913+
try:
914+
saved_meta = from_json(meta_file.read_text("utf-8"), LibraryMetaData)
915+
spec_path = None
916+
if saved_meta == meta:
917+
spec_path = Path(self.lib_doc_cache_path, meta.filepath_base.with_suffix(".spec.json"))
918+
return from_json(
919+
spec_path.read_text("utf-8"),
920+
LibraryDoc,
921+
)
922+
except (SystemExit, KeyboardInterrupt):
923+
raise
924+
except BaseException as e:
925+
raise RuntimeError(
926+
f"Failed to load library meta data for library {name} from {spec_path}"
927+
) from e
921928
except (SystemExit, KeyboardInterrupt):
922929
raise
923930
except BaseException as e:

robotcode/language_server/robotframework/diagnostics/library_doc.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ def __hash__(self) -> int:
257257

258258
@dataclass
259259
class ArgumentSpec:
260-
name: str
260+
name: Optional[str]
261261
type: str
262262
positional_only: List[str]
263263
positional_or_named: List[str]
@@ -377,6 +377,17 @@ def name_range(self) -> Range:
377377
else:
378378
return Range.invalid()
379379

380+
@single_call
381+
def normalized_tags(self) -> List[str]:
382+
return [normalize(tag) for tag in self.tags]
383+
384+
@single_call
385+
def is_private(self) -> bool:
386+
if get_robot_version() < (6, 0, 0):
387+
return False
388+
389+
return "robot:private" in self.normalized_tags()
390+
380391
@property
381392
def range(self) -> Range:
382393
if self.name_token is not None:

robotcode/utils/dataclasses.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,15 @@ def as_json(obj: Any, indent: Optional[bool] = None, compact: Optional[bool] = N
150150
return json.dumps(obj, default=__default, indent=4 if indent else None, separators=(",", ":") if compact else None)
151151

152152

153+
def _from_dict_with_name(
154+
name: str, value: Any, types: Union[Type[_T], Tuple[Type[_T], ...], None] = None, /, *, strict: bool = False
155+
) -> _T:
156+
try:
157+
return from_dict(value, types, strict=strict)
158+
except TypeError as e:
159+
raise TypeError(f"Can't convert value for '{name}': {e}") from e
160+
161+
153162
def from_dict(value: Any, types: Union[Type[_T], Tuple[Type[_T], ...], None] = None, /, *, strict: bool = False) -> _T:
154163
if types is None:
155164
return cast(_T, value)
@@ -177,7 +186,7 @@ def from_dict(value: Any, types: Union[Type[_T], Tuple[Type[_T], ...], None] = N
177186
or (isinstance(value, Sequence) and args and issubclass(origin or t, Sequence))
178187
):
179188
if isinstance(value, Mapping):
180-
return cast(_T, {n: from_dict(v, args[1] if args else None) for n, v in value.items()})
189+
return cast(_T, {n: _from_dict_with_name(n, v, args[1] if args else None) for n, v in value.items()})
181190
elif isinstance(value, Sequence) and args:
182191
return cast(_T, (origin or t)(from_dict(v, args) for v in value)) # type: ignore
183192

@@ -234,9 +243,13 @@ def from_dict(value: Any, types: Union[Type[_T], Tuple[Type[_T], ...], None] = N
234243
and match_signature is not None
235244
and match_type_hints is not None
236245
):
246+
237247
params: Dict[str, Any] = {
238-
k: from_dict(v, match_type_hints[k]) for k, v in match_value.items() if k in match_type_hints
248+
k: _from_dict_with_name(k, v, match_type_hints[k])
249+
for k, v in match_value.items()
250+
if k in match_type_hints
239251
}
252+
240253
try:
241254
return match(**params)
242255
except TypeError as ex:

0 commit comments

Comments
 (0)