Skip to content

Commit

Permalink
Replace deprecated typing class imports with native abstract base cla…
Browse files Browse the repository at this point in the history
…sses
  • Loading branch information
johvincau committed Jul 17, 2024
1 parent 8dcd8cb commit 001419a
Show file tree
Hide file tree
Showing 20 changed files with 126 additions and 145 deletions.
2 changes: 1 addition & 1 deletion openmc/bounding_box.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import annotations
from typing import Iterable
from collections.abc import Iterable

import numpy as np

Expand Down
3 changes: 1 addition & 2 deletions openmc/checkvalue.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import copy
import os
import typing # required to prevent typing.Union namespace overwriting Union
from collections.abc import Iterable

import numpy as np

# Type for arguments that accept file paths
PathLike = typing.Union[str, os.PathLike]
PathLike = str | os.PathLike


def check_type(name, value, expected_type, expected_iter_type=None, *, none_ok=False):
Expand Down
5 changes: 2 additions & 3 deletions openmc/data/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from pathlib import Path
from math import sqrt, log
from warnings import warn
from typing import Dict

# Isotopic abundances from Meija J, Coplen T B, et al, "Isotopic compositions
# of the elements 2013 (IUPAC Technical Report)", Pure. Appl. Chem. 88 (3),
Expand Down Expand Up @@ -283,13 +282,13 @@
NEUTRON_MASS = 1.00866491595

# Used in atomic_mass function as a cache
_ATOMIC_MASS: Dict[str, float] = {}
_ATOMIC_MASS: dict[str, float] = {}

# Regex for GNDS nuclide names (used in zam function)
_GNDS_NAME_RE = re.compile(r'([A-Zn][a-z]*)(\d+)((?:_[em]\d+)?)')

# Used in half_life function as a cache
_HALF_LIFE: Dict[str, float] = {}
_HALF_LIFE: dict[str, float] = {}
_LOG_TWO = log(2.0)

def atomic_mass(isotope):
Expand Down
3 changes: 1 addition & 2 deletions openmc/deplete/independent_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from __future__ import annotations
from collections.abc import Iterable
import copy
from typing import List, Set

import numpy as np
from uncertainties import ufloat
Expand Down Expand Up @@ -279,7 +278,7 @@ def _load_previous_results(self):
new_res = res_obj.distribute(self.local_mats, mat_indexes)
self.prev_res.append(new_res)

def _get_nuclides_with_data(self, cross_sections: List[MicroXS]) -> Set[str]:
def _get_nuclides_with_data(self, cross_sections: list[MicroXS]) -> set[str]:
"""Finds nuclides with cross section data"""
return set(cross_sections[0].nuclides)

Expand Down
6 changes: 3 additions & 3 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, Sequence
from typing import Iterable, Sequence

