Skip to content

Commit

Permalink
MNT: auto upgrade code base to Python 3.9
Browse files Browse the repository at this point in the history
  • Loading branch information
neutrinoceros committed Sep 3, 2023
1 parent 8ec65d1 commit 4cfd370
Show file tree
Hide file tree
Showing 71 changed files with 268 additions and 338 deletions.
3 changes: 1 addition & 2 deletions tests/unpin_requirements.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import re
import sys
from typing import List

import tomli_w

Expand All @@ -12,7 +11,7 @@
PINNED_VERSION_REGEXP = re.compile(r",?(<|<=|==)([0-9a-z]+\.?)+")


def unpin_requirements(requirements: List[str]) -> List[str]:
def unpin_requirements(requirements: list[str]) -> list[str]:
return [re.sub(PINNED_VERSION_REGEXP, "", _) for _ in requirements]


Expand Down
4 changes: 2 additions & 2 deletions yt/_maintenance/deprecation.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import warnings
from functools import wraps
from types import FunctionType
from typing import Dict, Optional
from typing import Optional


def issue_deprecation_warning(
Expand Down Expand Up @@ -45,7 +45,7 @@ def issue_deprecation_warning(
warnings.warn(msg, DeprecationWarning, stacklevel=stacklevel)


def future_positional_only(positions2names: Dict[int, str], /, **depr_kwargs):
def future_positional_only(positions2names: dict[int, str], /, **depr_kwargs):
"""Warn users when using a future positional-only argument as keyword.
Note that positional-only arguments are available from Python 3.8
See https://www.python.org/dev/peps/pep-0570/
Expand Down
18 changes: 9 additions & 9 deletions yt/_typing.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
from typing import List, Optional, Tuple, Union
from typing import Optional, Union

import unyt as un
from numpy import ndarray

FieldDescT = Tuple[str, Tuple[str, List[str], Optional[str]]]
KnownFieldsT = Tuple[FieldDescT, ...]
FieldDescT = tuple[str, tuple[str, list[str], Optional[str]]]
KnownFieldsT = tuple[FieldDescT, ...]

ParticleType = str
FieldType = str
FieldName = str
FieldKey = Tuple[FieldType, FieldName]
FieldKey = tuple[FieldType, FieldName]
ImplicitFieldKey = FieldName
AnyFieldKey = Union[FieldKey, ImplicitFieldKey]
DomainDimensions = Union[Tuple[int, ...], List[int], ndarray]
DomainDimensions = Union[tuple[int, ...], list[int], ndarray]

ParticleCoordinateTuple = Tuple[
ParticleCoordinateTuple = tuple[
str, # particle type
Tuple[ndarray, ndarray, ndarray], # xyz
tuple[ndarray, ndarray, ndarray], # xyz
Union[float, ndarray], # hsml
]

# Geometry specific types
AxisName = str
AxisOrder = Tuple[AxisName, AxisName, AxisName]
AxisOrder = tuple[AxisName, AxisName, AxisName]

# types that can be converted to un.Unit
Unit = Union[un.Unit, str]

# types that can be converted to un.unyt_quantity
Quantity = Union[un.unyt_quantity, Tuple[float, Unit]]
Quantity = Union[un.unyt_quantity, tuple[float, Unit]]
12 changes: 6 additions & 6 deletions yt/data_objects/data_containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import weakref
from collections import defaultdict
from contextlib import contextmanager
from typing import TYPE_CHECKING, List, Optional, Tuple
from typing import TYPE_CHECKING, Optional

import numpy as np

Expand Down Expand Up @@ -66,14 +66,14 @@ class YTDataContainer(abc.ABC):

_chunk_info = None
_num_ghost_zones = 0
_con_args: Tuple[str, ...] = ()
_con_args: tuple[str, ...] = ()
_skip_add = False
_container_fields: Tuple[AnyFieldKey, ...] = ()
_tds_attrs: Tuple[str, ...] = ()
_tds_fields: Tuple[str, ...] = ()
_container_fields: tuple[AnyFieldKey, ...] = ()
_tds_attrs: tuple[str, ...] = ()
_tds_fields: tuple[str, ...] = ()
_field_cache = None
_index = None
_key_fields: List[str]
_key_fields: list[str]

def __init__(self, ds: Optional["Dataset"], field_parameters) -> None:
"""
Expand Down
3 changes: 1 addition & 2 deletions yt/data_objects/index_subobjects/grid_patch.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import weakref
from typing import List

import numpy as np

Expand Down Expand Up @@ -270,7 +269,7 @@ def retrieve_ghost_zones(self, n_zones, fields, all_levels=False, smoothed=False

def get_vertex_centered_data(
self,
fields: List[FieldKey],
fields: list[FieldKey],
smoothed: bool = True,
no_ghost: bool = False,
):
Expand Down
3 changes: 1 addition & 2 deletions yt/data_objects/index_subobjects/octree_subset.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from contextlib import contextmanager
from functools import cached_property
from itertools import product, repeat
from typing import Tuple

import numpy as np
from unyt import unyt_array
Expand Down Expand Up @@ -39,7 +38,7 @@ class OctreeSubset(YTSelectionContainer):
_num_ghost_zones = 0
_type_name = "octree_subset"
_skip_add = True
_con_args: Tuple[str, ...] = ("base_region", "domain", "ds")
_con_args: tuple[str, ...] = ("base_region", "domain", "ds")
_domain_offset = 0
_cell_count = -1
_block_order = "C"
Expand Down
3 changes: 1 addition & 2 deletions yt/data_objects/particle_filters.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import copy
from contextlib import contextmanager
from typing import Dict

from yt.fields.field_info_container import NullFunc, TranslationFunc
from yt.funcs import mylog
from yt.utilities.exceptions import YTIllDefinedFilter

# One to one mapping
filter_registry: Dict[str, "ParticleFilter"] = {}
filter_registry: dict[str, "ParticleFilter"] = {}


class DummyFieldInfo:
Expand Down
3 changes: 1 addition & 2 deletions yt/data_objects/selection_objects/data_selection_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import uuid
from collections import defaultdict
from contextlib import contextmanager
from typing import Tuple

import numpy as np
from more_itertools import always_iterable
Expand Down Expand Up @@ -1398,7 +1397,7 @@ def _get_bbox(self):
"""
return self.ds.domain_left_edge, self.ds.domain_right_edge

def get_bbox(self) -> Tuple[unyt_array, unyt_array]:
def get_bbox(self) -> tuple[unyt_array, unyt_array]:
"""
Return the bounding box for this data container.
"""
Expand Down
48 changes: 19 additions & 29 deletions yt/data_objects/static_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,8 @@
from stat import ST_CTIME
from typing import (
Any,
DefaultDict,
Dict,
List,
Literal,
Optional,
Set,
Tuple,
Type,
Union,
)

Expand Down Expand Up @@ -89,11 +83,7 @@
else:
from typing_extensions import assert_never

if sys.version_info >= (3, 9):
from collections.abc import MutableMapping
else:
from typing import MutableMapping

from collections.abc import MutableMapping

# We want to support the movie format in the future.
# When such a thing comes to pass, I'll move all the stuff that is constant up
Expand Down Expand Up @@ -178,26 +168,26 @@ def ireq(self, value):
class Dataset(abc.ABC):
default_fluid_type = "gas"
default_field = ("gas", "density")
fluid_types: Tuple[FieldType, ...] = ("gas", "deposit", "index")
particle_types: Tuple[ParticleType, ...] = ("io",) # By default we have an 'all'
particle_types_raw: Optional[Tuple[ParticleType, ...]] = ("io",)
fluid_types: tuple[FieldType, ...] = ("gas", "deposit", "index")
particle_types: tuple[ParticleType, ...] = ("io",) # By default we have an 'all'
particle_types_raw: Optional[tuple[ParticleType, ...]] = ("io",)
geometry: Geometry = Geometry.CARTESIAN
coordinates = None
storage_filename = None
particle_unions: Optional[Dict[ParticleType, ParticleUnion]] = None
known_filters: Optional[Dict[ParticleType, ParticleFilter]] = None
_index_class: Type[Index]
field_units: Optional[Dict[AnyFieldKey, Unit]] = None
particle_unions: Optional[dict[ParticleType, ParticleUnion]] = None
known_filters: Optional[dict[ParticleType, ParticleFilter]] = None
_index_class: type[Index]
field_units: Optional[dict[AnyFieldKey, Unit]] = None
derived_field_list = requires_index("derived_field_list")
fields = requires_index("fields")
conversion_factors: Optional[Dict[str, float]] = None
conversion_factors: Optional[dict[str, float]] = None
# _instantiated represents an instantiation time (since Epoch)
# the default is a place holder sentinel, falsy value
_instantiated: float = 0
_particle_type_counts = None
_proj_type = "quad_proj"
_ionization_label_format = "roman_numeral"
_determined_fields: Optional[Dict[str, List[FieldKey]]] = None
_determined_fields: Optional[dict[str, list[FieldKey]]] = None
fields_detected = False

# these are set in self._parse_parameter_file()
Expand Down Expand Up @@ -253,7 +243,7 @@ def __init__(
self,
filename: str,
dataset_type: Optional[str] = None,
units_override: Optional[Dict[str, str]] = None,
units_override: Optional[dict[str, str]] = None,
# valid unit_system values include all keys from unyt.unit_systems.unit_systems_registry + "code"
unit_system: Literal[
"cgs",
Expand Down Expand Up @@ -281,7 +271,7 @@ def __init__(
return
self.dataset_type = dataset_type
self.conversion_factors = {}
self.parameters: Dict[str, Any] = {}
self.parameters: dict[str, Any] = {}
self.region_expression = self.r = RegionExpression(self)
self.known_filters = self.known_filters or {}
self.particle_unions = self.particle_unions or {}
Expand Down Expand Up @@ -751,7 +741,7 @@ def setup_deprecated_fields(self):
def _setup_coordinate_handler(self, axis_order: Optional[AxisOrder]) -> None:
# backward compatibility layer:
# turning off type-checker on a per-line basis
cls: Type[CoordinateHandler]
cls: type[CoordinateHandler]

if isinstance(self.geometry, tuple): # type: ignore [unreachable]
issue_deprecation_warning( # type: ignore [unreachable]
Expand Down Expand Up @@ -973,7 +963,7 @@ def _get_field_info(
# https://github.com/yt-project/yt/issues/3381
return field_info

def _are_ambiguous(candidates: List[FieldKey]) -> bool:
def _are_ambiguous(candidates: list[FieldKey]) -> bool:
if len(candidates) < 2:
return False

Expand All @@ -996,7 +986,7 @@ def _are_ambiguous(candidates: List[FieldKey]) -> bool:
elif all(ft in self.particle_types for ft in ftypes):
ptypes = ftypes

sub_types_list: List[Set[str]] = []
sub_types_list: list[set[str]] = []
for pt in ptypes:
if pt in self.particle_types_raw:
sub_types_list.append({pt})
Expand Down Expand Up @@ -1026,7 +1016,7 @@ def _get_field_info_helper(
self,
field: Union[FieldKey, ImplicitFieldKey, DerivedField],
/,
) -> Tuple[DerivedField, List[FieldKey]]:
) -> tuple[DerivedField, list[FieldKey]]:
self.index

ftype: str
Expand All @@ -1041,7 +1031,7 @@ def _get_field_info_helper(
raise YTFieldNotParseable(field)

if ftype == "unknown":
candidates: List[FieldKey] = [
candidates: list[FieldKey] = [
(ft, fn) for ft, fn in self.field_info if fn == fname
]

Expand Down Expand Up @@ -1297,7 +1287,7 @@ def _assign_unit_system(
# dimensions as amperes.
mks_system = False
mag_unit: Optional[unyt_quantity] = getattr(self, "magnetic_unit", None)
mag_dims: Optional[Set[Symbol]]
mag_dims: Optional[set[Symbol]]
if mag_unit is not None:
mag_dims = mag_unit.units.dimensions.free_symbols
else:
Expand Down Expand Up @@ -2075,7 +2065,7 @@ class ParticleFile:

start: Optional[int] = None
end: Optional[int] = None
total_particles: Optional[DefaultDict[str, int]] = None
total_particles: Optional[defaultdict[str, int]] = None

def __init__(self, ds, io, filename, file_id, range=None):
self.ds = ds
Expand Down
4 changes: 2 additions & 2 deletions yt/data_objects/time_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import weakref
from abc import ABC, abstractmethod
from functools import wraps
from typing import Optional, Type
from typing import Optional

import numpy as np
from more_itertools import always_iterable
Expand Down Expand Up @@ -146,7 +146,7 @@ class DatasetSeries:
# this annotation should really be Optional[Type[Dataset]]
# but we cannot import the yt.data_objects.static_output.Dataset
# class here without creating a circular import for now
_dataset_cls: Optional[Type] = None
_dataset_cls: Optional[type] = None

def __init_subclass__(cls, *args, **kwargs):
super().__init_subclass__(*args, **kwargs)
Expand Down
3 changes: 2 additions & 1 deletion yt/fields/derived_field.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import contextlib
import inspect
import re
from typing import Iterable, Optional, Union
from collections.abc import Iterable
from typing import Optional, Union

from more_itertools import always_iterable

Expand Down
5 changes: 2 additions & 3 deletions yt/fields/domain_context.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import abc
from typing import Tuple

from yt._typing import FieldKey

Expand All @@ -12,8 +11,8 @@ def __init__(cls, name, b, d):
type.__init__(cls, name, b, d)
domain_context_registry[name] = cls

_known_fluid_fields: Tuple[FieldKey, ...]
_known_particle_fields: Tuple[FieldKey, ...]
_known_fluid_fields: tuple[FieldKey, ...]
_known_particle_fields: tuple[FieldKey, ...]

def __init__(self, ds):
self.ds = ds
6 changes: 3 additions & 3 deletions yt/fields/field_detector.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from collections import defaultdict
from typing import Tuple, Union
from typing import Union

import numpy as np

Expand Down Expand Up @@ -101,7 +101,7 @@ def _reshape_vals(self, arr):
return arr
return arr.reshape(self.ActiveDimensions, order="C")

def __missing__(self, item: Union[Tuple[str, str], str]):
def __missing__(self, item: Union[tuple[str, str], str]):
from yt.fields.derived_field import NullFunc

if not isinstance(item, tuple):
Expand All @@ -117,7 +117,7 @@ def __missing__(self, item: Union[Tuple[str, str], str]):
# types not getting correctly identified.
# Note that the *only* way this works is if we also fix our field
# dependencies during checking. Bug #627 talks about this.
_item: Tuple[str, str] = finfo.name
_item: tuple[str, str] = finfo.name
if finfo is not None and finfo._function is not NullFunc:
try:
for param, param_v in permute_params.items():
Expand Down
Loading

0 comments on commit 4cfd370

Please sign in to comment.