Skip to content

Commit 222f7e0

Browse files
authored
[Node] Make renamed methods backwards compatible (#256)
1 parent 59e192f commit 222f7e0

File tree

1 file changed

+47
-0
lines changed
  • datacommons_client/endpoints

1 file changed

+47
-0
lines changed

datacommons_client/endpoints/node.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from concurrent.futures import ThreadPoolExecutor
22
from functools import partial
3+
from functools import wraps
34
from typing import Literal, Optional
5+
import warnings
46

57
from datacommons_client.endpoints.base import API
68
from datacommons_client.endpoints.base import Endpoint
@@ -21,6 +23,21 @@
2123

2224
PLACES_MAX_WORKERS = 10
2325

26+
_DEPRECATED_METHODS: dict[str, dict[str, str | dict[str, str]]] = {
27+
"fetch_entity_parents": {
28+
"new_name": "fetch_place_parents",
29+
"arg_map": {
30+
"entity_dcids": "place_dcids"
31+
}
32+
},
33+
"fetch_entity_ascendancy": {
34+
"new_name": "fetch_place_ancestors",
35+
"arg_map": {
36+
"entity_dcids": "place_dcids"
37+
}
38+
}
39+
}
40+
2441

2542
class NodeEndpoint(Endpoint):
2643
"""Initializes the NodeEndpoint with a given API configuration.
@@ -34,6 +51,36 @@ def __init__(self, api: API):
3451
"""Initializes the NodeEndpoint with a given API configuration."""
3552
super().__init__(endpoint="node", api=api)
3653

54+
def __getattr__(self, name):
55+
if name in _DEPRECATED_METHODS:
56+
method_info = _DEPRECATED_METHODS[name]
57+
new_name = method_info["new_name"]
58+
arg_map = method_info.get("arg_map", {})
59+
new_method = getattr(self, new_name)
60+
61+
@wraps(new_method)
62+
def wrapper(*args, **kwargs):
63+
for old_arg, new_arg in arg_map.items():
64+
if old_arg in kwargs:
65+
warnings.warn(
66+
f"Argument '{old_arg}' has been renamed and will removed"
67+
f" in a future version. Use '{new_arg}' instead.",
68+
category=DeprecationWarning,
69+
stacklevel=2)
70+
if new_arg not in kwargs:
71+
kwargs[new_arg] = kwargs.pop(old_arg)
72+
73+
warnings.warn(
74+
f"'{name}' is deprecated and will be removed in a future version. "
75+
f"Use '{new_name}' instead.",
76+
category=DeprecationWarning,
77+
stacklevel=2)
78+
return new_method(*args, **kwargs)
79+
80+
return wrapper
81+
raise AttributeError(
82+
f"'{self.__class__.__name__}' object has no attribute '{name}'")
83+
3784
def fetch(
3885
self,
3986
node_dcids: str | list[str],

0 commit comments

Comments
 (0)