Skip to content

Commit

Permalink
Check kwarg in mapdl.run (#2276)
Browse files Browse the repository at this point in the history
* Adding checks to Mapdl.run

* Adding unit test

* Adding another test

* Fixing tests

* Fixing tests

* Explaining a bit more

* Apply suggestions from code review

Co-authored-by: Kathy Pippert <[email protected]>

* Using warning and removing mention to 'mapdl.run'

---------

Co-authored-by: Camille <[email protected]>
Co-authored-by: Kathy Pippert <[email protected]>
  • Loading branch information
3 people authored Sep 1, 2023
1 parent d8669c1 commit c65d966
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 8 deletions.
26 changes: 22 additions & 4 deletions src/ansys/mapdl/core/mapdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -3204,11 +3204,31 @@ def run(
if "\n" in command or "\r" in command:
raise ValueError("Use ``input_strings`` for multi-line commands")

# check if we want to avoid the current non-interactive context.
# Check kwargs
verbose = kwargs.pop("verbose", False)
save_fig = kwargs.pop("savefig", False)

# Check if you want to avoid the current non-interactive context.
avoid_non_interactive = kwargs.pop("avoid_non_interactive", False)

# Check if there is an unused keyword argument. If there is, it
# might be because you wrote a wrong argument name.
#
# Remove empty string kwargs
for key, value in list(kwargs.items()):
if value == "":
kwargs.pop(key)

if kwargs:
warn(
"The following keyword arguments are not used:\n"
f"{', '.join(kwargs.keys())}\n"
"Make sure you are using the intended keyword arguments.",
UserWarning,
)

if self._store_commands and not avoid_non_interactive:
# If we are using NBLOCK on input, we should not strip the string
# If you are using NBLOCK on input, you should not strip the string
self._stored_commands.append(command)
return

Expand Down Expand Up @@ -3267,7 +3287,6 @@ def run(
# Edge case. `\title, 'par=1234' `
self._check_parameter_name(param_name)

verbose = kwargs.get("verbose", False)
text = self._run(command, verbose=verbose, mute=mute)

if command[:4].upper() == "/CLE" and self.is_grpc:
Expand All @@ -3294,7 +3313,6 @@ def run(
if short_cmd in PLOT_COMMANDS:
self._log.debug("It is a plot command.")
plot_path = self._get_plot_name(text)
save_fig = kwargs.get("savefig", False)
if save_fig:
return self._download_plot(plot_path, save_fig)
else:
Expand Down
8 changes: 4 additions & 4 deletions src/ansys/mapdl/core/mapdl_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3110,7 +3110,7 @@ def nsol(
comp=comp,
name=name,
sector=sector,
kwargs=kwargs,
**kwargs,
)
return self.vget("_temp", nvar)

Expand All @@ -3133,7 +3133,7 @@ def esol(
item=item,
comp=comp,
name=name,
kwargs=kwargs,
**kwargs,
)
return self.vget("_temp", nvar)

Expand Down Expand Up @@ -3203,7 +3203,7 @@ def get_nsol(
comp=comp,
name=name,
sector=sector,
kwargs=kwargs,
**kwargs,
)

def get_esol(
Expand Down Expand Up @@ -3317,7 +3317,7 @@ def get_esol(
comp=comp,
name=name,
sector=sector,
kwargs=kwargs,
**kwargs,
)
# Using get_variable because it deletes the intermediate parameter after using it.
return self.get_variable(VAR_IR, tstrt=tstrt, kcplx=kcplx)
Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@ def coupled_example(mapdl, cleared):
mapdl_code = mapdl_code.replace(
"SOLVE", "SOLVE\n/COM Ending script after first simulation\n/EOF"
)
mapdl.finish()
mapdl.input_strings(mapdl_code)


Expand Down
20 changes: 20 additions & 0 deletions tests/test_mapdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2129,3 +2129,23 @@ def test_distributed(mapdl):
assert mapdl._distributed
else:
assert not mapdl._distributed # assuming remote is using -smp


def test_non_used_kwargs(mapdl):
with pytest.warns(UserWarning):
mapdl.prep7(non_valid_argument=2)

with pytest.warns(UserWarning):
mapdl.run("/prep7", True, False, unvalid_argument=2)

kwarg = {"unvalid_argument": 2}
with pytest.warns(UserWarning):
mapdl.run("/prep7", True, None, **kwarg)


def test_non_valid_kwarg(mapdl):
mapdl.prep7()
mapdl.blc4(0, 0, 1, 1, 1)

with pytest.warns(UserWarning):
mapdl.cdwrite(options="DB", fname="test1", ext="cdb")

0 comments on commit c65d966

Please sign in to comment.