Skip to content

Commit c93272a

Browse files
committed
fix: tests and missing arguments
1 parent 13b3ba3 commit c93272a

File tree

2 files changed

+37
-26
lines changed

2 files changed

+37
-26
lines changed

src/pyconverter/xml2py/ast_tree.py

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def get_quant_iter_pos(name: str) -> tuple:
145145
146146
Parameters
147147
----------
148-
name : str
148+
name: str
149149
Name of the parameter containing the iterator.
150150
151151
Returns
@@ -1000,7 +1000,7 @@ def py_term(self, links=None, base_url=None):
10001000
if self.parm_types is not None:
10011001
ptype_str = " or ".join([parm_type.__name__ for parm_type in self.parm_types])
10021002

1003-
return f"{arg} : {ptype_str}"
1003+
return f"{arg}: {ptype_str}"
10041004
return f"{arg}"
10051005

10061006
if self.term.tag in item_needing_links_base_url:
@@ -2166,7 +2166,7 @@ class Argument:
21662166
"""Argument object."""
21672167

21682168
def __init__(
2169-
self, element: str | Element, initial_argument: List, description: Element | None = None
2169+
self, element: str | Element, initial_argument: List, description: Element | str | None = None
21702170
) -> None:
21712171
if description is None:
21722172
if isinstance(element[0], Term):
@@ -2265,7 +2265,7 @@ def multiple_args(self):
22652265
)
22662266
if name_iter_prev != name_iter_next:
22672267
logging.warning(
2268-
f"The argument name is not consistent : {name_iter_prev} != {name_iter_next}" # noqa : E501
2268+
f"The argument name is not consistent: {name_iter_prev} != {name_iter_next}" # noqa : E501
22692269
)
22702270
logging.info(
22712271
"Applying the longest name for the argument list as it's probably coming from a typography." # noqa : E501
@@ -2317,20 +2317,21 @@ def types(self) -> List[type]:
23172317
This is either a string, float, or integer (or some combination thereof).
23182318
23192319
"""
2320-
varlist = self._description.rec_find("Variablelist")
2321-
23222320
parm_types = [str]
2323-
if varlist is not None:
2324-
terms = varlist.terms
2325-
if terms:
2326-
terms_numeric = [is_numeric(term) for term in terms]
2327-
if any(terms_numeric):
2328-
parm_types = [int, str]
2329-
else:
2330-
parm_types = [str]
2321+
if isinstance(self._description, Element):
2322+
varlist = self._description.rec_find("Variablelist")
2323+
2324+
if varlist is not None:
2325+
terms = varlist.terms
2326+
if terms:
2327+
terms_numeric = [is_numeric(term) for term in terms]
2328+
if any(terms_numeric):
2329+
parm_types = [int, str]
2330+
else:
2331+
parm_types = [str]
23312332

2332-
# consider checking for bool
2333-
# terms_numeric = set(terms) == set(['1', '0'])
2333+
# consider checking for bool
2334+
# terms_numeric = set(terms) == set(['1', '0'])
23342335

23352336
return parm_types
23362337

@@ -2365,10 +2366,13 @@ def to_py_docstring(
23652366
) -> List[str]:
23662367
"""Return a list of string to enable converting the element to an RST format."""
23672368
if self.py_arg_name not in ["--", "–", ""]:
2368-
docstring = [f'{indent}{self.py_arg_name} : {self.str_types(" or ")}']
2369-
rst_description = self._description.to_rst(
2370-
indent=indent, max_length=max_length, links=links, base_url=base_url, fcache=fcache
2371-
)
2369+
docstring = [f'{indent}{self.py_arg_name}: {self.str_types(" or ")}']
2370+
if isinstance(self._description, str):
2371+
rst_description = self._description
2372+
else:
2373+
rst_description = self._description.to_rst(
2374+
indent=indent, max_length=max_length, links=links, base_url=base_url, fcache=fcache
2375+
)
23722376
description_indent = " " * 4 + indent
23732377
if not "* " in rst_description:
23742378
list_description = self.resized_description(
@@ -2378,7 +2382,7 @@ def to_py_docstring(
23782382
rst_description = textwrap.indent(rst_description, description_indent)
23792383
list_description = rst_description.split("\n")
23802384

2381-
docstring = [f'{indent}{self.py_arg_name} : {self.str_types(" or ")}']
2385+
docstring = [f'{indent}{self.py_arg_name}: {self.str_types(" or ")}']
23822386
docstring.extend(list_description)
23832387
else:
23842388
docstring = []
@@ -2464,8 +2468,15 @@ def arg_desc(self) -> List[Argument]:
24642468
arguments = ArgumentList(elem, self.args)
24652469
else:
24662470
arguments += ArgumentList(elem, self.args)
2467-
2471+
24682472
if arguments is not None:
2473+
if len(arguments.py_arg_names) < len(arguments.initial_args):
2474+
for arg in arguments.initial_args:
2475+
if arg not in arguments.py_arg_names:
2476+
new_arg = Argument(arg, arguments.initial_args, "")
2477+
if new_arg.py_arg_name != "":
2478+
arguments.arguments.append(new_arg)
2479+
24692480
return arguments.arguments
24702481

24712482
else:
@@ -2883,7 +2894,7 @@ def py_source(self, custom_functions=None, indent=""):
28832894
28842895
Parameters
28852896
----------
2886-
custom_functions : CustomFunctions, optional
2897+
custom_functions: CustomFunctions, optional
28872898
Custom functions to add to the command. The default is ``None``.
28882899
"""
28892900
if custom_functions is None or self.py_name not in custom_functions.py_names:
@@ -2905,9 +2916,9 @@ def to_python(self, custom_functions=None, indent=""):
29052916
29062917
Parameters
29072918
----------
2908-
custom_functions : CustomFunctions, optional
2919+
custom_functions: CustomFunctions, optional
29092920
Custom functions to add to the command. The default is ``None``.
2910-
indent : str, optional
2921+
indent: str, optional
29112922
Indentation of the Python function. The default is ``""``.
29122923
29132924
Returns

tests/test_writer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def test_convert(command_map, custom_functions):
3636
command_map["E"].py_source(custom_functions)
3737
== ' command = f"E,{i},{j},{k},{l},{m},{n},{o},{p}"\n return self.run(command, **kwargs)\n' # noqa : E501
3838
)
39-
assert 'def zoom(self, wn="", lab="", x1="", y1="", x2="", y2="", **kwargs):\n r"""Zooms a region of a display window.\n\n' in command_map[ # noqa : E501
39+
assert 'def zoom(self, wn: str="", lab: str="", x1: str="", y1: str="", x2: str="", y2: str="", **kwargs):\n r"""Zooms a region of a display window.\n\n' in command_map[ # noqa : E501
4040
"/ZOOM"
4141
].to_python(
4242
custom_functions

0 commit comments

Comments
 (0)