From c65d966071d879cfed0eeb0d31d427a9f20b09ef Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Fri, 1 Sep 2023 14:47:05 +0200 Subject: [PATCH] Check kwarg in mapdl.run (#2276) * 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 <84872299+PipKat@users.noreply.github.com> * Using warning and removing mention to 'mapdl.run' --------- Co-authored-by: Camille <78221213+clatapie@users.noreply.github.com> Co-authored-by: Kathy Pippert <84872299+PipKat@users.noreply.github.com> --- src/ansys/mapdl/core/mapdl.py | 26 ++++++++++++++++++++++---- src/ansys/mapdl/core/mapdl_grpc.py | 8 ++++---- tests/conftest.py | 1 + tests/test_mapdl.py | 20 ++++++++++++++++++++ 4 files changed, 47 insertions(+), 8 deletions(-) diff --git a/src/ansys/mapdl/core/mapdl.py b/src/ansys/mapdl/core/mapdl.py index 81b4953b68..0448024485 100644 --- a/src/ansys/mapdl/core/mapdl.py +++ b/src/ansys/mapdl/core/mapdl.py @@ -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 @@ -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: @@ -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: diff --git a/src/ansys/mapdl/core/mapdl_grpc.py b/src/ansys/mapdl/core/mapdl_grpc.py index fd7a5bc459..cf13af36cd 100644 --- a/src/ansys/mapdl/core/mapdl_grpc.py +++ b/src/ansys/mapdl/core/mapdl_grpc.py @@ -3110,7 +3110,7 @@ def nsol( comp=comp, name=name, sector=sector, - kwargs=kwargs, + **kwargs, ) return self.vget("_temp", nvar) @@ -3133,7 +3133,7 @@ def esol( item=item, comp=comp, name=name, - kwargs=kwargs, + **kwargs, ) return self.vget("_temp", nvar) @@ -3203,7 +3203,7 @@ def get_nsol( comp=comp, name=name, sector=sector, - kwargs=kwargs, + **kwargs, ) def get_esol( @@ -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) diff --git a/tests/conftest.py b/tests/conftest.py index 939de3648c..773db8b584 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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) diff --git a/tests/test_mapdl.py b/tests/test_mapdl.py index 55ce9a43ec..8eee9fbe26 100644 --- a/tests/test_mapdl.py +++ b/tests/test_mapdl.py @@ -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")