Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check kwarg in mapdl.run #2276

Merged
merged 12 commits into from
Sep 1, 2023
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:
raise ValueError(
"The following keyword arguments are not used by 'mapdl.run':\n"
f"{', '.join(kwargs.keys())}\n"
"Make sure you are using the intended keyword arguments in all\n"
"your mapdl method calls (for example, 'mapdl.something')."
)

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.raises(ValueError):
mapdl.prep7(non_valid_argument=2)

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

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


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

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