Skip to content

Commit 38c2ee0

Browse files
committed
fix: remove circular types with protocols
1 parent 7f16fb5 commit 38c2ee0

File tree

7 files changed

+2111
-7
lines changed

7 files changed

+2111
-7
lines changed

lightdash/dimensions.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from dataclasses import dataclass
55
from typing import Any, Dict, List, Optional
66

7+
from .types import Model, Dimension as DimensionProtocol
8+
79

810
@dataclass
911
class Dimension:
@@ -48,7 +50,7 @@ class Dimensions:
4850
4951
Will fetch dimensions from API on first access if not already cached.
5052
"""
51-
def __init__(self, model: "Model"):
53+
def __init__(self, model: Model):
5254
self._model = model
5355
self._dimensions: Optional[Dict[str, Dimension]] = None
5456

lightdash/metrics.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from dataclasses import dataclass
55
from typing import Any, Dict, List, Optional
66

7+
from .types import Model, Metric as MetricProtocol
8+
79

810
@dataclass
911
class Metric:
@@ -48,7 +50,7 @@ class Metrics:
4850
4951
Will fetch metrics from API on first access if not already cached.
5052
"""
51-
def __init__(self, model: "Model"):
53+
def __init__(self, model: Model):
5254
self._model = model
5355
self._metrics: Optional[Dict[str, Metric]] = None
5456

lightdash/models.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from dataclasses import dataclass
55
from typing import Any, Dict, List, Optional, Union, Sequence
66

7+
from .types import Model as ModelProtocol, Client
78
from .metrics import Metric, Metrics
89
from .dimensions import Dimension, Dimensions
910
from .query import Query
@@ -20,7 +21,7 @@ class Model:
2021
description: Optional[str] = None
2122

2223
def __post_init__(self):
23-
self._client: Optional["Client"] = None
24+
self._client: Optional[Client] = None
2425
self.metrics = Metrics(self)
2526
self.dimensions = Dimensions(self)
2627

@@ -34,7 +35,7 @@ def _repr_pretty_(self, p, cycle):
3435
else:
3536
p.text(str(self))
3637

37-
def _set_client(self, client: "Client") -> None:
38+
def _set_client(self, client: Client) -> None:
3839
"""Set the client reference for making API calls."""
3940
self._client = client
4041

@@ -126,7 +127,7 @@ class Models:
126127
127128
Will fetch models from API on first access if not already cached.
128129
"""
129-
def __init__(self, client: "Client"):
130+
def __init__(self, client: Client):
130131
self._client = client
131132
self._models: Optional[Dict[str, Model]] = None
132133

lightdash/query.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from .dimensions import Dimension
77
from .metrics import Metric
8+
from .types import Model
89

910

1011
class Query:
@@ -32,7 +33,7 @@ class Query:
3233
"""
3334
def __init__(
3435
self,
35-
model: "Model",
36+
model: Model,
3637
metrics: Sequence[Union[str, Metric]],
3738
dimensions: Sequence[Union[str, Dimension]] = (),
3839
limit: int = 50,

lightdash/types.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""
2+
Type definitions for Lightdash.
3+
"""
4+
from typing import Protocol, Dict, List, Optional, Any
5+
6+
7+
class Client(Protocol):
8+
"""Type protocol for the Lightdash client."""
9+
instance_url: str
10+
access_token: str
11+
project_uuid: str
12+
13+
def _make_request(
14+
self,
15+
method: str,
16+
path: str,
17+
params: Optional[Dict[str, Any]] = None,
18+
json: Optional[Dict[str, Any]] = None,
19+
) -> Dict[str, Any]: ...
20+
21+
22+
class Model(Protocol):
23+
"""Type protocol for a Lightdash model."""
24+
name: str
25+
type: str
26+
database_name: str
27+
schema_name: str
28+
label: Optional[str]
29+
description: Optional[str]
30+
31+
def list_metrics(self) -> List["Metric"]: ...
32+
def list_dimensions(self) -> List["Dimension"]: ...
33+
34+
35+
class Metric(Protocol):
36+
"""Type protocol for a Lightdash metric."""
37+
name: str
38+
model_name: str
39+
label: Optional[str]
40+
description: Optional[str]
41+
42+
@property
43+
def field_id(self) -> str: ...
44+
45+
46+
class Dimension(Protocol):
47+
"""Type protocol for a Lightdash dimension."""
48+
name: str
49+
model_name: str
50+
label: Optional[str]
51+
description: Optional[str]
52+
53+
@property
54+
def field_id(self) -> str: ...

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "lightdash"
3-
version = "0.2.3"
3+
version = "0.2.4"
44
description = "Query metrics from Lightdash"
55
readme = "README.md"
66
requires-python = ">=3.9"
@@ -18,6 +18,7 @@ dev = [
1818
"jupyter>=1.0.0",
1919
"ipykernel>=6.0.0", # Required for Jupyter notebooks
2020
"pandas>=2.0.0", # For DataFrame support in tests
21+
"polars>=1.22.0", # For DataFrame support in tests
2122
]
2223

2324
[tool.pytest.ini_options]

0 commit comments

Comments
 (0)