diff --git a/openmc/bounding_box.py b/openmc/bounding_box.py index 6e58ca8ba02..5f3d3a6cfdb 100644 --- a/openmc/bounding_box.py +++ b/openmc/bounding_box.py @@ -1,5 +1,5 @@ from __future__ import annotations -from typing import Iterable +from collections.abc import Iterable import numpy as np diff --git a/openmc/checkvalue.py b/openmc/checkvalue.py index 42df3e5efbe..4fa205b14f7 100644 --- a/openmc/checkvalue.py +++ b/openmc/checkvalue.py @@ -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): diff --git a/openmc/data/data.py b/openmc/data/data.py index 7caf31e26ee..d94d6aaaa39 100644 --- a/openmc/data/data.py +++ b/openmc/data/data.py @@ -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), @@ -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): diff --git a/openmc/deplete/independent_operator.py b/openmc/deplete/independent_operator.py index 250b94deb42..759abde1308 100644 --- a/openmc/deplete/independent_operator.py +++ b/openmc/deplete/independent_operator.py @@ -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 @@ -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) diff --git a/openmc/deplete/microxs.py b/openmc/deplete/microxs.py index 253ba8c2a17..46ad2347e90 100644 --- a/openmc/deplete/microxs.py +++ b/openmc/deplete/microxs.py @@ -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 @@ -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 @@ -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( diff --git a/openmc/deplete/openmc_operator.py b/openmc/deplete/openmc_operator.py index 75f0925c4e7..c0ff568bc27 100644 --- a/openmc/deplete/openmc_operator.py +++ b/openmc/deplete/openmc_operator.py @@ -7,7 +7,6 @@ from abc import abstractmethod from warnings import warn -from typing import List, Tuple, Dict import numpy as np @@ -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 diff --git a/openmc/deplete/reaction_rates.py b/openmc/deplete/reaction_rates.py index 4aaf829a9a5..714d9048b47 100644 --- a/openmc/deplete/reaction_rates.py +++ b/openmc/deplete/reaction_rates.py @@ -2,7 +2,6 @@ An ndarray to store reaction rates with string, integer, or slice indexing. """ -from typing import Dict import numpy as np @@ -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 diff --git a/openmc/deplete/results.py b/openmc/deplete/results.py index 7551379c64c..f897a88422c 100644 --- a/openmc/deplete/results.py +++ b/openmc/deplete/results.py @@ -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 @@ -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 @@ -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 @@ -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 @@ -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. @@ -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 @@ -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 @@ -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 @@ -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) diff --git a/openmc/geometry.py b/openmc/geometry.py index f9b418ec1f7..6cce4c18c70 100644 --- a/openmc/geometry.py +++ b/openmc/geometry.py @@ -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 @@ -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 ): @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/openmc/lib/mesh.py b/openmc/lib/mesh.py index 519bf4770de..78566d449a6 100644 --- a/openmc/lib/mesh.py +++ b/openmc/lib/mesh.py @@ -1,8 +1,7 @@ -from collections.abc import Mapping +from collections.abc import Mapping, Sequence 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 List, Tuple, Sequence from weakref import WeakValueDictionary import numpy as np @@ -171,7 +170,7 @@ def material_volumes( self, n_samples: int = 10_000, prn_seed: int | None = None - ) -> List[List[Tuple[Material, float]]]: + ) -> list[list[tuple[Material, float]]]: """Determine volume of materials in each mesh element .. versionadded:: 0.15.0 diff --git a/openmc/material.py b/openmc/material.py index b1e3877db6a..2bbcc9fe43a 100644 --- a/openmc/material.py +++ b/openmc/material.py @@ -5,9 +5,7 @@ from numbers import Real from pathlib import Path import re -import typing # imported separately as py3.8 requires typing.Iterable import warnings -from typing import List, Dict import lxml.etree as ET import numpy as np @@ -202,7 +200,7 @@ def depletable(self, depletable: bool): self._depletable = depletable @property - def paths(self) -> List[str]: + def paths(self) -> list[str]: if self._paths is None: raise ValueError('Material instance paths have not been determined. ' 'Call the Geometry.determine_paths() method.') @@ -217,15 +215,15 @@ def num_instances(self) -> int: return self._num_instances @property - def nuclides(self) -> List[namedtuple]: + def nuclides(self) -> list[namedtuple]: return self._nuclides @property - def isotropic(self) -> List[str]: + def isotropic(self) -> list[str]: return self._isotropic @isotropic.setter - def isotropic(self, isotropic: typing.Iterable[str]): + def isotropic(self, isotropic: Iterable[str]): cv.check_iterable_type('Isotropic scattering nuclides', isotropic, str) self._isotropic = list(isotropic) @@ -930,7 +928,7 @@ def add_s_alpha_beta(self, name: str, fraction: float = 1.0): def make_isotropic_in_lab(self): self.isotropic = [x.name for x in self._nuclides] - def get_elements(self) -> List[str]: + def get_elements(self) -> list[str]: """Returns all elements in the material .. versionadded:: 0.12 @@ -944,7 +942,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: str | None = 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. @@ -974,7 +972,7 @@ def get_nuclides(self, element: str | None = None) -> List[str]: return matching_nuclides - def get_nuclide_densities(self) -> Dict[str, tuple]: + def get_nuclide_densities(self) -> dict[str, tuple]: """Returns all nuclides in the material and their densities Returns @@ -992,7 +990,7 @@ def get_nuclide_densities(self) -> Dict[str, tuple]: return nuclides - def get_nuclide_atom_densities(self, nuclide: str | None = 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 @@ -1078,7 +1076,7 @@ def get_nuclide_atom_densities(self, nuclide: str | None = None) -> Dict[str, fl return nuclides def get_activity(self, units: str = 'Bq/cm3', by_nuclide: bool = False, - volume: float | None = None) -> 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]. @@ -1101,7 +1099,7 @@ def get_activity(self, units: str = 'Bq/cm3', by_nuclide: bool = False, Returns ------- - typing.Union[dict, float] + Union[dict, float] If by_nuclide is True then a dictionary whose keys are nuclide names and values are activity is returned. Otherwise the activity of the material is returned as a float. @@ -1125,7 +1123,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: float | None = None) -> 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]. @@ -1173,7 +1171,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: float | None = 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 @@ -1311,8 +1309,8 @@ def _get_macroscopic_xml(self, macroscopic: str) -> ET.Element: return xml_element def _get_nuclides_xml( - self, nuclides: typing.Iterable[NuclideTuple], - nuclides_to_ignore: Iterable[str] | None = None)-> List[ET.Element]: + self, nuclides: Iterable[NuclideTuple], + nuclides_to_ignore: Iterable[str] | None = None)-> list[ET.Element]: xml_elements = [] # Remove any nuclides to ignore from the XML export @@ -1398,7 +1396,7 @@ def to_xml_element( return element @classmethod - def mix_materials(cls, materials, fracs: typing.Iterable[float], + def mix_materials(cls, materials, fracs: Iterable[float], percent_type: str = 'ao', name: str | None = None) -> Material: """Mix materials together based on atom, weight, or volume fractions diff --git a/openmc/mesh.py b/openmc/mesh.py index 19555ae57c4..644f01fa72f 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -1,14 +1,12 @@ from __future__ import annotations -import typing import warnings from abc import ABC, abstractmethod -from collections.abc import Iterable +from collections.abc import Iterable, Sequence from functools import wraps from math import pi, sqrt, atan2 from numbers import Integral, Real from pathlib import Path import tempfile -from typing import Optional, Sequence, Tuple, List import h5py import lxml.etree as ET @@ -154,7 +152,7 @@ def get_homogenized_materials( prn_seed: int | None = None, include_void: bool = True, **kwargs - ) -> List[openmc.Material]: + ) -> list[openmc.Material]: """Generate homogenized materials over each element in a mesh. .. versionadded:: 0.15.0 @@ -592,7 +590,7 @@ def _check_vtk_datasets(self, datasets: dict): """ for label, dataset in datasets.items(): - errmsg = ( + errmsg = (ict f"The size of the dataset '{label}' ({dataset.size}) should be" f" equal to the number of mesh cells ({self.num_mesh_cells})" ) @@ -655,7 +653,7 @@ def dimension(self): return tuple(self._dimension) @dimension.setter - def dimension(self, dimension: typing.Iterable[int]): + def dimension(self, dimension: Iterable[int]): cv.check_type('mesh dimension', dimension, Iterable, Integral) cv.check_length('mesh dimension', dimension, 1, 3) self._dimension = dimension @@ -672,7 +670,7 @@ def lower_left(self): return self._lower_left @lower_left.setter - def lower_left(self, lower_left: typing.Iterable[Real]): + def lower_left(self, lower_left: Iterable[Real]): cv.check_type('mesh lower_left', lower_left, Iterable, Real) cv.check_length('mesh lower_left', lower_left, 1, 3) self._lower_left = lower_left @@ -692,7 +690,7 @@ def upper_right(self): return [l + w * d for l, w, d in zip(ls, ws, dims)] @upper_right.setter - def upper_right(self, upper_right: typing.Iterable[Real]): + def upper_right(self, upper_right: Iterable[Real]): cv.check_type('mesh upper_right', upper_right, Iterable, Real) cv.check_length('mesh upper_right', upper_right, 1, 3) self._upper_right = upper_right @@ -716,7 +714,7 @@ def width(self): return [(u - l) / d for u, l, d in zip(us, ls, dims)] @width.setter - def width(self, width: typing.Iterable[Real]): + def width(self, width: Iterable[Real]): cv.check_type('mesh width', width, Iterable, Real) cv.check_length('mesh width', width, 1, 3) self._width = width @@ -1484,7 +1482,7 @@ def __repr__(self): def get_indices_at_coords( self, coords: Sequence[float] - ) -> Tuple[int, int, int]: + ) -> tuple[int, int, int]: """Finds the index of the mesh voxel at the specified x,y,z coordinates. .. versionadded:: 0.15.0 @@ -1496,7 +1494,7 @@ def get_indices_at_coords( Returns ------- - Tuple[int, int, int] + tuple[int, int, int] The r, phi, z indices """ @@ -1562,7 +1560,7 @@ def from_hdf5(cls, group: h5py.Group): @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: int | None = None, phi_grid_bounds: Sequence[float] = (0.0, 2*pi), @@ -2215,7 +2213,7 @@ def volumes(self): return self._volumes @volumes.setter - def volumes(self, volumes: typing.Iterable[Real]): + def volumes(self, volumes: Iterable[Real]): cv.check_type("Unstructured mesh volumes", volumes, Iterable, Real) self._volumes = volumes diff --git a/openmc/model/model.py b/openmc/model/model.py index 2160c97e6cc..2ea579ab7df 100644 --- a/openmc/model/model.py +++ b/openmc/model/model.py @@ -6,7 +6,6 @@ from numbers import Integral from tempfile import NamedTemporaryFile import warnings -from typing import Optional, Dict import h5py import lxml.etree as ET @@ -83,7 +82,7 @@ def __init__(self, geometry=None, materials=None, settings=None, self.plots = plots @property - def geometry(self) -> Optional[openmc.Geometry]: + def geometry(self) -> openmc.Geometry | None: return self._geometry @geometry.setter @@ -92,7 +91,7 @@ def geometry(self, geometry): self._geometry = geometry @property - def materials(self) -> Optional[openmc.Materials]: + def materials(self) -> openmc.Materials | None: return self._materials @materials.setter @@ -106,7 +105,7 @@ def materials(self, materials): self._materials.append(mat) @property - def settings(self) -> Optional[openmc.Settings]: + def settings(self) -> openmc.Settings | None: return self._settings @settings.setter @@ -115,7 +114,7 @@ def settings(self, settings): self._settings = settings @property - def tallies(self) -> Optional[openmc.Tallies]: + def tallies(self) -> openmc.Tallies | None: return self._tallies @tallies.setter @@ -129,7 +128,7 @@ def tallies(self, tallies): self._tallies.append(tally) @property - def plots(self) -> Optional[openmc.Plots]: + def plots(self) -> openmc.Plots | None: return self._plots @plots.setter @@ -169,7 +168,7 @@ def _cells_by_id(self) -> dict: @property @lru_cache(maxsize=None) - def _cells_by_name(self) -> Dict[int, openmc.Cell]: + def _cells_by_name(self) -> dict[int, openmc.Cell]: # Get the names maps, but since names are not unique, store a set for # each name key. In this way when the user requests a change by a name, # the change will be applied to all of the same name. @@ -182,7 +181,7 @@ def _cells_by_name(self) -> Dict[int, openmc.Cell]: @property @lru_cache(maxsize=None) - def _materials_by_name(self) -> Dict[int, openmc.Material]: + def _materials_by_name(self) -> dict[int, openmc.Material]: if self.materials is None: mats = self.geometry.get_all_materials().values() else: diff --git a/openmc/model/surface_composite.py b/openmc/model/surface_composite.py index 2945f73b71e..29411fe4608 100644 --- a/openmc/model/surface_composite.py +++ b/openmc/model/surface_composite.py @@ -1,12 +1,11 @@ from abc import ABC, abstractmethod -from collections.abc import Iterable +from collections.abc import Iterable, Sequence from copy import copy from functools import partial from math import sqrt, pi, sin, cos, isclose from numbers import Real import warnings import operator -from typing import Sequence import numpy as np from scipy.spatial import ConvexHull, Delaunay diff --git a/openmc/plots.py b/openmc/plots.py index 6a2b68332bd..7532d9d5cb1 100644 --- a/openmc/plots.py +++ b/openmc/plots.py @@ -1,7 +1,6 @@ from collections.abc import Iterable, Mapping from numbers import Integral, Real from pathlib import Path -from typing import Optional import h5py import lxml.etree as ET diff --git a/openmc/settings.py b/openmc/settings.py index 78821a3abd1..1f22347502f 100644 --- a/openmc/settings.py +++ b/openmc/settings.py @@ -4,7 +4,6 @@ from math import ceil from numbers import Integral, Real from pathlib import Path -import typing # required to prevent typing.Union namespace overwriting Union import lxml.etree as ET @@ -518,11 +517,11 @@ def max_order(self, max_order: int | None): self._max_order = max_order @property - def source(self) -> typing.List[SourceBase]: + def source(self) -> list[SourceBase]: return self._source @source.setter - def source(self, source: typing.Union[SourceBase, typing.Iterable[SourceBase]]): + def source(self, source: SourceBase | Iterable[SourceBase]): if not isinstance(source, MutableSequence): source = [source] self._source = cv.CheckedList(SourceBase, 'source distributions', source) @@ -800,7 +799,7 @@ def temperature(self, temperature: dict): self._temperature = temperature @property - def trace(self) -> typing.Iterable: + def trace(self) -> Iterable: return self._trace @trace.setter @@ -813,11 +812,11 @@ def trace(self, trace: Iterable): self._trace = trace @property - def track(self) -> typing.Iterable[typing.Iterable[int]]: + def track(self) -> Iterable[Iterable[int]]: return self._track @track.setter - def track(self, track: typing.Iterable[typing.Iterable[int]]): + def track(self, track: Iterable[Iterable[int]]): cv.check_type('track', track, Sequence) for t in track: if len(t) != 3: @@ -900,12 +899,12 @@ def resonance_scattering(self, res: dict): self._resonance_scattering = res @property - def volume_calculations(self) -> typing.List[VolumeCalculation]: + def volume_calculations(self) -> list[VolumeCalculation]: return self._volume_calculations @volume_calculations.setter def volume_calculations( - self, vol_calcs: typing.Union[VolumeCalculation, typing.Iterable[VolumeCalculation]] + self, vol_calcs: VolumeCalculation | Iterable[VolumeCalculation] ): if not isinstance(vol_calcs, MutableSequence): vol_calcs = [vol_calcs] @@ -999,11 +998,11 @@ def write_initial_source(self, value: bool): self._write_initial_source = value @property - def weight_windows(self) -> typing.List[WeightWindows]: + def weight_windows(self) -> list[WeightWindows]: return self._weight_windows @weight_windows.setter - def weight_windows(self, value: typing.Union[WeightWindows, typing.Iterable[WeightWindows]]): + def weight_windows(self, value: WeightWindows | Iterable[WeightWindows]): if not isinstance(value, MutableSequence): value = [value] self._weight_windows = cv.CheckedList(WeightWindows, 'weight windows', value) @@ -1061,7 +1060,7 @@ def weight_windows_file(self, value: PathLike): self._weight_windows_file = value @property - def weight_window_generators(self) -> typing.List[WeightWindowGenerator]: + def weight_window_generators(self) -> list[WeightWindowGenerator]: return self._weight_window_generators @weight_window_generators.setter diff --git a/openmc/source.py b/openmc/source.py index 21dc305f348..e35e62f5fca 100644 --- a/openmc/source.py +++ b/openmc/source.py @@ -1,12 +1,10 @@ from __future__ import annotations from abc import ABC, abstractmethod -from collections.abc import Iterable +from collections.abc import Iterable, Sequence from enum import IntEnum from numbers import Real import warnings -import typing # imported separately as py3.8 requires typing.Iterable -# also required to prevent typing.Union namespace overwriting Union -from typing import Sequence, Dict, Any +from typing import Any import lxml.etree as ET import numpy as np @@ -58,7 +56,7 @@ class SourceBase(ABC): def __init__( self, strength: float | None = 1.0, - constraints: Dict[str, Any] | None = None + constraints: dict[str, Any] | None = None ): self.strength = strength self.constraints = constraints @@ -75,11 +73,11 @@ def strength(self, strength): self._strength = strength @property - def constraints(self) -> Dict[str, Any]: + def constraints(self) -> dict[str, Any]: return self._constraints @constraints.setter - def constraints(self, constraints: Dict[str, Any] | None): + def constraints(self, constraints: dict[str, Any] | None): self._constraints = {} if constraints is None: return @@ -200,7 +198,7 @@ def from_xml_element(cls, elem: ET.Element, meshes=None) -> SourceBase: raise ValueError(f'Source type {source_type} is not recognized') @staticmethod - def _get_constraints(elem: ET.Element) -> Dict[str, Any]: + def _get_constraints(elem: ET.Element) -> dict[str, Any]: # Find element containing constraints constraints_elem = elem.find("constraints") elem = constraints_elem if constraints_elem is not None else elem @@ -315,7 +313,7 @@ def __init__( strength: float = 1.0, particle: str = 'neutron', domains: Sequence[openmc.Cell | openmc.Material | openmc.Universe] | None = None, - constraints: Dict[str, Any] | None = None + constraints: dict[str, Any] | None = None ): if domains is not None: warnings.warn("The 'domains' arguments has been replaced by the " @@ -528,7 +526,7 @@ def __init__( self, mesh: MeshBase, sources: Sequence[SourceBase], - constraints: Dict[str, Any] | None = None, + constraints: dict[str, Any] | None = None, ): super().__init__(strength=None, constraints=constraints) self.mesh = mesh @@ -705,7 +703,7 @@ def __init__( library: str | None = None, parameters: str | None = None, strength: float = 1.0, - constraints: Dict[str, Any] | None = None + constraints: dict[str, Any] | None = None ) -> None: super().__init__(strength=strength, constraints=constraints) @@ -831,7 +829,7 @@ def __init__( self, path: PathLike | None = None, strength: float = 1.0, - constraints: Dict[str, Any] | None = None + constraints: dict[str, Any] | None = None ): super().__init__(strength=strength, constraints=constraints) self._path = None @@ -966,8 +964,8 @@ class SourceParticle: """ def __init__( self, - r: typing.Iterable[float] = (0., 0., 0.), - u: typing.Iterable[float] = (0., 0., 1.), + r: Iterable[float] = (0., 0., 0.), + u: Iterable[float] = (0., 0., 1.), E: float = 1.0e6, time: float = 0.0, wgt: float = 1.0, @@ -1003,7 +1001,7 @@ def to_tuple(self) -> tuple: def write_source_file( - source_particles: typing.Iterable[SourceParticle], + source_particles: Iterable[SourceParticle], filename: PathLike, **kwargs ): """Write a source file using a collection of source particles @@ -1046,7 +1044,7 @@ def write_source_file( fh.create_dataset('source_bank', data=arr, dtype=source_dtype) -def read_source_file(filename: PathLike) -> typing.List[SourceParticle]: +def read_source_file(filename: PathLike) -> list[SourceParticle]: """Read a source file and return a list of source particles. .. versionadded:: 0.15.0 diff --git a/openmc/stats/multivariate.py b/openmc/stats/multivariate.py index ce9740ad212..06c65896f49 100644 --- a/openmc/stats/multivariate.py +++ b/openmc/stats/multivariate.py @@ -1,7 +1,6 @@ from __future__ import annotations -import typing from abc import ABC, abstractmethod -from collections.abc import Iterable +from collections.abc import Iterable, Sequence from math import cos, pi from numbers import Real from warnings import warn @@ -9,6 +8,7 @@ import lxml.etree as ET import numpy as np +import openmc import openmc.checkvalue as cv from .._xml import get_text from ..mesh import MeshBase @@ -212,7 +212,7 @@ class Monodirectional(UnitSphere): """ - def __init__(self, reference_uvw: typing.Sequence[float] = [1., 0., 0.]): + def __init__(self, reference_uvw: Sequence[float] = [1., 0., 0.]): super().__init__(reference_uvw) def to_xml_element(self): @@ -789,8 +789,8 @@ class Box(Spatial): def __init__( self, - lower_left: typing.Sequence[float], - upper_right: typing.Sequence[float], + lower_left: Sequence[float], + upper_right: Sequence[float], only_fissionable: bool = False ): self.lower_left = lower_left @@ -889,7 +889,7 @@ class Point(Spatial): """ - def __init__(self, xyz: typing.Sequence[float] = (0., 0., 0.)): + def __init__(self, xyz: Sequence[float] = (0., 0., 0.)): self.xyz = xyz @property @@ -939,9 +939,9 @@ def from_xml_element(cls, elem: ET.Element): def spherical_uniform( r_outer: float, r_inner: float = 0.0, - thetas: typing.Sequence[float] = (0., pi), - phis: typing.Sequence[float] = (0., 2*pi), - origin: typing.Sequence[float] = (0., 0., 0.) + thetas: Sequence[float] = (0., pi), + phis: Sequence[float] = (0., 2*pi), + origin: Sequence[float] = (0., 0., 0.) ): """Return a uniform spatial distribution over a spherical shell. diff --git a/openmc/stats/univariate.py b/openmc/stats/univariate.py index edfe10af74d..2d93b1f1b5d 100644 --- a/openmc/stats/univariate.py +++ b/openmc/stats/univariate.py @@ -1,9 +1,8 @@ from __future__ import annotations import math -import typing from abc import ABC, abstractmethod from collections import defaultdict -from collections.abc import Iterable +from collections.abc import Iterable, Sequence from copy import deepcopy from numbers import Real from warnings import warn @@ -210,8 +209,8 @@ def from_xml_element(cls, elem: ET.Element): @classmethod def merge( cls, - dists: typing.Sequence[Discrete], - probs: typing.Sequence[int] + dists: Sequence[Discrete], + probs: Sequence[int] ): """Merge multiple discrete distributions into a single distribution @@ -859,8 +858,8 @@ class Tabular(Univariate): def __init__( self, - x: typing.Sequence[float], - p: typing.Sequence[float], + x: Sequence[float], + p: Sequence[float], interpolation: str = 'linear-linear', ignore_negative: bool = False ): @@ -1100,7 +1099,7 @@ class Legendre(Univariate): """ - def __init__(self, coefficients: typing.Sequence[float]): + def __init__(self, coefficients: Sequence[float]): self.coefficients = coefficients self._legendre_poly = None @@ -1156,8 +1155,8 @@ class Mixture(Univariate): def __init__( self, - probability: typing.Sequence[float], - distribution: typing.Sequence[Univariate] + probability: Sequence[float], + distribution: Sequence[Univariate] ): self.probability = probability self.distribution = distribution @@ -1319,8 +1318,8 @@ def clip(self, tolerance: float = 1e-6, inplace: bool = False) -> Mixture: def combine_distributions( - dists: typing.Sequence[Univariate], - probs: typing.Sequence[float] + dists: Sequence[Univariate], + probs: Sequence[float] ): """Combine distributions with specified probabilities diff --git a/openmc/weight_windows.py b/openmc/weight_windows.py index ef118b7ea94..2bd442dd6da 100644 --- a/openmc/weight_windows.py +++ b/openmc/weight_windows.py @@ -1,6 +1,6 @@ from __future__ import annotations from numbers import Real, Integral -from typing import Iterable, List, Dict, Sequence +from collections.abc import Iterable, Sequence import warnings import lxml.etree as ET @@ -353,7 +353,7 @@ def to_xml_element(self) -> ET.Element: return element @classmethod - def from_xml_element(cls, elem: ET.Element, meshes: Dict[int, MeshBase]) -> WeightWindows: + def from_xml_element(cls, elem: ET.Element, meshes: dict[int, MeshBase]) -> WeightWindows: """Generate weight window settings from an XML element Parameters @@ -407,7 +407,7 @@ def from_xml_element(cls, elem: ET.Element, meshes: Dict[int, MeshBase]) -> Weig ) @classmethod - def from_hdf5(cls, group: h5py.Group, meshes: Dict[int, MeshBase]) -> WeightWindows: + def from_hdf5(cls, group: h5py.Group, meshes: dict[int, MeshBase]) -> WeightWindows: """Create weight windows from HDF5 group Parameters @@ -457,7 +457,7 @@ def from_hdf5(cls, group: h5py.Group, meshes: Dict[int, MeshBase]) -> WeightWind ) -def wwinp_to_wws(path: PathLike) -> List[WeightWindows]: +def wwinp_to_wws(path: PathLike) -> list[WeightWindows]: """Create WeightWindows instances from a wwinp file .. versionadded:: 0.13.1