Skip to content

Commit

Permalink
updated importlib.resources to stop using deprecated path function (#…
Browse files Browse the repository at this point in the history
…1550)

* updated importlib.resrouces functionality to now use *files* function instead of *path*

* remove commented out code from before

* changed importlib.resources import to help with black formatting

* added variable elements to help with black code formatting issue, due to lines being too long with the new as_file(files(... paradigm needed

* reran by using the black formatter itself -- should be good now

* changed import structure back to only bringing in the namespace and accessing functions from that

* added missing namespace reference

* added missing namespace reference, again

* made minor changes to return of data_path function and updated the comments accordingly

* updated test_prescient.py for the formatting error

---------

Co-authored-by: Ludovico Bianchi <lbianchi@lbl.gov>
  • Loading branch information
azaidi06 and lbianchi-lbl authored Feb 14, 2025
1 parent 997c3a5 commit b24fde4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
4 changes: 3 additions & 1 deletion idaes/apps/grid_integration/examples/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import pandas as pd


with resources.path("idaes.tests.prescient.5bus", "__init__.py") as pkg_file:
with resources.as_file(
resources.files("idaes.tests.prescient.5bus").joinpath("__init__.py")
) as pkg_file:
prescient_5bus = Path(pkg_file).parent

# rts_gmlc_generator_dataframe = pd.read_csv("gen.csv")
Expand Down
18 changes: 10 additions & 8 deletions idaes/apps/grid_integration/tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ class TestDoubleLoopIntegration:
@pytest.fixture
def data_path(self) -> Path:
# NOTE here we want the path to the entire 5bus directory
# we need to specify __init__.py as a workaround for Python 3.9,
# where importlib.resources.path() requires the resource to be a file
# directories are not supported and will raise an error if attempted
with resources.path("idaes.tests.prescient.5bus", "__init__.py") as pkg_file:
return Path(pkg_file).parent
# we need to specify __init__.py and then use `pathlib.Path.parent` since
# `importlib.resources.as_path()` only supports directories on Python 3.12+
with resources.as_file(
resources.files("idaes.tests.prescient.5bus").joinpath("__init__.py")
) as pkg_file:
return pkg_file.parent

@pytest.mark.unit
def test_data_path_available(self, data_path: Path):
Expand All @@ -71,9 +72,10 @@ def self_scheduler_output_dir(self, tmp_path: Path) -> Path:

@pytest.fixture
def self_scheduler_plugin_path(self) -> Path:
with resources.path(
"idaes.apps.grid_integration.tests",
"self_scheduler_integration_test_plugin.py",
with resources.as_file(
resources.files("idaes.apps.grid_integration.tests").joinpath(
"self_scheduler_integration_test_plugin.py"
)
) as p:
return Path(p)

Expand Down
11 changes: 6 additions & 5 deletions idaes/tests/prescient/test_prescient.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ class Test5Bus:
@pytest.fixture
def data_path(self) -> Path:
# NOTE here we want the path to the entire 5bus directory
# we need to specify __init__.py as a workaround for Python 3.9,
# where importlib.resources.path() requires the resource to be a file
# directories are not supported and will raise an error if attempted
with resources.path("idaes.tests.prescient.5bus", "__init__.py") as pkg_file:
return Path(pkg_file).parent
# we need to specify __init__.py and then use `pathlib.Path.parent` since
# `importlib.resources.as_path()` only supports directories on Python 3.12+
with resources.as_file(
resources.files("idaes.tests.prescient.5bus").joinpath("__init__.py")
) as pkg_file:
return pkg_file.parent

@pytest.mark.unit
def test_data_path_available(self, data_path: Path):
Expand Down

0 comments on commit b24fde4

Please sign in to comment.