From 72483cbca856806e47133712c56527a2f4050803 Mon Sep 17 00:00:00 2001 From: Mathijs Verhaegh Date: Sat, 13 Jul 2024 00:46:33 +0200 Subject: [PATCH] use typevar for _item_or_default annotation Otherwise you lose all typing when you use that because it returns Any. --- xarray/conventions.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/xarray/conventions.py b/xarray/conventions.py index 45b2284bb97..acbba4b839d 100644 --- a/xarray/conventions.py +++ b/xarray/conventions.py @@ -2,7 +2,7 @@ from collections import defaultdict from collections.abc import Hashable, Iterable, Mapping, MutableMapping -from typing import TYPE_CHECKING, Any, Literal, Union +from typing import TYPE_CHECKING, Any, Literal, Union, TypeVar import numpy as np import pandas as pd @@ -383,8 +383,8 @@ def _update_bounds_encoding(variables: T_Variables) -> None: if "calendar" in encoding: bounds_encoding.setdefault("calendar", encoding["calendar"]) - -def _item_or_default(obj: Mapping | Any, key: Hashable, default: Any = None): +T = TypeVar("T") +def _item_or_default(obj: Mapping[Any, T] | T, key: Hashable, default: T = None) -> T: """ Return item by key if obj is mapping and key is present, else return default value. """