Skip to content

Commit

Permalink
Update type enforcement to 3.10+ syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
johvincau committed Jul 16, 2024
1 parent 2107af5 commit 65b5917
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 79 deletions.
2 changes: 1 addition & 1 deletion openmc/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def get_universe(univ_id):
def from_xml(
cls,
path: PathLike = 'geometry.xml',
materials: typing.Optional[typing.Union[PathLike, 'openmc.Materials']] = 'materials.xml'
materials: PathLike | 'openmc.Materials' | None = 'materials.xml'
) -> Geometry:
"""Generate geometry from XML file
Expand Down
64 changes: 32 additions & 32 deletions openmc/material.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import re
import typing # imported separately as py3.8 requires typing.Iterable
import warnings
from typing import Optional, List, Union, Dict
from typing import List, Dict

import lxml.etree as ET
import numpy as np
Expand Down Expand Up @@ -161,11 +161,11 @@ def __repr__(self) -> str:
return string

@property
def name(self) -> Optional[str]:
def name(self) -> str | None:
return self._name

@name.setter
def name(self, name: Optional[str]):
def name(self, name: str | None):
if name is not None:
cv.check_type(f'name for Material ID="{self._id}"',
name, str)
Expand All @@ -174,17 +174,17 @@ def name(self, name: Optional[str]):
self._name = ''

@property
def temperature(self) -> Optional[float]:
def temperature(self) -> float | None:
return self._temperature

@temperature.setter
def temperature(self, temperature: Optional[Real]):
def temperature(self, temperature: Real | None):
cv.check_type(f'Temperature for Material ID="{self._id}"',
temperature, (Real, type(None)))
self._temperature = temperature

@property
def density(self) -> Optional[float]:
def density(self) -> float | None:
return self._density

@property
Expand Down Expand Up @@ -248,7 +248,7 @@ def average_molar_mass(self) -> float:
return mass / moles

@property
def volume(self) -> Optional[float]:
def volume(self) -> float | None:
return self._volume

@volume.setter
Expand All @@ -258,7 +258,7 @@ def volume(self, volume: Real):
self._volume = volume

@property
def ncrystal_cfg(self) -> Optional[str]:
def ncrystal_cfg(self) -> str | None:
return self._ncrystal_cfg

@property
Expand All @@ -274,7 +274,7 @@ def fissionable_mass(self) -> float:
return density*self.volume

@property
def decay_photon_energy(self) -> Optional[Univariate]:
def decay_photon_energy(self) -> Univariate | None:
warnings.warn(
"The 'decay_photon_energy' property has been replaced by the "
"get_decay_photon_energy() method and will be removed in a future "
Expand All @@ -285,8 +285,8 @@ def get_decay_photon_energy(
self,
clip_tolerance: float = 1e-6,
units: str = 'Bq',
volume: Optional[float] = None
) -> Optional[Univariate]:
volume: float | None = None
) -> Univariate | None:
r"""Return energy distribution of decay photons from unstable nuclides.
.. versionadded:: 0.14.0
Expand Down Expand Up @@ -471,7 +471,7 @@ def add_volume_information(self, volume_calc):
else:
raise ValueError(f'No volume information found for material ID={self.id}.')

