diff --git a/openmc/checkvalue.py b/openmc/checkvalue.py index 4fa205b14..5ff2cf9ac 100644 --- a/openmc/checkvalue.py +++ b/openmc/checkvalue.py @@ -37,7 +37,7 @@ def check_type(name, value, expected_type, expected_iter_type=None, *, none_ok=F [t.__name__ for t in expected_type])) else: msg = (f'Unable to set "{name}" to "{value}" which is not of type "' - f'{expected_type.__name__}"') + f'{expected_type}"') raise TypeError(msg) if expected_iter_type: diff --git a/openmc/mgxs/library.py b/openmc/mgxs/library.py index 12a4630bd..f782cbe9e 100644 --- a/openmc/mgxs/library.py +++ b/openmc/mgxs/library.py @@ -10,6 +10,7 @@ import numpy as np import openmc import openmc.mgxs import openmc.checkvalue as cv +from openmc.checkvalue import PathLike from ..tallies import ESTIMATOR_TYPES @@ -851,7 +852,7 @@ class Library: 'since a statepoint has not yet been loaded' raise ValueError(msg) - cv.check_type('filename', filename, str) + cv.check_type('filename', filename, (str, PathLike)) cv.check_type('directory', directory, str) import h5py @@ -894,7 +895,7 @@ class Library: """ - cv.check_type('filename', filename, str) + cv.check_type('filename', filename, (str, PathLike)) cv.check_type('directory', directory, str) # Make directory if it does not exist @@ -930,7 +931,7 @@ class Library: """ - cv.check_type('filename', filename, str) + cv.check_type('filename', filename, (str, PathLike)) cv.check_type('directory', directory, str) # Make directory if it does not exist diff --git a/openmc/mgxs/mdgxs.py b/openmc/mgxs/mdgxs.py index b95a4fbc0..c12c1a9ab 100644 --- a/openmc/mgxs/mdgxs.py +++ b/openmc/mgxs/mdgxs.py @@ -7,6 +7,7 @@ import numpy as np import openmc import openmc.checkvalue as cv +from openmc.checkvalue import PathLike from openmc.mgxs import MGXS from .mgxs import _DOMAIN_TO_FILTER @@ -722,7 +723,7 @@ class MDGXS(MGXS): """ - cv.check_type('filename', filename, str) + cv.check_type('filename', filename, (str, PathLike)) cv.check_type('directory', directory, str) cv.check_value('format', format, ['csv', 'excel', 'pickle', 'latex']) cv.check_value('xs_type', xs_type, ['macro', 'micro']) diff --git a/openmc/mgxs/mgxs.py b/openmc/mgxs/mgxs.py index b8f2b8d3f..533ab0ad3 100644 --- a/openmc/mgxs/mgxs.py +++ b/openmc/mgxs/mgxs.py @@ -10,6 +10,7 @@ import numpy as np import openmc from openmc.data import REACTION_MT, REACTION_NAME, FISSION_MTS import openmc.checkvalue as cv +from openmc.checkvalue import PathLike from ..tallies import ESTIMATOR_TYPES from . import EnergyGroups @@ -1982,7 +1983,7 @@ class MGXS: """ - cv.check_type('filename', filename, str) + cv.check_type('filename', filename, (str, PathLike)) cv.check_type('directory', directory, str) cv.check_value('format', format, ['csv', 'excel', 'pickle', 'latex']) cv.check_value('xs_type', xs_type, ['macro', 'micro']) diff --git a/openmc/mgxs_library.py b/openmc/mgxs_library.py index ce86f97df..4bc2d4a5a 100644 --- a/openmc/mgxs_library.py +++ b/openmc/mgxs_library.py @@ -2514,8 +2514,7 @@ class MGXSLibrary: """ - check_type('filename', filename, PathLike) - + check_type('filename', filename, (str, PathLike)) # Create and write to the HDF5 file file = h5py.File(filename, "w", libver=libver) file.attrs['filetype'] = np.bytes_(_FILETYPE_MGXS_LIBRARY) @@ -2554,7 +2553,7 @@ class MGXSLibrary: raise ValueError("Either path or openmc.config['mg_cross_sections']" "must be set") - check_type('filename', filename, str) + check_type('filename', filename, (str, PathLike)) file = h5py.File(filename, 'r') # Check filetype and version diff --git a/openmc/plots.py b/openmc/plots.py index e9130f0be..a0bde3f00 100644 --- a/openmc/plots.py +++ b/openmc/plots.py @@ -439,7 +439,7 @@ class PlotBase(IDManagerMixin): @filename.setter def filename(self, filename): - cv.check_type('filename', filename, str) + cv.check_type('filename', filename, (str, PathLike)) self._filename = filename @property diff --git a/tests/unit_tests/test_pathlike_simple.py b/tests/unit_tests/test_pathlike_simple.py new file mode 100644 index 000000000..e0116bf01 --- /dev/null +++ b/tests/unit_tests/test_pathlike_simple.py @@ -0,0 +1,46 @@ +"""Simple test for PathLike filename support""" + +from pathlib import Path + +import pytest +import openmc +from openmc.checkvalue import check_type, PathLike + + +def test_pathlike_type_checking(): + """Test that PathLike type checking works correctly""" + + # Test with string (should work) + check_type('filename', 'test.txt', PathLike) + + # Test with Path object (should work) + path_obj = Path('test.txt') + check_type('filename', path_obj, PathLike) + + # Test with Path object containing subdirectories (should work) + path_with_subdir = Path('subdir') / 'test.txt' + check_type('filename', path_with_subdir, PathLike) + + # Test with invalid type (should raise TypeError) + with pytest.raises(TypeError): + check_type('filename', 123, PathLike) + + +def test_plot_filename_pathlike(): + """Test that plot filename accepts Path objects""" + + plot = openmc.Plot() + + # Test with string (should still work) + plot.filename = "test_plot" + assert plot.filename == "test_plot" + + # Test with Path object + path_obj = Path("test_plot_path") + plot.filename = path_obj + assert plot.filename == path_obj + + # Test with Path object containing subdirectories + path_with_subdir = Path("subdir") / "test_plot" + plot.filename = path_with_subdir + assert plot.filename == path_with_subdir