Skip to content

Commit

Permalink
Add some type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
bouweandela committed Jul 2, 2024
1 parent 1a73e84 commit 8e55c61
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 99 deletions.
11 changes: 10 additions & 1 deletion lib/iris/common/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
# See LICENSE in the root of the repository for full licensing details.
"""Provides the infrastructure to support the common metadata API."""

from __future__ import annotations

from abc import ABCMeta
from collections import namedtuple
from collections.abc import Iterable, Mapping
from copy import deepcopy
from functools import lru_cache, wraps
import re

import cf_units
import numpy as np
import numpy.ma as ma
from xxhash import xxh64_hexdigest
Expand Down Expand Up @@ -151,6 +154,12 @@ class BaseMetadata(metaclass=_NamedTupleMeta):

__slots__ = ()

standard_name: str | None
long_name: str | None
var_name: str | None
units: cf_units.Unit
attributes: Mapping

@lenient_service
def __eq__(self, other):
"""Determine whether the associated metadata members are equivalent.
Expand Down Expand Up @@ -681,7 +690,7 @@ def from_metadata(cls, other):
result = cls(**kwargs)
return result

def name(self, default=None, token=False):
def name(self, default: str | None = None, token: bool = False) -> str:
"""Return a string name representing the identity of the metadata.
First it tries standard name, then it tries the long name, then
Expand Down
33 changes: 21 additions & 12 deletions lib/iris/common/mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
# See LICENSE in the root of the repository for full licensing details.
"""Provides common metadata mixin behaviour."""

from __future__ import annotations

from collections.abc import Mapping
from functools import wraps
from typing import Any

import cf_units

Expand Down Expand Up @@ -138,11 +141,17 @@ def update(self, other, **kwargs):


class CFVariableMixin:
_metadata_manager: Any

@wraps(BaseMetadata.name)
def name(self, default=None, token=None):
def name(
self,
default: str | None = None,
token: bool | None = None,
) -> str:
return self._metadata_manager.name(default=default, token=token)

def rename(self, name):
def rename(self, name: str | None) -> None:
"""Change the human-readable name.
If 'name' is a valid standard name it will assign it to
Expand All @@ -161,30 +170,30 @@ def rename(self, name):
self.var_name = None

@property
def standard_name(self):
def standard_name(self) -> str | None:
"""The CF Metadata standard name for the object."""
return self._metadata_manager.standard_name

@standard_name.setter
def standard_name(self, name):
def standard_name(self, name: str | None) -> None:
self._metadata_manager.standard_name = _get_valid_standard_name(name)

@property
def long_name(self):
def long_name(self) -> str | None:
"""The CF Metadata long name for the object."""
return self._metadata_manager.long_name

@long_name.setter
def long_name(self, name):
def long_name(self, name: str | None) -> None:
self._metadata_manager.long_name = name

@property
def var_name(self):
def var_name(self) -> str | None:
"""The NetCDF variable name for the object."""
return self._metadata_manager.var_name

@var_name.setter
def var_name(self, name):
def var_name(self, name: str | None) -> None:
if name is not None:
result = self._metadata_manager.token(name)
if result is None or not name:
Expand All @@ -193,20 +202,20 @@ def var_name(self, name):
self._metadata_manager.var_name = name

@property
def units(self):
def units(self) -> cf_units.Unit:
"""The S.I. unit of the object."""
return self._metadata_manager.units

@units.setter
def units(self, unit):
def units(self, unit: cf_units.Unit | str | None) -> None:
self._metadata_manager.units = cf_units.as_unit(unit)

@property
def attributes(self):
def attributes(self) -> LimitedAttributeDict:
return self._metadata_manager.attributes

@attributes.setter
def attributes(self, attributes):
def attributes(self, attributes: Mapping) -> None:
self._metadata_manager.attributes = LimitedAttributeDict(attributes or {})

@property
Expand Down
Loading

0 comments on commit 8e55c61

Please sign in to comment.