Skip to content

Commit d65ef27

Browse files
committed
Accept datasource attribute on DatasourceItem, but don't process it
> When I try to explore datasources it always fail with a TypeError. > No idea why this happens. Exception: TypeError: DatasourceItem.__init__() got an unexpected keyword argument 'datasource'
1 parent d29da52 commit d65ef27

File tree

3 files changed

+87
-3
lines changed

3 files changed

+87
-3
lines changed

CHANGES.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ grafana-wtf changelog
66
in progress
77
===========
88
- CI: Verify support for Grafana 11
9-
- AMG compatibility: Fetch Grafana version from `/api/frontend/settings`
10-
instead of `/api/health`. Thanks, @squadgazzz.
9+
- AMG compatibility: Fetch Grafana version from ``/api/frontend/settings``
10+
instead of ``/api/health``. Thanks, @squadgazzz.
11+
- Accept ``datasource`` attribute on ``DatasourceItem``, but
12+
ignore it for the time being. Thanks, @apepojken.
1113

1214
2024-04-20 0.19.1
1315
=================

grafana_wtf/model.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
# License: GNU Affero General Public License, Version 3
44
import dataclasses
55
import logging
6+
import warnings
67
from collections import OrderedDict
7-
from typing import Dict, List, Optional
8+
from typing import Any, Dict, List, Optional
89
from urllib.parse import urljoin
910

1011
from munch import Munch
@@ -158,6 +159,10 @@ def transform(section):
158159

159160
@dataclasses.dataclass
160161
class DatasourceItem:
162+
"""
163+
Represent a datasource reference within a panel, annotation, or templating (variable).
164+
"""
165+
161166
uid: Optional[str] = None
162167
name: Optional[str] = None
163168
type: Optional[str] = None
@@ -168,11 +173,31 @@ def from_payload(cls, payload: any):
168173
if isinstance(payload, Munch):
169174
payload = dict(payload)
170175
if isinstance(payload, dict):
176+
cls.validate(payload)
171177
return cls(**payload)
172178
if isinstance(payload, str):
173179
return cls(name=payload)
174180
raise TypeError(f"Unknown payload type for DatasourceItem: {type(payload)}")
175181

182+
@classmethod
183+
def validate(cls, data: dict):
184+
if "datasource" in data:
185+
warnings.warn(
186+
f"""
187+
The `datasource` attribute is ignored for the time being.
188+
189+
See also: https://github.com/panodata/grafana-wtf/issues/110
190+
191+
Please report back this occurrence to the grafana-wtf issue tracker,
192+
so the maintainers can improve the situation.
193+
194+
The context of this error is in `DatasourceItem`, using this ingress data:
195+
{data}
196+
""".strip(),
197+
UserWarning,
198+
)
199+
del data["datasource"]
200+
176201

177202
@dataclasses.dataclass
178203
class DatasourceExplorationItem:

tests/test_model.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import re
2+
3+
import pytest
4+
from munch import Munch
5+
6+
from grafana_wtf.model import DatasourceItem
7+
8+
DATA = dict(uid="foo", name="bar", type="baz", url="qux")
9+
10+
11+
def test_datasource_item_basic():
12+
item = DatasourceItem(**DATA)
13+
assert item.uid == "foo"
14+
15+
16+
def test_datasource_item_dict_success():
17+
item = DatasourceItem.from_payload(DATA)
18+
assert item.uid == "foo"
19+
20+
21+
def test_datasource_item_munch():
22+
item = DatasourceItem.from_payload(Munch(**DATA))
23+
assert item.uid == "foo"
24+
25+
26+
def test_datasource_item_str():
27+
item = DatasourceItem.from_payload("Hotzenplotz")
28+
assert item.uid is None
29+
assert item.name == "Hotzenplotz"
30+
31+
32+
def test_datasource_item_dict_unknown_attribute():
33+
mydata = DATA.copy()
34+
mydata.update({"more": "data"})
35+
with pytest.raises(TypeError) as ex:
36+
DatasourceItem.from_payload(mydata)
37+
assert ex.match(re.escape("__init__() got an unexpected keyword argument 'more'"))
38+
39+
40+
def test_datasource_item_dict_compensate_datasource():
41+
"""
42+
Validate that the `datasource` attribute is ignored.
43+
44+
TypeError: DatasourceItem.__init__() got an unexpected keyword argument 'datasource'
45+
https://github.com/panodata/grafana-wtf/issues/110
46+
"""
47+
mydata = DATA.copy()
48+
mydata.update({"datasource": "unknown"})
49+
with pytest.warns(UserWarning) as record:
50+
item = DatasourceItem.from_payload(mydata)
51+
assert item.uid == "foo"
52+
53+
# Check that only one warning was raised.
54+
assert len(record) == 1
55+
56+
# Check that the message matches.
57+
assert "The `datasource` attribute is ignored for the time being" in record[0].message.args[0]

0 commit comments

Comments
 (0)