Repository navigation
Convert path_input to str in _process_CLI_arguments - #4188
barlowa124 wants to merge 1 commit into
Conversation
The returned args are documented as a list of str, but path_input was appended unchanged. openmc.lib.init() calls .encode() on every argument, so Model.init_lib(directory=Path(...)) raised AttributeError after exporting the XML files. subprocess-based callers are unaffected. Fixes: openmc-dev#4185
|
Thanks for picking up #4185! The change fixes the One thing to consider: import os
import tempfile
class FsPath:
def __fspath__(self):
return 'model'
os.chdir(tempfile.mkdtemp())
os.mkdir('model')
entry, = os.scandir('.')
for p in (entry, FsPath()):
print(repr(str(p)), repr(os.fspath(p)))With this PR, Would you consider Here are two test cases for class _FsPath:
"""Path-like object that defines only __fspath__()."""
def __init__(self, path):
self._path = path
def __fspath__(self):
return self._path
def test_process_cli_arguments_path_input_direntry(tmp_path):
(tmp_path / 'model').mkdir()
with os.scandir(tmp_path) as it:
entry = next(it)
args = list(_process_CLI_arguments(path_input=entry))
assert args[-1] == str(tmp_path / 'model')
assert all(isinstance(arg, str) for arg in args)
def test_process_cli_arguments_path_input_fspath_only(tmp_path):
args = list(_process_CLI_arguments(path_input=_FsPath(str(tmp_path))))
assert args[-1] == str(tmp_path)
assert all(isinstance(arg, str) for arg in args)Thanks again! Drafted with Claude Code (Claude Opus 5.5); reviewed by grok-4.7. |
Description
Model.init_lib(directory=Path(...))fails withAttributeError: 'PosixPath' object has no attribute 'encode'(#4185). Thedirectoryargument is documented asPathLike, is passed through aspath_inputto_process_CLI_arguments, and reachesopenmc.lib.init, which calls.encode()on every argument.restart_fileis converted withstr()in the same function.path_inputwas appended unchanged.The fix converts
path_inputwithstr(), matching therestart_filehandling two blocks earlier. The subprocess-based callers (run,calculate_volumes) acceptstrarguments, so they are unaffected.plot_geometrytakes apath_inputdocumented asstronly and appends it unchanged. That path is left alone sincesubprocess.Popenacceptsos.PathLikearguments and the annotation does not promisePathLikesupport.str()was used rather thanos.fspath()to match therestart_fileconversion directly above it. For aPathLikeargument the two are equivalent.No documentation change is needed.
init_libalready annotatesdirectoryasPathLike | None, so the change brings the implementation in line with the documented contract.tests/unit_testshad no coverage of_process_CLI_arguments.The new
test_executor.pychecks thatpath_inputcomes back asstrfor bothstrandPathinputs. It could not be run locally becauseimport openmcrequires a builtlibopenmcand this environment has no HDF5 toolchain for the C++ build. The function was verified standalone instead. Before the change it returned aPosixPathfor aPathinput. After the change it returnsstr.Fixes #4185
AI Assistance
Checklist
Summary by CodeRabbit
Bug Fixes
Tests