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 8dcd8cb
Show file tree
Hide file tree
Showing 14 changed files with 96 additions and 100 deletions.
3 changes: 1 addition & 2 deletions openmc/data/decay.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from io import StringIO
from math import log
import re
from typing import Optional
from warnings import warn

import numpy as np
Expand Down Expand Up @@ -579,7 +578,7 @@ def sources(self):
_DECAY_PHOTON_ENERGY = {}


def decay_photon_energy(nuclide: str) -> Optional[Univariate]:
def decay_photon_energy(nuclide: str) -> Univariate | None:
"""Get photon energy distribution resulting from the decay of a nuclide
This function relies on data stored in a depletion chain. Before calling it
Expand Down
3 changes: 1 addition & 2 deletions openmc/deplete/coupled_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import copy
from warnings import warn
from typing import Optional

import numpy as np
from uncertainties import ufloat
Expand All @@ -34,7 +33,7 @@
__all__ = ["CoupledOperator", "Operator", "OperatorResult"]


def _find_cross_sections(model: Optional[str] = None):
def _find_cross_sections(model: str | None = None):
"""Determine cross sections to use for depletion
Parameters
Expand Down
18 changes: 9 additions & 9 deletions openmc/deplete/microxs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from __future__ import annotations
from tempfile import TemporaryDirectory
from typing import List, Tuple, Iterable, Optional, Union, Sequence
from typing import List, Tuple, Iterable, Sequence

import pandas as pd
import numpy as np
Expand Down Expand Up @@ -41,10 +41,10 @@ def _resolve_chain_file_path(chain_file: str):
def get_microxs_and_flux(
model: openmc.Model,
domains,
nuclides: Optional[Iterable[str]] = None,
reactions: Optional[Iterable[str]] = None,
energies: Optional[Union[Iterable[float], str]] = None,
chain_file: Optional[PathLike] = None,
nuclides: Iterable[str] | None = None,
reactions: Iterable[str] | None = None,
energies: Iterable[float] | str | None = None,
chain_file: PathLike | None = None,
run_kwargs=None
) -> Tuple[List[np.ndarray], List[MicroXS]]:
"""Generate a microscopic cross sections and flux from a Model
Expand Down Expand Up @@ -205,12 +205,12 @@ def __init__(self, data: np.ndarray, nuclides: List[str], reactions: List[str]):
@classmethod
def from_multigroup_flux(
cls,
energies: Union[Sequence[float], str],
energies: Sequence[float] | str,
multigroup_flux: Sequence[float],
chain_file: Optional[PathLike] = None,
chain_file: PathLike | None = None,
temperature: float = 293.6,
nuclides: Optional[Sequence[str]] = None,
reactions: Optional[Sequence[str]] = None,
nuclides: Sequence[str] | None = None,
reactions: Sequence[str] | None = None,
**init_kwargs: dict,
) -> MicroXS:
"""Generated microscopic cross sections from a known flux.
Expand Down
8 changes: 4 additions & 4 deletions openmc/deplete/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import bisect
import math
import typing # required to prevent typing.Union namespace overwriting Union
from typing import Iterable, Optional, Tuple, List
from typing import Iterable, Tuple, List
from warnings import warn

import h5py
Expand Down Expand Up @@ -100,7 +100,7 @@ def get_activity(
mat: typing.Union[Material, str],
units: str = "Bq/cm3",
by_nuclide: bool = False,
volume: Optional[float] = None
volume: float | None = None
) -> Tuple[np.ndarray, typing.Union[np.ndarray, List[dict]]]:
"""Get activity of material over time.
Expand Down Expand Up @@ -218,7 +218,7 @@ def get_decay_heat(
mat: typing.Union[Material, str],
units: str = "W",
by_nuclide: bool = False,
volume: Optional[float] = None
volume: float | None = None
) -> Tuple[np.ndarray, typing.Union[np.ndarray, List[dict]]]:
"""Get decay heat of material over time.
Expand Down Expand Up @@ -526,7 +526,7 @@ def get_step_where(
def export_to_materials(
self,
burnup_index: int,
nuc_with_data: Optional[Iterable[str]] = None,
nuc_with_data: Iterable[str] | None = None,
path: PathLike = 'materials.xml'
) -> Materials:
"""Return openmc.Materials object based on results at a given step
Expand Down
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
4 changes: 2 additions & 2 deletions openmc/lib/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from ctypes import (c_int, c_int32, c_char_p, c_double, POINTER, Structure,
create_string_buffer, c_uint64, c_size_t)
from random import getrandbits
from typing import Optional, List, Tuple, Sequence
from typing import List, Tuple, Sequence
from weakref import WeakValueDictionary

import numpy as np
Expand Down Expand Up @@ -170,7 +170,7 @@ def volumes(self) -> np.ndarray:
def material_volumes(
self,
n_samples: int = 10_000,
prn_seed: Optional[int] = None
prn_seed: int | None = None
) -> List[List[Tuple[Material, float]]]:
"""Determine volume of materials in each mesh element
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
Loading

0 comments on commit 8dcd8cb

Please sign in to comment.