import pandas as pd
import numpy as np
Expand Down Expand Up @@ -46,7 +46,7 @@ def get_microxs_and_flux(
energies: Iterable[float] | str | None = None,
chain_file: PathLike | None = None,
run_kwargs=None
) -> Tuple[List[np.ndarray], List[MicroXS]]:
) -> tuple[list[np.ndarray], list[MicroXS]]:
"""Generate a microscopic cross sections and flux from a Model
.. versionadded:: 0.14.0
Expand Down Expand Up @@ -183,7 +183,7 @@ class MicroXS:
:data:`openmc.deplete.chain.REACTIONS`
"""
def __init__(self, data: np.ndarray, nuclides: List[str], reactions: List[str]):
def __init__(self, data: np.ndarray, nuclides: list[str], reactions: list[str]):
# Validate inputs
if data.shape[:2] != (len(nuclides), len(reactions)):
raise ValueError(
Expand Down
3 changes: 1 addition & 2 deletions openmc/deplete/openmc_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

from abc import abstractmethod
from warnings import warn
from typing import List, Tuple, Dict

import numpy as np

Expand Down Expand Up @@ -185,7 +184,7 @@ def _differentiate_burnable_mats(self):
"""Assign distribmats for each burnable material"""
pass

def _get_burnable_mats(self) -> Tuple[List[str], Dict[str, float], List[str]]:
def _get_burnable_mats(self) -> tuple[list[str], dict[str, float], list[str]]:
"""Determine depletable materials, volumes, and nuclides
Returns
Expand Down
7 changes: 3 additions & 4 deletions openmc/deplete/reaction_rates.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
An ndarray to store reaction rates with string, integer, or slice indexing.
"""
from typing import Dict

import numpy as np

Expand Down Expand Up @@ -53,9 +52,9 @@ class ReactionRates(np.ndarray):
# the __array_finalize__ method (discussed here:
# https://docs.scipy.org/doc/numpy/user/basics.subclassing.html)

index_mat: Dict[str, int]
index_nuc: Dict[str, int]
index_rx: Dict[str, int]
index_mat: dict[str, int]
index_nuc: dict[str, int]
index_rx: dict[str, int]

def __new__(cls, local_mats, nuclides, reactions, from_results=False):
# Create appropriately-sized zeroed-out ndarray
Expand Down
29 changes: 14 additions & 15 deletions openmc/deplete/results.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import numbers
import bisect
import math
import typing # required to prevent typing.Union namespace overwriting Union
from typing import Iterable, Tuple, List
from collections.abc import Iterable
from warnings import warn

import h5py
Expand Down Expand Up @@ -97,11 +96,11 @@ def from_hdf5(cls, filename: PathLike):

def get_activity(
self,
mat: typing.Union[Material, str],
mat: Material | str,
units: str = "Bq/cm3",
by_nuclide: bool = False,
volume: float | None = None
) -> Tuple[np.ndarray, typing.Union[np.ndarray, List[dict]]]:
) -> tuple[np.ndarray, np.ndarray | list[dict]]:
"""Get activity of material over time.
.. versionadded:: 0.14.0
Expand Down Expand Up @@ -152,11 +151,11 @@ def get_activity(

def get_atoms(
self,
mat: typing.Union[Material, str],
mat: Material | str,
nuc: str,
nuc_units: str = "atoms",
time_units: str = "s"
) -> Tuple[np.ndarray, np.ndarray]:
) -> tuple[np.ndarray, np.ndarray]:
"""Get number of nuclides over time from a single material
Parameters
Expand Down Expand Up @@ -215,11 +214,11 @@ def get_atoms(

def get_decay_heat(
self,
mat: typing.Union[Material, str],
mat: Material | str,
units: str = "W",
by_nuclide: bool = False,
volume: float | None = None
) -> Tuple[np.ndarray, typing.Union[np.ndarray, List[dict]]]:
) -> tuple[np.ndarray, np.ndarray | list[dict]]:
"""Get decay heat of material over time.
.. versionadded:: 0.14.0
Expand All @@ -242,7 +241,7 @@ def get_decay_heat(
-------
times : numpy.ndarray
Array of times in [s]
decay_heat : numpy.ndarray or List[dict]
decay_heat : numpy.ndarray or list[dict]
Array of total decay heat values if by_nuclide = False (default)
or list of dictionaries of decay heat values by nuclide if
by_nuclide = True.
Expand Down Expand Up @@ -270,11 +269,11 @@ def get_decay_heat(
return times, decay_heat

def get_mass(self,
mat: typing.Union[Material, str],
mat: Material | str,
nuc: str,
mass_units: str = "g",
time_units: str = "s"
) -> Tuple[np.ndarray, np.ndarray]:
) -> tuple[np.ndarray, np.ndarray]:
"""Get mass of nuclides over time from a single material
.. versionadded:: 0.14.0
Expand Down Expand Up @@ -324,10 +323,10 @@ def get_mass(self,

def get_reaction_rate(
self,
mat: typing.Union[Material, str],
mat: Material | str,
nuc: str,
rx: str
) -> Tuple[np.ndarray, np.ndarray]:
) -> tuple[np.ndarray, np.ndarray]:
"""Get reaction rate in a single material/nuclide over time
Parameters
Expand Down Expand Up @@ -364,7 +363,7 @@ def get_reaction_rate(

return times, rates

def get_keff(self, time_units: str = 's') -> Tuple[np.ndarray, np.ndarray]:
def get_keff(self, time_units: str = 's') -> tuple[np.ndarray, np.ndarray]:
"""Evaluates the eigenvalue from a results list.
.. versionadded:: 0.13.1
Expand Down Expand Up @@ -400,7 +399,7 @@ def get_keff(self, time_units: str = 's') -> Tuple[np.ndarray, np.ndarray]:
times = _get_time_as(times, time_units)
return times, eigenvalues

def get_eigenvalue(self, time_units: str = 's') -> Tuple[np.ndarray, np.ndarray]:
def get_eigenvalue(self, time_units: str = 's') -> tuple[np.ndarray, np.ndarray]:
warn("The get_eigenvalue(...) function has been renamed get_keff and "
"will be removed in a future version of OpenMC.", FutureWarning)
return self.get_keff(time_units)
Expand Down
35 changes: 17 additions & 18 deletions openmc/geometry.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations
import os
import typing
from collections import defaultdict
from copy import deepcopy
from collections.abc import Iterable
Expand Down Expand Up @@ -41,7 +40,7 @@ class Geometry:

def __init__(
self,
root: openmc.UniverseBase | typing.Iterable[openmc.Cell] | None = None,
root: openmc.UniverseBase | Iterable[openmc.Cell] | None = None,
merge_surfaces: bool = False,
surface_precision: int = 10
):
Expand Down Expand Up @@ -316,7 +315,7 @@ def find(self, point) -> list:
"""
return self.root_universe.find(point)

def get_instances(self, paths) -> typing.Union[int, typing.List[int]]:
def get_instances(self, paths) -> int | list[int]:
"""Return the instance number(s) for a cell/material in a geometry path.
The instance numbers are used as indices into distributed
Expand Down Expand Up @@ -363,7 +362,7 @@ def get_instances(self, paths) -> typing.Union[int, typing.List[int]]:

return indices if return_list else indices[0]

