From 18f8d498587cbd3026e355eb08e33da922c10f0c Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Fri, 22 Nov 2024 16:40:37 +0100 Subject: [PATCH 1/6] fix: python version warning --- src/ansys/mapdl/core/__init__.py | 2 +- src/ansys/mapdl/core/helpers.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ansys/mapdl/core/__init__.py b/src/ansys/mapdl/core/__init__.py index 172b961ac8..99df5b589f 100644 --- a/src/ansys/mapdl/core/__init__.py +++ b/src/ansys/mapdl/core/__init__.py @@ -64,7 +64,7 @@ BUILDING_GALLERY: bool = False RUNNING_TESTS: bool = False -DEPRECATING_MINIMUM_PYTHON_VERSION: bool = True +DEPRECATING_MINIMUM_PYTHON_VERSION: bool = False MINIMUM_PYTHON_VERSION: Tuple[int, int] = (3, 10) # Import related globals diff --git a/src/ansys/mapdl/core/helpers.py b/src/ansys/mapdl/core/helpers.py index 308520e5d0..5dac68d227 100644 --- a/src/ansys/mapdl/core/helpers.py +++ b/src/ansys/mapdl/core/helpers.py @@ -73,7 +73,7 @@ def run_first_time() -> None: "release." ) - if sys.version_info[1] <= MINIMUM_PYTHON_VERSION[1]: + if sys.version_info[1] < MINIMUM_PYTHON_VERSION[1]: warn( f"Python {py_ver} is not being tested or officially supported. " "It is recommended you use a newer version of Python. " From f6c19cd01e7bad8818726160640d67a443cda4be Mon Sep 17 00:00:00 2001 From: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com> Date: Fri, 22 Nov 2024 15:43:54 +0000 Subject: [PATCH 2/6] chore: adding changelog file 3570.fixed.md [dependabot-skip] --- doc/changelog.d/3570.fixed.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 doc/changelog.d/3570.fixed.md diff --git a/doc/changelog.d/3570.fixed.md b/doc/changelog.d/3570.fixed.md new file mode 100644 index 0000000000..eb89327d90 --- /dev/null +++ b/doc/changelog.d/3570.fixed.md @@ -0,0 +1 @@ +fix: python version warning \ No newline at end of file From 41db24e014f03d60280fdadbae6f056bdcb0d869 Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Mon, 25 Nov 2024 11:27:56 +0100 Subject: [PATCH 3/6] feat: using a function to retrieve the python version --- src/ansys/mapdl/core/helpers.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/ansys/mapdl/core/helpers.py b/src/ansys/mapdl/core/helpers.py index 5dac68d227..0f70d2096d 100644 --- a/src/ansys/mapdl/core/helpers.py +++ b/src/ansys/mapdl/core/helpers.py @@ -22,6 +22,7 @@ """Module for helper functions""" +from functools import namedtuple import importlib import os import sys @@ -43,6 +44,10 @@ def is_installed(package_name: str) -> bool: return False +def get_python_version() -> namedtuple: + return sys.version_info + + def run_first_time() -> None: """Run this function the first time PyMAPDL is imported""" from ansys.mapdl.core import ( @@ -61,11 +66,13 @@ def run_first_time() -> None: os.makedirs(USER_DATA_PATH) # Show warning about Python compatibility - py_ver = f"{sys.version_info[0]}.{sys.version_info[1]}" + version_info = get_python_version() + + py_ver = f"{version_info[0]}.{version_info[1]}" py_ver_min = f"{MINIMUM_PYTHON_VERSION[0]}.{MINIMUM_PYTHON_VERSION[1]}" if ( - sys.version_info[1] == MINIMUM_PYTHON_VERSION[1] + version_info[1] == MINIMUM_PYTHON_VERSION[1] and DEPRECATING_MINIMUM_PYTHON_VERSION ): warn( @@ -73,7 +80,7 @@ def run_first_time() -> None: "release." ) - if sys.version_info[1] < MINIMUM_PYTHON_VERSION[1]: + if version_info[1] < MINIMUM_PYTHON_VERSION[1]: warn( f"Python {py_ver} is not being tested or officially supported. " "It is recommended you use a newer version of Python. " From c08eed6474c6b84268a6a7adfb009173208e116d Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Mon, 25 Nov 2024 11:31:35 +0100 Subject: [PATCH 4/6] tests: adding tests --- tests/test_mapdl.py | 70 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 68 insertions(+), 2 deletions(-) diff --git a/tests/test_mapdl.py b/tests/test_mapdl.py index 8749850056..7763f72387 100644 --- a/tests/test_mapdl.py +++ b/tests/test_mapdl.py @@ -22,6 +22,7 @@ """Test MAPDL interface""" from datetime import datetime +from importlib import reload import os from pathlib import Path import re @@ -29,6 +30,7 @@ import tempfile import time from unittest.mock import patch +from warnings import catch_warnings import grpc import numpy as np @@ -44,6 +46,7 @@ from ansys.mapdl.reader.rst import Result from ansys.mapdl import core as pymapdl +from ansys.mapdl.core import USER_DATA_PATH from ansys.mapdl.core.commands import CommandListingOutput from ansys.mapdl.core.errors import ( CommandDeprecated, @@ -60,7 +63,8 @@ # Path to files needed for examples PATH = os.path.dirname(os.path.abspath(__file__)) -test_files = os.path.join(PATH, "test_files") +TEST_FILES = os.path.join(PATH, "test_files") +FIRST_TIME_FILE = os.path.join(USER_DATA_PATH, ".firstime") if VALID_PORTS: @@ -827,7 +831,7 @@ def test_cyclic_solve(mapdl, cleared): # build the cyclic model mapdl.prep7() mapdl.shpp("off") - mapdl.cdread("db", os.path.join(test_files, "sector.cdb")) + mapdl.cdread("db", os.path.join(TEST_FILES, "sector.cdb")) mapdl.prep7() time.sleep(1.0) mapdl.cyclic() @@ -2508,3 +2512,65 @@ def test_cwd_changing_directory(mapdl): assert mapdl._path == prev_path assert mapdl.directory == prev_path + + +def test_load_not_raising_warning(): + assert os.path.exists(FIRST_TIME_FILE) + + os.remove(FIRST_TIME_FILE) + + with catch_warnings(): + reload(pymapdl) + + +@pytest.mark.parametrize( + "python_version,minimal_version,deprecating,context", + [ + ((3, 9, 10), (3, 9), False, catch_warnings()), # standard case + ( + (3, 9, 10), + (3, 9), + True, + pytest.warns(UserWarning, match="will be dropped in the next minor"), + ), + ( + (3, 9, 10), + (3, 10), + False, + pytest.warns( + UserWarning, match="It is recommended you use a newer version of Python" + ), + ), + ( + (3, 9, 10), + (3, 10), + True, + pytest.warns( + UserWarning, match="It is recommended you use a newer version of Python" + ), + ), + ], +) +def test_raising_warns(python_version, minimal_version, deprecating, context): + # To trigger the warnings + os.remove(FIRST_TIME_FILE) + + def func(*args, **kwargs): + return python_version + + # We can't use reload here because it seems to remove the patching + with patch("ansys.mapdl.core.helpers.get_python_version", func) as mck_pyver: + with patch( + "ansys.mapdl.core.DEPRECATING_MINIMUM_PYTHON_VERSION", deprecating + ) as mck_dep: + with patch( + "ansys.mapdl.core.MINIMUM_PYTHON_VERSION", minimal_version + ) as mck_min: + with context: + pymapdl.helpers.run_first_time() + + # Assert warnings won't be retrigger + with catch_warnings(): + reload(pymapdl) + + pymapdl.helpers.run_first_time() From 526e513caefd87222df418e93a795cb89fc182da Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Mon, 25 Nov 2024 11:37:51 +0100 Subject: [PATCH 5/6] refactor: using multiple context managers in with. --- tests/test_mapdl.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/test_mapdl.py b/tests/test_mapdl.py index 7763f72387..733c8c8ef3 100644 --- a/tests/test_mapdl.py +++ b/tests/test_mapdl.py @@ -2558,16 +2558,16 @@ def test_raising_warns(python_version, minimal_version, deprecating, context): def func(*args, **kwargs): return python_version - # We can't use reload here because it seems to remove the patching - with patch("ansys.mapdl.core.helpers.get_python_version", func) as mck_pyver: - with patch( + # We can't use "reload" here because it seems to remove the patching + with ( + patch("ansys.mapdl.core.helpers.get_python_version", func) as mck_pyver, + patch( "ansys.mapdl.core.DEPRECATING_MINIMUM_PYTHON_VERSION", deprecating - ) as mck_dep: - with patch( - "ansys.mapdl.core.MINIMUM_PYTHON_VERSION", minimal_version - ) as mck_min: - with context: - pymapdl.helpers.run_first_time() + ) as mck_dep, + patch("ansys.mapdl.core.MINIMUM_PYTHON_VERSION", minimal_version) as mck_min, + context, + ): + pymapdl.helpers.run_first_time() # Assert warnings won't be retrigger with catch_warnings(): From 7fcc4fe9f06bdf67dccdb9d75117bfa76a76c249 Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Mon, 25 Nov 2024 11:47:59 +0100 Subject: [PATCH 6/6] refactor: removing as. --- tests/test_mapdl.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/tests/test_mapdl.py b/tests/test_mapdl.py index 733c8c8ef3..c6a3c5f010 100644 --- a/tests/test_mapdl.py +++ b/tests/test_mapdl.py @@ -2560,11 +2560,9 @@ def func(*args, **kwargs): # We can't use "reload" here because it seems to remove the patching with ( - patch("ansys.mapdl.core.helpers.get_python_version", func) as mck_pyver, - patch( - "ansys.mapdl.core.DEPRECATING_MINIMUM_PYTHON_VERSION", deprecating - ) as mck_dep, - patch("ansys.mapdl.core.MINIMUM_PYTHON_VERSION", minimal_version) as mck_min, + patch("ansys.mapdl.core.helpers.get_python_version", func), + patch("ansys.mapdl.core.DEPRECATING_MINIMUM_PYTHON_VERSION", deprecating), + patch("ansys.mapdl.core.MINIMUM_PYTHON_VERSION", minimal_version), context, ): pymapdl.helpers.run_first_time()