create PathLike type for hints

This commit is contained in:
Jonathan Shimwell 2022-09-06 17:22:31 +01:00
parent 1c917201ae
commit 225dc5c43c
4 changed files with 15 additions and 8 deletions

View file

@ -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

View file

@ -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]:

View file

@ -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('</materials>\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

View file

@ -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