From a744bb772dcf85e2b8fb8d603b19462478fff84e Mon Sep 17 00:00:00 2001 From: Maxime Rey Date: Thu, 26 Dec 2024 11:59:25 +0100 Subject: [PATCH 1/4] Remove bare except and replace == None by is None. --- src/ansys/mechanical/core/embedding/app_libraries.py | 2 +- src/ansys/mechanical/core/embedding/initializer.py | 4 ++-- src/ansys/mechanical/core/embedding/logger/__init__.py | 4 ++-- src/ansys/mechanical/core/embedding/logger/windows_api.py | 2 +- src/ansys/mechanical/core/mechanical.py | 4 +--- tests/conftest.py | 6 +++--- 6 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/ansys/mechanical/core/embedding/app_libraries.py b/src/ansys/mechanical/core/embedding/app_libraries.py index eb8bff5df..69a75e88c 100644 --- a/src/ansys/mechanical/core/embedding/app_libraries.py +++ b/src/ansys/mechanical/core/embedding/app_libraries.py @@ -74,7 +74,7 @@ def add_mechanical_python_libraries(app_or_version): elif isinstance(app_or_version, App): installdir.append(os.environ[f"AWP_ROOT{app_or_version.version}"]) else: - raise ValueError(f"Invalid input: expected an integer (version) or an instance of App().") + raise ValueError("Invalid input: expected an integer (version) or an instance of App().") location = os.path.join(installdir[0], "Addins", "ACT", "libraries", "Mechanical") sys.path.append(location) diff --git a/src/ansys/mechanical/core/embedding/initializer.py b/src/ansys/mechanical/core/embedding/initializer.py index 05c1ead6e..10cd673bb 100644 --- a/src/ansys/mechanical/core/embedding/initializer.py +++ b/src/ansys/mechanical/core/embedding/initializer.py @@ -138,7 +138,7 @@ def __is_lib_loaded(libname: str): # pragma: no cover RTLD_NOLOAD = 4 try: ctypes.CDLL(libname, RTLD_NOLOAD) - except: + except OSError: return False return True @@ -174,7 +174,7 @@ def initialize(version: int = None): ) return - if version == None: + if version is None: version = _get_latest_default_version() version = __check_for_supported_version(version=version) diff --git a/src/ansys/mechanical/core/embedding/logger/__init__.py b/src/ansys/mechanical/core/embedding/logger/__init__.py index bf8133d48..c96922631 100644 --- a/src/ansys/mechanical/core/embedding/logger/__init__.py +++ b/src/ansys/mechanical/core/embedding/logger/__init__.py @@ -144,14 +144,14 @@ def set_log_level(cls, level: int) -> None: @classmethod def set_log_directory(cls, value: str) -> None: """Configure logging to write to a directory.""" - if value == None: + if value is None: return _get_backend().set_directory(value) @classmethod def set_log_base_directory(cls, directory: str) -> None: """Configure logging to write in a time-stamped subfolder in this directory.""" - if directory == None: + if directory is None: return _get_backend().set_base_directory(directory) diff --git a/src/ansys/mechanical/core/embedding/logger/windows_api.py b/src/ansys/mechanical/core/embedding/logger/windows_api.py index 8e5bcbc3a..a301b42e0 100644 --- a/src/ansys/mechanical/core/embedding/logger/windows_api.py +++ b/src/ansys/mechanical/core/embedding/logger/windows_api.py @@ -41,7 +41,7 @@ def _get_logger(): import Ansys return Ansys.Common.WB1ManagedUtils.Logger - except: + except (ImportError, RuntimeError): raise Exception("Logging cannot be used until after Mechanical embedding is initialized.") diff --git a/src/ansys/mechanical/core/mechanical.py b/src/ansys/mechanical/core/mechanical.py index 55c7bc5fd..cbfe77003 100644 --- a/src/ansys/mechanical/core/mechanical.py +++ b/src/ansys/mechanical/core/mechanical.py @@ -480,9 +480,8 @@ def version(self) -> str: >>> mechanical.version '242' - """ - if self._version == None: + if self._version is None: try: self._disable_logging = True script = ( @@ -537,7 +536,6 @@ def _multi_connect(self, n_attempts=5, timeout=60): timeout : float, optional Maximum allowable time in seconds for establishing a connection. The default is ``60``. - """ # This prevents a single failed connection from blocking other attempts connected = False diff --git a/tests/conftest.py b/tests/conftest.py index 74de05054..bffde69ad 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -167,7 +167,7 @@ def embedded_app(pytestconfig, request): @pytest.fixture(autouse=True) def mke_app_reset(request): global EMBEDDED_APP - if EMBEDDED_APP == None: + if EMBEDDED_APP is None: # embedded app was not started - no need to do anything return terminal_reporter = request.config.pluginmanager.getplugin("terminalreporter") @@ -178,7 +178,7 @@ def mke_app_reset(request): _CHECK_PROCESS_RETURN_CODE = os.name == "nt" -# set to true if you want to see all the subprocess stdout/stderr +# set to True if you want to see all the subprocess stdout/stderr _PRINT_SUBPROCESS_OUTPUT_TO_CONSOLE = False @@ -374,7 +374,7 @@ def mechanical_pool(): def pytest_addoption(parser): mechanical_path = atp.get_mechanical_path(False) - if mechanical_path == None: + if mechanical_path is None: parser.addoption("--ansys-version", default="242") else: mechanical_version = atp.version_from_path("mechanical", mechanical_path) From 2db6875f9b6a7f1b2b466ce29ac8c08a988fa47c Mon Sep 17 00:00:00 2001 From: Maxime Rey Date: Thu, 26 Dec 2024 14:40:05 +0100 Subject: [PATCH 2/4] Remove f-string without placeholders. --- src/ansys/mechanical/core/mechanical.py | 13 ++++++------- src/ansys/mechanical/core/pool.py | 6 +++--- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/ansys/mechanical/core/mechanical.py b/src/ansys/mechanical/core/mechanical.py index cbfe77003..18652eaac 100644 --- a/src/ansys/mechanical/core/mechanical.py +++ b/src/ansys/mechanical/core/mechanical.py @@ -444,14 +444,14 @@ def __init__( self._disable_logging = False if self._local: - self.log_info(f"Mechanical connection is treated as local.") + self.log_info("Mechanical connection is treated as local.") else: - self.log_info(f"Mechanical connection is treated as remote.") + self.log_info("Mechanical connection is treated as remote.") # connect and validate to the channel self._multi_connect(timeout=timeout) - self.log_info("Mechanical is ready to accept grpc calls") + self.log_info("Mechanical is ready to accept grpc calls.") def __del__(self): # pragma: no cover """Clean up on exit.""" @@ -1397,7 +1397,7 @@ def download( if chunk_size > 4 * 1024 * 1024: # 4MB raise ValueError( - f"Chunk sizes bigger than 4 MB can generate unstable behaviour in PyMechanical. " + "Chunk sizes bigger than 4 MB can generate unstable behaviour in PyMechanical. " "Decrease the ``chunk_size`` value." ) @@ -1516,8 +1516,8 @@ def save_chunks_to_file(self, responses, filename, progress_bar=False, target_na if progress_bar: if not _HAS_TQDM: # pragma: no cover raise ModuleNotFoundError( - f"To use the keyword argument 'progress_bar', you need to have installed " - f"the 'tqdm' package.To avoid this message you can set 'progress_bar=False'." + "To use the keyword argument 'progress_bar', you need to have installed " + "the 'tqdm' package.To avoid this message you can set 'progress_bar=False'." ) file_size = 0 @@ -1570,7 +1570,6 @@ def download_project(self, extensions=None, target_dir=None, progress_bar=False) Download all the files in the project. >>> local_file_path_list = mechanical.download_project() - """ destination_directory = target_dir.rstrip("\\/") diff --git a/src/ansys/mechanical/core/pool.py b/src/ansys/mechanical/core/pool.py index 0bad20c81..5f58d8882 100644 --- a/src/ansys/mechanical/core/pool.py +++ b/src/ansys/mechanical/core/pool.py @@ -342,8 +342,8 @@ def map( if progress_bar: if not _HAS_TQDM: # pragma: no cover raise ModuleNotFoundError( - f"To use the keyword argument 'progress_bar', you must have installed " - f"the 'tqdm' package. To avoid this message, you can set 'progress_bar=False'." + "To use the keyword argument 'progress_bar', you must have installed " + "the 'tqdm' package. To avoid this message, you can set 'progress_bar=False'." ) pbar = tqdm(total=jobs_count, desc="Mechanical Running") @@ -386,7 +386,7 @@ def run(name_local=""): else: run_thread.join() if not complete[0]: # pragma: no cover - LOG.error(f"Stopped instance because running failed.") + LOG.error("Stopped instance because running failed.") try: obj.exit() except Exception as e: From 707345f017292afd14cd00982b188e658d881b82 Mon Sep 17 00:00:00 2001 From: Maxime Rey Date: Thu, 26 Dec 2024 19:29:29 +0100 Subject: [PATCH 3/4] Add proper exception. --- src/ansys/mechanical/core/embedding/app.py | 2 +- src/ansys/mechanical/core/misc.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ansys/mechanical/core/embedding/app.py b/src/ansys/mechanical/core/embedding/app.py index a91512c45..a839eab06 100644 --- a/src/ansys/mechanical/core/embedding/app.py +++ b/src/ansys/mechanical/core/embedding/app.py @@ -47,7 +47,7 @@ HAS_ANSYS_VIZ = True """Whether or not PyVista exists.""" -except: +except ImportError: HAS_ANSYS_VIZ = False diff --git a/src/ansys/mechanical/core/misc.py b/src/ansys/mechanical/core/misc.py index 84d45789d..0469b81e5 100644 --- a/src/ansys/mechanical/core/misc.py +++ b/src/ansys/mechanical/core/misc.py @@ -150,7 +150,7 @@ def check_valid_start_instance(start_instance): if start_instance.lower() not in ["true", "false"]: raise ValueError( - f"The value for 'start_instance' should be 'True' or 'False' (case insensitive)." + "The value for 'start_instance' should be 'True' or 'False' (case insensitive)." ) return start_instance.lower() == "true" From 5c6663533f2d918fc3339e965b53b83948c19299 Mon Sep 17 00:00:00 2001 From: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com> Date: Thu, 26 Dec 2024 18:37:40 +0000 Subject: [PATCH 4/4] chore: adding changelog file 1011.miscellaneous.md [dependabot-skip] --- doc/changelog.d/1011.miscellaneous.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 doc/changelog.d/1011.miscellaneous.md diff --git a/doc/changelog.d/1011.miscellaneous.md b/doc/changelog.d/1011.miscellaneous.md new file mode 100644 index 000000000..975814ed1 --- /dev/null +++ b/doc/changelog.d/1011.miscellaneous.md @@ -0,0 +1 @@ +Remove f-string without placeholders and specify exception type. \ No newline at end of file