diff --git a/openmc/material.py b/openmc/material.py index de1b2187be..31e6746117 100644 --- a/openmc/material.py +++ b/openmc/material.py @@ -3,8 +3,11 @@ 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 +from typing import Optional, Union from xml.etree import ElementTree as ET import numpy as np @@ -206,7 +209,7 @@ class Material(IDManagerMixin): return self._volume @name.setter - def name(self, name): + def name(self, name: Optional[str]): if name is not None: cv.check_type(f'name for Material ID="{self._id}"', name, str) @@ -215,25 +218,25 @@ class Material(IDManagerMixin): self._name = '' @temperature.setter - def temperature(self, temperature): + def temperature(self, temperature: Optional[Real]): cv.check_type(f'Temperature for Material ID="{self._id}"', temperature, (Real, type(None))) self._temperature = temperature @depletable.setter - def depletable(self, depletable): + def depletable(self, depletable: bool): cv.check_type(f'Depletable flag for Material ID="{self._id}"', depletable, bool) self._depletable = depletable @volume.setter - def volume(self, volume): + def volume(self, volume: Real): if volume is not None: cv.check_type('material volume', volume, Real) self._volume = volume @isotropic.setter - def isotropic(self, isotropic): + def isotropic(self, isotropic: typing.Iterable[str]): cv.check_iterable_type('Isotropic scattering nuclides', isotropic, str) self._isotropic = list(isotropic) @@ -251,7 +254,7 @@ class Material(IDManagerMixin): return density*self.volume @classmethod - def from_hdf5(cls, group): + def from_hdf5(cls, group: str): """Create material from HDF5 group Parameters @@ -305,7 +308,7 @@ class Material(IDManagerMixin): return material - def add_volume_information(self, volume_calc): + def add_volume_information(self, volume_calc: openmc.VolumeCalculation): """Add volume information to a material. Parameters @@ -325,7 +328,7 @@ class Material(IDManagerMixin): raise ValueError('No volume information found for material ID={}.' .format(self.id)) - def set_density(self, units, density=None): + def set_density(self, units: str, density:Optional[float]=None): """Set the density of the material Parameters @@ -357,7 +360,7 @@ class Material(IDManagerMixin): density, Real) self._density = density - def add_nuclide(self, nuclide, percent, percent_type='ao'): + def add_nuclide(self, nuclide: str, percent: float, percent_type: str='ao'): """Add a nuclide to the material Parameters @@ -391,7 +394,7 @@ class Material(IDManagerMixin): self._nuclides.append(NuclideTuple(nuclide, percent, percent_type)) - def remove_nuclide(self, nuclide): + def remove_nuclide(self, nuclide: str): """Remove a nuclide from the material Parameters @@ -407,7 +410,7 @@ class Material(IDManagerMixin): if nuclide == nuc.name: self.nuclides.remove(nuc) - def add_macroscopic(self, macroscopic): + def add_macroscopic(self, macroscopic: str): """Add a macroscopic to the material. This will also set the density of the material to 1.0, unless it has been otherwise set, as a default for Macroscopic cross sections. @@ -449,7 +452,7 @@ class Material(IDManagerMixin): if self._density is None: self.set_density('macro', 1.0) - def remove_macroscopic(self, macroscopic): + def remove_macroscopic(self, macroscopic: str): """Remove a macroscopic from the material Parameters @@ -468,8 +471,10 @@ class Material(IDManagerMixin): if macroscopic == self._macroscopic: self._macroscopic = None - def add_element(self, element, percent, percent_type='ao', enrichment=None, - enrichment_target=None, enrichment_type=None): + def add_element(self, element: str, percent: float, percent_type: str='ao', + enrichment: Optional[float]=None, + enrichment_target: Optional[str]=None, + enrichment_type: Optional[str]=None): """Add a natural element to the material Parameters @@ -574,8 +579,10 @@ class Material(IDManagerMixin): enrichment_type): self.add_nuclide(*nuclide) - def add_elements_from_formula(self, formula, percent_type='ao', enrichment=None, - enrichment_target=None, enrichment_type=None): + def add_elements_from_formula(self, formula: str, percent_type: str='ao', + enrichment: Optional[float]=None, + enrichment_target: Optional[float]=None, + enrichment_type: Optional[str]=None): """Add a elements from a chemical formula to the material. .. versionadded:: 0.12 @@ -672,7 +679,7 @@ class Material(IDManagerMixin): else: self.add_element(element, percent, percent_type) - def add_s_alpha_beta(self, name, fraction=1.0): + def add_s_alpha_beta(self, name: str, fraction: float=1.0): r"""Add an :math:`S(\alpha,\beta)` table to the material Parameters @@ -821,7 +828,7 @@ class Material(IDManagerMixin): return nuclides - def get_mass_density(self, nuclide=None): + def get_mass_density(self, nuclide: Optional[str]=None): """Return mass density of one or all nuclides Parameters @@ -844,7 +851,7 @@ class Material(IDManagerMixin): mass_density += density_i return mass_density - def get_mass(self, nuclide=None): + def get_mass(self, nuclide: Optional[str]=None): """Return mass of one or all nuclides. Note that this method requires that the :attr:`Material.volume` has @@ -866,7 +873,7 @@ class Material(IDManagerMixin): raise ValueError("Volume must be set in order to determine mass.") return self.volume*self.get_mass_density(nuclide) - def clone(self, memo=None): + def clone(self, memo: Optional[dict]=None): """Create a copy of this material with a new unique ID. Parameters @@ -905,7 +912,7 @@ class Material(IDManagerMixin): return memo[self] - def _get_nuclide_xml(self, nuclide): + def _get_nuclide_xml(self, nuclide: str): xml_element = ET.Element("nuclide") xml_element.set("name", nuclide.name) @@ -916,13 +923,13 @@ class Material(IDManagerMixin): return xml_element - def _get_macroscopic_xml(self, macroscopic): + def _get_macroscopic_xml(self, macroscopic: str): xml_element = ET.Element("macroscopic") xml_element.set("name", macroscopic) return xml_element - def _get_nuclides_xml(self, nuclides): + def _get_nuclides_xml(self, nuclides: typing.Iterable[str]): xml_elements = [] for nuclide in nuclides: xml_elements.append(self._get_nuclide_xml(nuclide)) @@ -989,7 +996,9 @@ class Material(IDManagerMixin): return element @classmethod - def mix_materials(cls, materials, fracs, percent_type='ao', name=None): + def mix_materials(cls, materials: typing.Iterable[openmc.Material], + fracs: typing.Iterable[float], percent_type: str='ao', + name: Optional[str]=None): """Mix materials together based on atom, weight, or volume fractions .. versionadded:: 0.12 @@ -1087,7 +1096,7 @@ class Material(IDManagerMixin): return new_mat @classmethod - def from_xml_element(cls, elem): + def from_xml_element(cls, elem: ET.Element): """Generate material from an XML element Parameters @@ -1190,7 +1199,7 @@ class Materials(cv.CheckedList): if cross_sections is not None: self._cross_sections = Path(cross_sections) - def append(self, material): + def append(self, material: openmc.Material): """Append material to collection Parameters @@ -1201,7 +1210,7 @@ class Materials(cv.CheckedList): """ super().append(material) - def insert(self, index, material): + def insert(self, index: int, material: openmc.Material): """Insert material before index Parameters @@ -1218,7 +1227,7 @@ class Materials(cv.CheckedList): for material in self: material.make_isotropic_in_lab() - def export_to_xml(self, path='materials.xml'): + def export_to_xml(self, path: Union[str, os.PathLike]='materials.xml'): """Export material collection to an XML file. Parameters @@ -1265,12 +1274,12 @@ class Materials(cv.CheckedList): fh.write('\n') @classmethod - def from_xml(cls, path='materials.xml'): + def from_xml(cls, path: Union[str, os.PathLike]='materials.xml'): """Generate materials collection from XML file Parameters ---------- - path : str, optional + path : str Path to materials XML file Returns