Skip to content

Commit

Permalink
use typevar for _item_or_default annotation
Browse files Browse the repository at this point in the history
Otherwise you lose all typing when you use that because it returns Any.
  • Loading branch information
Ostheer authored Jul 12, 2024
1 parent b218b3c commit 72483cb
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions xarray/conventions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
"""
Expand Down

0 comments on commit 72483cb

Please sign in to comment.