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

Solve py312 warning on pkgutil.get_loader #7597

Merged
merged 1 commit into from
Apr 9, 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
11 changes: 6 additions & 5 deletions src/ert/config/ert_config.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# mypy: ignore-errors
import copy
import importlib
import logging
import os
import pkgutil
from collections import defaultdict
from dataclasses import dataclass, field
from datetime import datetime
Expand All @@ -19,7 +19,6 @@
Sequence,
Tuple,
Union,
cast,
overload,
)

Expand Down Expand Up @@ -59,7 +58,7 @@
from .workflow_job import ErtScriptLoadFailure, WorkflowJob

if TYPE_CHECKING:
from importlib.abc import FileLoader
pass
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of passing TYPE_CHECKING, can we just remove it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ay yes, should have. Sorry I clicked the merge button too soon.



logger = logging.getLogger(__name__)
Expand All @@ -68,8 +67,10 @@
def site_config_location() -> str:
if "ERT_SITE_CONFIG" in os.environ:
return os.environ["ERT_SITE_CONFIG"]
ert_shared_loader = cast("FileLoader", pkgutil.get_loader("ert.shared"))
return path.dirname(ert_shared_loader.get_filename()) + "/share/ert/site-config"
return str(
Path(importlib.util.find_spec("ert.shared").origin).parent
/ "share/ert/site-config"
)


@dataclass
Expand Down
17 changes: 9 additions & 8 deletions src/ert/shared/hook_implementations/jobs.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import importlib.util
import os
import pkgutil
from os.path import dirname
from typing import TYPE_CHECKING, Dict, List, Optional, cast
from pathlib import Path
from typing import Dict, List, Optional

from jinja2 import Template

from ert.shared.plugins.plugin_manager import hook_implementation
from ert.shared.plugins.plugin_response import plugin_response

if TYPE_CHECKING:
from importlib.abc import FileLoader


def _resolve_ert_share_path() -> str:
ert_shared_loader = cast("FileLoader", pkgutil.get_loader("ert.shared"))
return dirname(ert_shared_loader.get_filename()) + "/share/ert"
spec = importlib.util.find_spec("ert.shared")
assert spec, "Could not find ert.shared in import path"
assert spec.has_location
spec_origin = spec.origin
assert spec_origin
return str(Path(spec_origin).parent / "share/ert")


def _get_jobs_from_directories(directories: List[str]) -> Dict[str, str]:
Expand Down
12 changes: 4 additions & 8 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import fileinput
import importlib
import json
import logging
import os
import pkgutil
import resource
import shutil
import stat
import sys
from argparse import ArgumentParser
from os.path import dirname
from pathlib import Path
from textwrap import dedent
from typing import TYPE_CHECKING, cast
from unittest.mock import MagicMock

from qtpy.QtWidgets import QApplication
Expand Down Expand Up @@ -40,9 +38,6 @@

from .utils import SOURCE_DIR

if TYPE_CHECKING:
from importlib.abc import FileLoader

st.register_type_strategy(Path, st.builds(Path, st.text().map(lambda x: "/tmp/" + x)))


Expand Down Expand Up @@ -97,8 +92,9 @@ def fixture_source_root():
def class_source_root(request, source_root):
request.cls.SOURCE_ROOT = source_root
request.cls.TESTDATA_ROOT = source_root / "test-data"
ert_shared_loader = cast("FileLoader", pkgutil.get_loader("ert.shared"))
request.cls.SHARE_ROOT = dirname(ert_shared_loader.get_filename()) + "/share"
request.cls.SHARE_ROOT = str(
Path(importlib.util.find_spec("ert.shared").origin).parent / "share"
)
yield


Expand Down
14 changes: 6 additions & 8 deletions tests/unit_tests/shared/share/test_forward_models.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import importlib
import os
import pkgutil
from os.path import dirname
from typing import TYPE_CHECKING, cast

if TYPE_CHECKING:
from importlib.abc import FileLoader
from pathlib import Path


def extract_executable(filename):
Expand All @@ -21,8 +17,10 @@ def file_exist_and_is_executable(file_path):


def test_validate_scripts():
ert_shared_loader = cast("FileLoader", pkgutil.get_loader("ert.shared"))
fm_path = dirname(ert_shared_loader.get_filename()) + "/share/ert/forward-models"
fm_path = (
Path(importlib.util.find_spec("ert.shared").origin).parent
/ "share/ert/forward-models"
)
for fm_dir in os.listdir(fm_path):
fm_dir = os.path.join(fm_path, fm_dir)
# get all sub-folder in forward-models
Expand Down
14 changes: 4 additions & 10 deletions tests/unit_tests/shared/share/test_rms.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import importlib.util
import json
import os
import pkgutil
import shutil
import stat
import subprocess
import sys
from os.path import dirname
from typing import TYPE_CHECKING, cast
from pathlib import Path
from unittest.mock import patch

import pytest
Expand All @@ -15,10 +14,6 @@

from ._import_from_location import import_from_location

if TYPE_CHECKING:
from importlib.abc import FileLoader


# import rms.py from ert/forward-models/res/script
# package-data path which. These are kept out of the ert package to avoid the
# overhead of importing ert. This is necessary as these may be invoked as a
Expand Down Expand Up @@ -54,9 +49,8 @@
"""


def _get_ert_shared_dir():
ert_shared_loader = cast("FileLoader", pkgutil.get_loader("ert.shared"))
return dirname(ert_shared_loader.get_filename())
def _get_ert_shared_dir() -> str:
return str(Path(importlib.util.find_spec("ert.shared").origin).parent)


@pytest.mark.usefixtures("use_tmpdir")
Expand Down
16 changes: 5 additions & 11 deletions tests/unit_tests/shared/share/test_templating.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import importlib
import json
import os
import pkgutil
import subprocess
from os.path import dirname
from typing import TYPE_CHECKING, cast
from pathlib import Path

import jinja2
import pytest
Expand All @@ -12,10 +11,6 @@

from ._import_from_location import import_from_location

if TYPE_CHECKING:
from importlib.abc import FileLoader


# import template_render.py from ert/forward-models/templating/script
# package-data path which. These are kept out of the ert package to avoid the
# overhead of importing ert. This is necessary as these may be invoked as a
Expand Down Expand Up @@ -224,10 +219,9 @@ def test_template_executable():
"--template_file template "
"--input_files other.json"
)
ert_shared_loader = cast("FileLoader", pkgutil.get_loader("ert.shared"))
template_render_exec = (
dirname(ert_shared_loader.get_filename())
+ "/share/ert/forward-models/templating/script/template_render.py"
template_render_exec = str(
Path(importlib.util.find_spec("ert.shared").origin).parent
/ "share/ert/forward-models/templating/script/template_render.py"
)

subprocess.call(template_render_exec + params, shell=True, stdout=subprocess.PIPE)
Expand Down