diff --git a/rsconnect/actions.py b/rsconnect/actions.py index 4e21f579..407ca229 100644 --- a/rsconnect/actions.py +++ b/rsconnect/actions.py @@ -31,7 +31,7 @@ manifest_add_file, read_manifest_file, ) -from .environment import Environment, EnvironmentException +from .environment import Environment, MakeEnvironment, EnvironmentException from .log import logger from .metadata import AppStore from .models import AppModes @@ -141,7 +141,7 @@ def inspect_environment( environment_json = check_output(args, universal_newlines=True) except subprocess.CalledProcessError as e: raise api.RSConnectException("Error inspecting environment: %s" % e.output) - return Environment(**json.loads(environment_json)) # type: ignore + return MakeEnvironment(**json.loads(environment_json)) # type: ignore def _verify_server(connect_server): diff --git a/rsconnect/environment.py b/rsconnect/environment.py index ee93c022..e5a96ff7 100644 --- a/rsconnect/environment.py +++ b/rsconnect/environment.py @@ -17,6 +17,7 @@ try: import typing + from typing import Optional except ImportError: typing = None @@ -25,22 +26,23 @@ exec_dir = os.path.dirname(sys.executable) -if sys.version_info[:2] < (3, 7): +Environment = collections.namedtuple( + "Environment", ("conda", "contents", "error", "filename", "locale", "package_manager", "pip", "python", "source",), +) - Environment = collections.namedtuple( - "Environment", - ("conda", "contents", "error", "filename", "locale", "package_manager", "pip", "python", "source",), - ) - Environment.__new__.__defaults__ = (None, "", None, "", "", "", None, None, None) - -else: - Environment = collections.namedtuple( - "Environment", - ("conda", "contents", "error", "filename", "locale", "package_manager", "pip", "python", "source",), - defaults=(None, "", None, "", "", "", None, None, None), - ) - Environment.__doc__ = "Data class encapsulating values needed by various deployment and metadata functions" +def MakeEnvironment( + conda=None, # type: Optional[str] + contents="", # type: Optional[str] + error=None, # type: Optional[str] + filename="", # type: Optional[str] + locale="", # type: Optional[str] + package_manager="", # type: Optional[str] + pip=None, # type: Optional[str] + python=None, # type: Optional[str] + source=None, # type: Optional[str] +): + return Environment(conda, contents, error, filename, locale, package_manager, pip, python, source) class EnvironmentException(Exception): @@ -76,20 +78,20 @@ def detect_environment(dirname, force_generate=False, conda_mode=False, conda=No if result is not None: if conda_mode and result["package_manager"] != "conda": - return Environment( + return MakeEnvironment( error=( 'Conda was requested but no activated Conda environment was found. See "conda activate ' '--help" for more information.' ) ) - result["python"] = get_python_version(Environment(**result)) + result["python"] = get_python_version(MakeEnvironment(**result)) result["pip"] = get_version("pip") if conda: result["conda"] = get_conda_version(conda) result["locale"] = get_default_locale() - return Environment(**result) + return MakeEnvironment(**result) def get_conda(conda=None): diff --git a/tests/test_actions.py b/tests/test_actions.py index 4cda7119..79075469 100644 --- a/tests/test_actions.py +++ b/tests/test_actions.py @@ -42,7 +42,7 @@ which_python, ) from rsconnect.api import RSConnectException, RSConnectServer -from rsconnect.environment import Environment +from rsconnect.environment import MakeEnvironment from .test_data_util import get_manifest_path, get_api_path, get_dir @@ -322,7 +322,7 @@ def test_inspect_environment(self): False, False, sys.executable, - Environment( + MakeEnvironment( conda=None, filename="requirements.txt", locale="en_US.UTF-8", @@ -337,7 +337,7 @@ def test_inspect_environment(self): False, False, sys.executable, - Environment( + MakeEnvironment( conda=None, filename="requirements.txt", locale="en_US.UTF-8", @@ -352,7 +352,7 @@ def test_inspect_environment(self): True, True, "/very/serious/whython", - Environment( + MakeEnvironment( conda="/opt/Conda/bin/conda", filename="requirements.txt", locale="en_US.UTF-8", @@ -367,7 +367,7 @@ def test_inspect_environment(self): False, True, "unused", - Environment(error="Could not even do things"), + MakeEnvironment(error="Could not even do things"), id="exploding", ), ], diff --git a/tests/test_environment.py b/tests/test_environment.py index 82093ccc..5b32cb75 100644 --- a/tests/test_environment.py +++ b/tests/test_environment.py @@ -7,6 +7,7 @@ from rsconnect.environment import ( Environment, EnvironmentException, + MakeEnvironment, detect_environment, get_default_locale, get_python_version, @@ -23,7 +24,7 @@ def python_version(): def test_get_python_version(self): self.assertEqual( - get_python_version(Environment(package_manager="pip")), self.python_version(), + get_python_version(MakeEnvironment(package_manager="pip")), self.python_version(), ) def test_get_default_locale(self): @@ -40,7 +41,7 @@ def test_file(self): self.assertIsInstance(result.locale, str) self.assertIn(".", result.locale) - expected = Environment( + expected = MakeEnvironment( contents="numpy\npandas\nmatplotlib\n", filename="requirements.txt", locale=result.locale, @@ -63,7 +64,7 @@ def test_pip_freeze(self): self.assertIsInstance(result.locale, str) self.assertIn(".", result.locale) - expected = Environment( + expected = MakeEnvironment( contents=result.contents, filename="requirements.txt", locale=result.locale,