From 3623408bc54baad55d8429a178dd408fc89e1115 Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Thu, 27 Oct 2022 17:38:27 +0100 Subject: [PATCH] avoid namespace overwrite of union --- openmc/checkvalue.py | 4 ++-- openmc/deplete/results.py | 7 ++++--- openmc/material.py | 4 ++-- openmc/settings.py | 9 +++++---- openmc/source.py | 1 + 5 files changed, 14 insertions(+), 11 deletions(-) diff --git a/openmc/checkvalue.py b/openmc/checkvalue.py index c5d80b2fcb..840306486f 100644 --- a/openmc/checkvalue.py +++ b/openmc/checkvalue.py @@ -1,12 +1,12 @@ import copy import os -from typing import Union +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 = Union[str, os.PathLike] +PathLike = typing.Union[str, os.PathLike] def check_type(name, value, expected_type, expected_iter_type=None, *, none_ok=False): diff --git a/openmc/deplete/results.py b/openmc/deplete/results.py index d9aa52b79e..9552a6ba61 100644 --- a/openmc/deplete/results.py +++ b/openmc/deplete/results.py @@ -1,7 +1,8 @@ import numbers import bisect import math -from typing import Iterable, Optional, Tuple, Union +import typing # required to prevent typing.Union namespace overwriting Union +from typing import Iterable, Optional, Tuple from warnings import warn import h5py @@ -95,7 +96,7 @@ class Results(list): def get_atoms( self, - mat: Union[Material, str], + mat: typing.Union[Material, str], nuc: str, nuc_units: str = "atoms", time_units: str = "s" @@ -165,7 +166,7 @@ class Results(list): def get_reaction_rate( self, - mat: Union[Material, str], + mat: typing.Union[Material, str], nuc: str, rx: str ) -> Tuple[np.ndarray, np.ndarray]: diff --git a/openmc/material.py b/openmc/material.py index 1979b39588..10ade8bfe4 100644 --- a/openmc/material.py +++ b/openmc/material.py @@ -6,7 +6,7 @@ from pathlib import Path import re import typing # imported separately as py3.8 requires typing.Iterable import warnings -from typing import Optional, Union +from typing import Optional from xml.etree import ElementTree as ET import h5py @@ -968,7 +968,7 @@ class Material(IDManagerMixin): Returns ------- - Union[dict, float] + typing.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. diff --git a/openmc/settings.py b/openmc/settings.py index ad5a9b28ae..45658f8127 100644 --- a/openmc/settings.py +++ b/openmc/settings.py @@ -6,7 +6,8 @@ import itertools from math import ceil from numbers import Integral, Real from pathlib import Path -from typing import Optional, Union +import typing # required to prevent typing.Union namespace overwriting Union +from typing import Optional from xml.etree import ElementTree as ET import openmc.checkvalue as cv @@ -582,7 +583,7 @@ class Settings: self._max_order = max_order @source.setter - def source(self, source: Union[Source, typing.Iterable[Source]]): + def source(self, source: typing.Union[Source, typing.Iterable[Source]]): if not isinstance(source, MutableSequence): source = [source] self._source = cv.CheckedList(Source, 'source distributions', source) @@ -843,7 +844,7 @@ class Settings: @volume_calculations.setter def volume_calculations( - self, vol_calcs: Union[VolumeCalculation, typing.Iterable[VolumeCalculation]] + self, vol_calcs: typing.Union[VolumeCalculation, typing.Iterable[VolumeCalculation]] ): if not isinstance(vol_calcs, MutableSequence): vol_calcs = [vol_calcs] @@ -889,7 +890,7 @@ class Settings: self._write_initial_source = value @weight_windows.setter - def weight_windows(self, value: Union[WeightWindows, typing.Iterable[WeightWindows]]): + def weight_windows(self, value: typing.Union[WeightWindows, typing.Iterable[WeightWindows]]): if not isinstance(value, MutableSequence): value = [value] self._weight_windows = cv.CheckedList(WeightWindows, 'weight windows', value) diff --git a/openmc/source.py b/openmc/source.py index a5907344e7..d886e98910 100644 --- a/openmc/source.py +++ b/openmc/source.py @@ -3,6 +3,7 @@ from enum import Enum 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 Optional from xml.etree import ElementTree as ET