From 225dc5c43cece1deea1049fcd703e06541ad4e46 Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Tue, 6 Sep 2022 17:22:31 +0100 Subject: [PATCH] create PathLike type for hints --- openmc/checkvalue.py | 5 +++++ openmc/deplete/results.py | 7 ++++--- openmc/material.py | 6 +++--- openmc/settings.py | 5 +++-- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/openmc/checkvalue.py b/openmc/checkvalue.py index d50b3fcaec..c5d80b2fcb 100644 --- a/openmc/checkvalue.py +++ b/openmc/checkvalue.py @@ -1,8 +1,13 @@ import copy +import os +from typing import Union from collections.abc import Iterable import numpy as np +# Type for arguments that accept file paths +PathLike = Union[str, os.PathLike] + def check_type(name, value, expected_type, expected_iter_type=None, *, none_ok=False): """Ensure that an object is of an expected type. Optionally, if the object is diff --git a/openmc/deplete/results.py b/openmc/deplete/results.py index aea54ba8c5..ab21232207 100644 --- a/openmc/deplete/results.py +++ b/openmc/deplete/results.py @@ -1,7 +1,7 @@ import numbers import bisect import math -from typing import Iterable, Optional, Tuple +from typing import Iterable, Optional, Tuple, Union from warnings import warn import h5py @@ -12,6 +12,7 @@ import openmc.checkvalue as cv from openmc.data.library import DataLibrary from openmc.material import Material, Materials from openmc.exceptions import DataError +from openmc.checkvalue import PathLike __all__ = ["Results", "ResultsList"] @@ -71,7 +72,7 @@ class Results(list): @classmethod - def from_hdf5(cls, filename: str): + def from_hdf5(cls, filename: PathLike): """Load in depletion results from a previous file Parameters @@ -164,7 +165,7 @@ class Results(list): def get_reaction_rate( self, - mat: Material, + mat: Union[Material, str], nuc: str, rx: str ) -> Tuple[np.ndarray, np.ndarray]: diff --git a/openmc/material.py b/openmc/material.py index f137ec0cd7..15a5933a64 100644 --- a/openmc/material.py +++ b/openmc/material.py @@ -3,7 +3,6 @@ from collections.abc import Iterable from copy import deepcopy from numbers import Real from pathlib import Path -import os import re import typing # imported separately as py3.8 requires typing.Iterable import warnings @@ -18,6 +17,7 @@ import openmc.data import openmc.checkvalue as cv from ._xml import clean_indentation, reorder_attributes from .mixin import IDManagerMixin +from openmc.checkvalue import PathLike # Units for density supported by OpenMC @@ -1382,7 +1382,7 @@ class Materials(cv.CheckedList): for material in self: material.make_isotropic_in_lab() - def export_to_xml(self, path: Union[str, os.PathLike] = 'materials.xml'): + def export_to_xml(self, path: PathLike = 'materials.xml'): """Export material collection to an XML file. Parameters @@ -1429,7 +1429,7 @@ class Materials(cv.CheckedList): fh.write('\n') @classmethod - def from_xml(cls, path: Union[str, os.PathLike] = 'materials.xml'): + def from_xml(cls, path: PathLike = 'materials.xml'): """Generate materials collection from XML file Parameters diff --git a/openmc/settings.py b/openmc/settings.py index 4372e679f3..ad5a9b28ae 100644 --- a/openmc/settings.py +++ b/openmc/settings.py @@ -13,6 +13,7 @@ import openmc.checkvalue as cv from . import RegularMesh, Source, VolumeCalculation, WeightWindows from ._xml import clean_indentation, get_text, reorder_attributes +from openmc.checkvalue import PathLike class RunMode(Enum): @@ -1535,7 +1536,7 @@ class Settings: if text is not None: self.max_tracks = int(text) - def export_to_xml(self, path: Union[str, os.PathLike] = 'settings.xml'): + def export_to_xml(self, path: PathLike = 'settings.xml'): """Export simulation settings to an XML file. Parameters @@ -1607,7 +1608,7 @@ class Settings: tree.write(str(p), xml_declaration=True, encoding='utf-8') @classmethod - def from_xml(cls, path: Union[str, os.PathLike] = 'settings.xml'): + def from_xml(cls, path: PathLike = 'settings.xml'): """Generate settings from XML file .. versionadded:: 0.13.0