mirror of
https://github.com/openmc-dev/openmc.git
synced 2026-07-21 06:25:30 -04:00
Update check_type calls to accept both str and os.PathLike objects. (#3618)
Co-authored-by: Paul Romano <paul.k.romano@gmail.com>
This commit is contained in:
parent
f10d7d9f67
commit
a74c1424a8
7 changed files with 58 additions and 10 deletions
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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'])
|
||||
|
|
|
|||
|
|
@ -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'])
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
46
tests/unit_tests/test_pathlike_simple.py
Normal file
46
tests/unit_tests/test_pathlike_simple.py
Normal file
|
|
@ -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
|
||||
Loading…
Add table
Add a link
Reference in a new issue