def set_density(self, units: str, density: Optional[float] = None):
def set_density(self, units: str, density: float | None = None):
"""Set the density of the material
Parameters
Expand Down Expand Up @@ -685,10 +685,10 @@ def remove_macroscopic(self, macroscopic: str):
self._macroscopic = 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,
cross_sections: Optional[str] = None):
enrichment: float | None = None,
enrichment_target: str | None = None,
enrichment_type: str | None = None,
cross_sections: str | None = None):
"""Add a natural element to the material
Parameters
Expand Down Expand Up @@ -799,9 +799,9 @@ def add_element(self, element: str, percent: float, percent_type: str = 'ao',
self.add_nuclide(*nuclide)

def add_elements_from_formula(self, formula: str, percent_type: str = 'ao',
enrichment: Optional[float] = None,
enrichment_target: Optional[str] = None,
enrichment_type: Optional[str] = None):
enrichment: float | None = None,
enrichment_target: str | None = None,
enrichment_type: str | None = None):
"""Add a elements from a chemical formula to the material.
.. versionadded:: 0.12
Expand Down Expand Up @@ -944,7 +944,7 @@ def get_elements(self) -> List[str]:

return sorted({re.split(r'(\d+)', i)[0] for i in self.get_nuclides()})

def get_nuclides(self, element: Optional[str] = None) -> List[str]:
def get_nuclides(self, element: str | None = None) -> List[str]:
"""Returns a list of all nuclides in the material, if the element
argument is specified then just nuclides of that element are returned.
Expand Down Expand Up @@ -992,7 +992,7 @@ def get_nuclide_densities(self) -> Dict[str, tuple]:

return nuclides

def get_nuclide_atom_densities(self, nuclide: Optional[str] = None) -> Dict[str, float]:
def get_nuclide_atom_densities(self, nuclide: str | None = None) -> Dict[str, float]:
"""Returns one or all nuclides in the material and their atomic
densities in units of atom/b-cm
Expand Down Expand Up @@ -1078,7 +1078,7 @@ def get_nuclide_atom_densities(self, nuclide: Optional[str] = None) -> Dict[str,
return nuclides

def get_activity(self, units: str = 'Bq/cm3', by_nuclide: bool = False,
volume: Optional[float] = None) -> Union[Dict[str, float], float]:
volume: float | None = None) -> Dict[str, float] | float:
"""Returns the activity of the material or for each nuclide in the
material in units of [Bq], [Bq/g] or [Bq/cm3].
Expand Down Expand Up @@ -1125,7 +1125,7 @@ def get_activity(self, units: str = 'Bq/cm3', by_nuclide: bool = False,
return activity if by_nuclide else sum(activity.values())

def get_decay_heat(self, units: str = 'W', by_nuclide: bool = False,
volume: Optional[float] = None) -> Union[Dict[str, float], float]:
volume: float | None = None) -> Dict[str, float] | float:
"""Returns the decay heat of the material or for each nuclide in the
material in units of [W], [W/g] or [W/cm3].
Expand Down Expand Up @@ -1173,7 +1173,7 @@ def get_decay_heat(self, units: str = 'W', by_nuclide: bool = False,

return decayheat if by_nuclide else sum(decayheat.values())

def get_nuclide_atoms(self, volume: Optional[float] = None) -> Dict[str, float]:
def get_nuclide_atoms(self, volume: float | None = None) -> Dict[str, float]:
"""Return number of atoms of each nuclide in the material
.. versionadded:: 0.13.1
Expand Down Expand Up @@ -1202,7 +1202,7 @@ def get_nuclide_atoms(self, volume: Optional[float] = None) -> Dict[str, float]:
atoms[nuclide] = 1.0e24 * atom_per_bcm * volume
return atoms

