Skip to content

Commit

Permalink
replace typing.Optional and typing.Union with shorthand syntax availa…
Browse files Browse the repository at this point in the history
…ble from 3.10
  • Loading branch information
JostMigenda committed Jun 18, 2024
1 parent 13ee3aa commit 58c313e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
3 changes: 1 addition & 2 deletions python/snewpy/_model_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from importlib.resources import open_text
from pathlib import Path
from tqdm.auto import tqdm
from typing import Optional

from snewpy import model_path
from snewpy import __version__ as snewpy_version
Expand Down Expand Up @@ -79,7 +78,7 @@ class FileHandle:

path: Path
remote: str = None
md5: Optional[str] = None
md5: str | None = None

def check(self) -> None:
"""Check if the given file exists locally and has a correct md5 sum.
Expand Down
19 changes: 9 additions & 10 deletions python/snewpy/flux.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
.. autoclass:: Axes
"""
from typing import Union, Optional
#from snewpy.neutrino import Flavor
from snewpy.flavor import FlavorScheme, FlavorMatrix
from astropy import units as u
Expand All @@ -73,8 +72,8 @@ class Axes(IntEnum):
energy=2, #Energy dimension

@classmethod
def get(cls, value:Union['Axes',int,str])->'Axes':
"convert string,int or Axes value to Axes"
def get(cls, value: 'Axes' | int | str)->'Axes':
"convert string, int or Axes value to Axes"
if isinstance(value,str):
return cls[value]
else:
Expand All @@ -85,14 +84,14 @@ class _ContainerBase:
:noindex:
"""
unit = None
def __init__(self,
def __init__(self,
data: u.Quantity,
flavor: list[FlavorScheme],
time: u.Quantity[u.s],
time: u.Quantity[u.s],
energy: u.Quantity[u.MeV],
*,
integrable_axes: Optional[set[Axes]] = None,
flavor_scheme:Optional[FlavorScheme] = None
integrable_axes: set[Axes] | None = None,
flavor_scheme: FlavorScheme | None = None
):
"""A container class storing the physical quantity (flux, fluence, rate...), which depends on flavor, time and energy.
Expand Down Expand Up @@ -204,7 +203,7 @@ def __repr__(self) -> str:
]
return f"{self.__class__.__name__} {self.array.shape} [{self.array.unit}]: <{' x '.join(s)}>"

def sum(self, axis: Union[Axes,str])->'Container':
def sum(self, axis: Axes | str)->'Container':
"""Sum along given axis, producing a Container with the summary quantity.
Parameters
Expand Down Expand Up @@ -249,7 +248,7 @@ def sum(self, axis: Union[Axes,str])->'Container':
axes[axis] = axes[axis].take([0,-1])
return Container(array,*axes, integrable_axes = self._integrable_axes.difference({axis}))

def integrate(self, axis:Union[Axes,str], limits:np.ndarray=None)->'Container':
def integrate(self, axis: Axes | str, limits:np.ndarray=None)->'Container':
"""Integrate along given axis, producing a Container with the integral quantity.
Parameters
Expand Down Expand Up @@ -317,7 +316,7 @@ def integrate(self, axis:Union[Axes,str], limits:np.ndarray=None)->'Container':
#choose the proper class
return Container(array, *axes, integrable_axes=self._integrable_axes.difference({axis}))

def integrate_or_sum(self, axis:Union[Axes,str])->'Container':
def integrate_or_sum(self, axis: Axes | str)->'Container':
if self.can_integrate(axis):
return self.integrate(axis)
else:
Expand Down
11 changes: 5 additions & 6 deletions python/snewpy/neutrino.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from enum import IntEnum
from astropy import units as u
from dataclasses import dataclass
from typing import Optional
import numpy as np
from collections.abc import Mapping
from .flavor import ThreeFlavor as Flavor
Expand Down Expand Up @@ -41,10 +40,10 @@ class MixingParameters3Flavor(Mapping):
deltaCP: u.Quantity[u.deg]
#square mass difference
dm21_2: u.Quantity[u.eV**2]
dm32_2: Optional[u.Quantity] = None
dm31_2: Optional[u.Quantity] = None
dm32_2: u.Quantity | None = None
dm31_2: u.Quantity | None = None
#mass ordering
mass_order: Optional[MassHierarchy] = None
mass_order: MassHierarchy | None = None
# Note: in IH, the mass splittings are: m3..............m1..m2.

def __iter__(self):
Expand Down Expand Up @@ -124,8 +123,8 @@ class MixingParameters4Flavor(MixingParameters3Flavor):
theta34: u.Quantity[u.deg] = 0<<u.deg
#sterile neutrino mass squared differences
dm41_2: u.Quantity[u.eV**2] = 0<<u.eV**2
dm42_2: Optional[u.Quantity] = None
dm43_2: Optional[u.Quantity] = None
dm42_2: u.Quantity | None = None
dm43_2: u.Quantity | None = None

def __post_init__(self):
super().__post_init__()
Expand Down

0 comments on commit 58c313e

Please sign in to comment.