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

Adding a warning if there is a conflict between env vars and arguments #2913

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/ansys/mapdl/core/launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -799,9 +799,19 @@ def get_start_instance(start_instance: bool = True):
Raised when ``PYMAPDL_START_INSTANCE`` is not either true or false
(case independent).

Notes
-----
If the environment variable ``PYMAPDL_START_INSTANCE`` is set,
hence the argument ``start_instance`` is overwritten.

"""
germa89 marked this conversation as resolved.
Show resolved Hide resolved
if "PYMAPDL_START_INSTANCE" in os.environ and os.environ["PYMAPDL_START_INSTANCE"]:
# It should not be empty
if isinstance(start_instance, bool):
warnings.warn(
"The environment variable 'PYMAPDL_START_INSTANCE' is set, "
"hence the argument 'start_instance' is overwritten."
)
start_instance = os.environ["PYMAPDL_START_INSTANCE"]
else:
LOG.debug(
Expand Down Expand Up @@ -1475,6 +1485,11 @@ def launch_mapdl(
# Getting IP from env var
ip_env_var = os.environ.get("PYMAPDL_IP", "")
if ip_env_var != "":
if ip:
warnings.warn(
"The env var 'PYMAPDL_IP' is set, hence the 'ip' argument is overwritten."
)

ip = ip_env_var
LOG.debug(f"An IP ({ip}) has been set using 'PYMAPDL_IP' env var.")

Expand Down
14 changes: 13 additions & 1 deletion tests/test_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import os
import tempfile
from time import sleep
import warnings

import psutil
import pytest
Expand Down Expand Up @@ -648,7 +649,18 @@ def test_ip_and_start_instance(

return # Exit

options = launch_mapdl(start_instance=start_instance, ip=ip, _debug_no_launch=True)
if (
isinstance(start_instance_envvar, bool) and isinstance(start_instance, bool)
) or (ip_envvar and ip):
with pytest.warns(UserWarning):
options = launch_mapdl(
start_instance=start_instance, ip=ip, _debug_no_launch=True
)
else:
with warnings.catch_warnings():
options = launch_mapdl(
start_instance=start_instance, ip=ip, _debug_no_launch=True
)

if start_instance_envvar is True:
assert options["start_instance"] is True
Expand Down
Loading