def get_all_cells(self) -> typing.Dict[int, openmc.Cell]:
def get_all_cells(self) -> dict[int, openmc.Cell]:
"""Return all cells in the geometry.
Returns
Expand All @@ -377,7 +376,7 @@ def get_all_cells(self) -> typing.Dict[int, openmc.Cell]:
else:
return {}

def get_all_universes(self) -> typing.Dict[int, openmc.Universe]:
def get_all_universes(self) -> dict[int, openmc.Universe]:
"""Return all universes in the geometry.
Returns
Expand All @@ -392,7 +391,7 @@ def get_all_universes(self) -> typing.Dict[int, openmc.Universe]:
universes.update(self.root_universe.get_all_universes())
return universes

def get_all_nuclides(self) -> typing.List[str]:
def get_all_nuclides(self) -> list[str]:
"""Return all nuclides within the geometry.
Returns
Expand All @@ -406,7 +405,7 @@ def get_all_nuclides(self) -> typing.List[str]:
all_nuclides |= set(material.get_nuclides())
return sorted(all_nuclides)

def get_all_materials(self) -> typing.Dict[int, openmc.Material]:
def get_all_materials(self) -> dict[int, openmc.Material]:
"""Return all materials within the geometry.
Returns
Expand All @@ -421,7 +420,7 @@ def get_all_materials(self) -> typing.Dict[int, openmc.Material]:
else:
return {}

def get_all_material_cells(self) -> typing.Dict[int, openmc.Cell]:
def get_all_material_cells(self) -> dict[int, openmc.Cell]:
"""Return all cells filled by a material
Returns
Expand All @@ -440,7 +439,7 @@ def get_all_material_cells(self) -> typing.Dict[int, openmc.Cell]:

return material_cells

def get_all_material_universes(self) -> typing.Dict[int, openmc.Universe]:
def get_all_material_universes(self) -> dict[int, openmc.Universe]:
"""Return all universes having at least one material-filled cell.
This method can be used to find universes that have at least one cell
Expand All @@ -463,7 +462,7 @@ def get_all_material_universes(self) -> typing.Dict[int, openmc.Universe]:

return material_universes

def get_all_lattices(self) -> typing.Dict[int, openmc.Lattice]:
def get_all_lattices(self) -> dict[int, openmc.Lattice]:
"""Return all lattices defined
Returns
Expand All @@ -481,7 +480,7 @@ def get_all_lattices(self) -> typing.Dict[int, openmc.Lattice]:

return lattices

def get_all_surfaces(self) -> typing.Dict[int, openmc.Surface]:
def get_all_surfaces(self) -> dict[int, openmc.Surface]:
"""
Return all surfaces used in the geometry
Expand Down Expand Up @@ -517,7 +516,7 @@ def _get_domains_by_name(self, name, case_sensitive, matching, domain_type) -> l

def get_materials_by_name(
self, name, case_sensitive=False, matching=False
) -> typing.List[openmc.Material]:
) -> list[openmc.Material]:
"""Return a list of materials with matching names.
Parameters
Expand All @@ -540,7 +539,7 @@ def get_materials_by_name(

def get_cells_by_name(
self, name, case_sensitive=False, matching=False
) -> typing.List[openmc.Cell]:
) -> list[openmc.Cell]:
"""Return a list of cells with matching names.
Parameters
Expand All @@ -563,7 +562,7 @@ def get_cells_by_name(

def get_surfaces_by_name(
self, name, case_sensitive=False, matching=False
) -> typing.List[openmc.Surface]:
) -> list[openmc.Surface]:
"""Return a list of surfaces with matching names.
.. versionadded:: 0.13.3
Expand All @@ -588,7 +587,7 @@ def get_surfaces_by_name(

def get_cells_by_fill_name(
self, name, case_sensitive=False, matching=False
) -> typing.List[openmc.Cell]:
) -> list[openmc.Cell]:
"""Return a list of cells with fills with matching names.
Parameters
Expand Down Expand Up @@ -635,7 +634,7 @@ def get_cells_by_fill_name(

def get_universes_by_name(
self, name, case_sensitive=False, matching=False
) -> typing.List[openmc.Universe]:
) -> list[openmc.Universe]:
"""Return a list of universes with matching names.
Parameters
Expand All @@ -658,7 +657,7 @@ def get_universes_by_name(

def get_lattices_by_name(
self, name, case_sensitive=False, matching=False
) -> typing.List[openmc.Lattice]:
) -> list[openmc.Lattice]:
"""Return a list of lattices with matching names.
Parameters
Expand All @@ -679,7 +678,7 @@ def get_lattices_by_name(
"""
return self._get_domains_by_name(name, case_sensitive, matching, 'lattice')

def remove_redundant_surfaces(self) -> typing.Dict[int, openmc.Surface]:
def remove_redundant_surfaces(self) -> dict[int, openmc.Surface]:
"""Remove and return all of the redundant surfaces.
Uses surface_precision attribute of Geometry instance for rounding and
Expand Down
Loading

0 comments on commit 001419a

Please sign in to comment.