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

fix: python version warning #3570

Merged
merged 6 commits into from
Nov 25, 2024
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
1 change: 1 addition & 0 deletions doc/changelog.d/3570.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fix: python version warning
2 changes: 1 addition & 1 deletion src/ansys/mapdl/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 10 additions & 3 deletions src/ansys/mapdl/core/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

"""Module for helper functions"""

from functools import namedtuple
import importlib
import os
import sys
Expand All @@ -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 (
Expand All @@ -61,19 +66,21 @@ 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(
f"Support for Python {py_ver} will be dropped in the next minor "
"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. "
Expand Down
68 changes: 66 additions & 2 deletions tests/test_mapdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@

"""Test MAPDL interface"""
from datetime import datetime
from importlib import reload
import os
from pathlib import Path
import re
import shutil
import tempfile
import time
from unittest.mock import patch
from warnings import catch_warnings

import grpc
import numpy as np
Expand All @@ -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,
Expand All @@ -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:
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -2508,3 +2512,63 @@ 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),
patch("ansys.mapdl.core.DEPRECATING_MINIMUM_PYTHON_VERSION", deprecating),
patch("ansys.mapdl.core.MINIMUM_PYTHON_VERSION", minimal_version),
context,
):
pymapdl.helpers.run_first_time()

# Assert warnings won't be retrigger
with catch_warnings():
reload(pymapdl)

pymapdl.helpers.run_first_time()
Loading