def get_mass_density(self, nuclide: Optional[str] = None) -> float:
def get_mass_density(self, nuclide: str | None = None) -> float:
"""Return mass density of one or all nuclides
Parameters
Expand All @@ -1224,7 +1224,7 @@ def get_mass_density(self, nuclide: Optional[str] = None) -> float:
mass_density += density_i
return mass_density

def get_mass(self, nuclide: Optional[str] = None, volume: Optional[float] = None) -> float:
def get_mass(self, nuclide: str | None = None, volume: float | None = None) -> float:
"""Return mass of one or all nuclides.
Note that this method requires that the :attr:`Material.volume` has
Expand Down Expand Up @@ -1254,7 +1254,7 @@ def get_mass(self, nuclide: Optional[str] = None, volume: Optional[float] = None
raise ValueError("Volume must be set in order to determine mass.")
return volume*self.get_mass_density(nuclide)

def clone(self, memo: Optional[dict] = None) -> Material:
def clone(self, memo: dict | float = None) -> Material:
"""Create a copy of this material with a new unique ID.
Parameters
Expand Down Expand Up @@ -1312,7 +1312,7 @@ def _get_macroscopic_xml(self, macroscopic: str) -> ET.Element:

def _get_nuclides_xml(
self, nuclides: typing.Iterable[NuclideTuple],
nuclides_to_ignore: Optional[typing.Iterable[str]] = None)-> List[ET.Element]:
nuclides_to_ignore: Iterable[str] | None = None)-> List[ET.Element]:
xml_elements = []

# Remove any nuclides to ignore from the XML export
Expand All @@ -1324,7 +1324,7 @@ def _get_nuclides_xml(
return xml_elements

def to_xml_element(
self, nuclides_to_ignore: Optional[typing.Iterable[str]] = None) -> ET.Element:
self, nuclides_to_ignore: Iterable[str] | None = None) -> ET.Element:
"""Return XML representation of the material
Parameters
Expand Down Expand Up @@ -1399,7 +1399,7 @@ def to_xml_element(

@classmethod
def mix_materials(cls, materials, fracs: typing.Iterable[float],
percent_type: str = 'ao', name: Optional[str] = None) -> Material:
percent_type: str = 'ao', name: str | None = None) -> Material:
"""Mix materials together based on atom, weight, or volume fractions
.. versionadded:: 0.12
Expand Down Expand Up @@ -1596,7 +1596,7 @@ def __init__(self, materials=None):
self += materials

@property
def cross_sections(self) -> Optional[Path]:
def cross_sections(self) -> Path | None:
return self._cross_sections

@cross_sections.setter
Expand Down Expand Up @@ -1686,7 +1686,7 @@ def _write_xml(self, file, header=True, level=0, spaces_per_level=2,
file.write(indentation)

def export_to_xml(self, path: PathLike = 'materials.xml',
nuclides_to_ignore: Optional[typing.Iterable[str]] = None):
nuclides_to_ignore: Iterable[str] | None = None):
"""Export material collection to an XML file.
Parameters
Expand Down
34 changes: 17 additions & 17 deletions openmc/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class MeshBase(IDManagerMixin, ABC):
next_id = 1
used_ids = set()

def __init__(self, mesh_id: Optional[int] = None, name: str = ''):
def __init__(self, mesh_id: int | None = None, name: str = ''):
# Initialize Mesh class attributes
self.id = mesh_id
self.name = name
Expand Down Expand Up @@ -151,7 +151,7 @@ def get_homogenized_materials(
self,
model: openmc.Model,
n_samples: int = 10_000,
prn_seed: Optional[int] = None,
prn_seed: int | None = None,
include_void: bool = True,
**kwargs
) -> List[openmc.Material]:
Expand Down Expand Up @@ -393,7 +393,7 @@ def num_mesh_cells(self):

def write_data_to_vtk(self,
filename: PathLike,
datasets: Optional[dict] = None,
datasets: dict | None = None,
volume_normalization: bool = True,
curvilinear: bool = False):
"""Creates a VTK object of the mesh
Expand Down Expand Up @@ -642,7 +642,7 @@ class RegularMesh(StructuredMesh):
"""

def __init__(self, mesh_id: Optional[int] = None, name: str = ''):
def __init__(self, mesh_id: int | None = None, name: str = ''):
super().__init__(mesh_id, name)

self._dimension = None
Expand Down Expand Up @@ -815,7 +815,7 @@ def from_rect_lattice(
cls,
lattice: 'openmc.RectLattice',
division: int = 1,
mesh_id: Optional[int] = None,
mesh_id: int | None = None,
name: str = ''
):
"""Create mesh from an existing rectangular lattice
Expand Down Expand Up @@ -853,9 +853,9 @@ def from_rect_lattice(
@classmethod
def from_domain(
cls,
domain: typing.Union['openmc.Cell', 'openmc.Region', 'openmc.Universe', 'openmc.Geometry'],
domain: 'openmc.Cell' | 'openmc.Region' | 'openmc.Universe' | 'openmc.Geometry',
dimension: Sequence[int] = (10, 10, 10),
mesh_id: Optional[int] = None,
mesh_id: int | None = None,
name: str = ''
):
"""Create mesh from an existing openmc cell, region, universe or
Expand Down Expand Up @@ -962,7 +962,7 @@ def from_xml_element(cls, elem: ET.Element):

return mesh

def build_cells(self, bc: Optional[str] = None):
def build_cells(self, bc: str | None = None):
"""Generates a lattice of universes with the same dimensionality
as the mesh object. The individual cells/universes produced
will not have material definitions applied and so downstream code
Expand Down Expand Up @@ -1363,7 +1363,7 @@ def __init__(
z_grid: Sequence[float],
phi_grid: Sequence[float] = (0, 2*pi),
origin: Sequence[float] = (0., 0., 0.),
mesh_id: Optional[int] = None,
mesh_id: int | None = None,
name: str = '',
):
super().__init__(mesh_id, name)
Expand Down Expand Up @@ -1564,7 +1564,7 @@ def from_domain(
cls,
domain: typing.Union['openmc.Cell', 'openmc.Region', 'openmc.Universe', 'openmc.Geometry'],
dimension: Sequence[int] = (10, 10, 10),
mesh_id: Optional[int] = None,
mesh_id: int | None = None,
phi_grid_bounds: Sequence[float] = (0.0, 2*pi),
name: str = ''
):
Expand Down Expand Up @@ -1813,7 +1813,7 @@ def __init__(
phi_grid: Sequence[float] = (0, 2*pi),
theta_grid: Sequence[float] = (0, pi),
origin: Sequence[float] = (0., 0., 0.),
mesh_id: Optional[int] = None,
mesh_id: int | None = None,
name: str = '',
):
super().__init__(mesh_id, name)
Expand Down Expand Up @@ -2139,9 +2139,9 @@ class UnstructuredMesh(MeshBase):
_LINEAR_TET = 0
_LINEAR_HEX = 1

def __init__(self, filename: PathLike, library: str, mesh_id: Optional[int] = None,
def __init__(self, filename: PathLike, library: str, mesh_id: int | None = None,
name: str = '', length_multiplier: float = 1.0,
options: Optional[str] = None):
options: str | None = None):
super().__init__(mesh_id, name)
self.filename = filename
self._volumes = None
Expand Down Expand Up @@ -2173,11 +2173,11 @@ def library(self, lib: str):
self._library = lib

@property
def options(self) -> Optional[str]:
def options(self) -> str | None:
return self._options

@options.setter
def options(self, options: Optional[str]):
def options(self, options: str | None):
cv.check_type('options', options, (str, type(None)))
self._options = options

Expand Down Expand Up @@ -2353,8 +2353,8 @@ def write_vtk_mesh(self, **kwargs):

def write_data_to_vtk(
self,
filename: Optional[PathLike] = None,
datasets: Optional[dict] = None,
filename: PathLike | None = None,
datasets: dict | None = None,
volume_normalization: bool = True
):
"""Map data to unstructured VTK mesh elements.
Expand Down
2 changes: 1 addition & 1 deletion openmc/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ def to_ipython_image(self, openmc_exec='openmc', cwd='.'):
# Return produced image
return _get_plot_image(self, cwd)

def to_vtk(self, output: Optional[PathLike] = None,
def to_vtk(self, output: PathLike | None = None,
openmc_exec: str = 'openmc', cwd: str = '.'):
"""Render plot as an voxel image
Expand Down
Loading

0 comments on commit 65b5917

Please sign in to comment.