From 0824213f713c2edd29b42f5e90f90334ea491c70 Mon Sep 17 00:00:00 2001 From: Pradish Bijukchhe Date: Sat, 5 Oct 2024 17:06:55 +0545 Subject: [PATCH 1/3] change labels() method return type --- pytheus/metrics.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pytheus/metrics.py b/pytheus/metrics.py index f69e0a0..2fd3082 100644 --- a/pytheus/metrics.py +++ b/pytheus/metrics.py @@ -5,7 +5,7 @@ import time from contextlib import contextmanager from dataclasses import dataclass -from typing import Callable, Dict, Generator, Iterable, Optional, Sequence, Tuple, Type, Union +from typing import Callable, Dict, Generator, Iterable, Optional, Self, Sequence, Tuple, Type, Union from pytheus.backends import get_backend from pytheus.exceptions import ( @@ -202,7 +202,7 @@ def _raise_if_cannot_observe(self) -> None: if not self._can_observe: raise UnobservableMetricException - def labels(self, pytheus_labels_: Optional[Labels] = None, **kwargs: str) -> "_Metric": + def labels(self, pytheus_labels_: Optional[Labels] = None, **kwargs: str) -> Self: """ If no labels is passed to the call returns itself. If there are already present labels, they will be updated with the passed labels_ and if From be07f3503f375059926a4265511a153e513d9dc9 Mon Sep 17 00:00:00 2001 From: Pradish Bijukchhe Date: Sat, 5 Oct 2024 17:32:43 +0545 Subject: [PATCH 2/3] add backwards compatibility for self --- pytheus/metrics.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/pytheus/metrics.py b/pytheus/metrics.py index 2fd3082..b179906 100644 --- a/pytheus/metrics.py +++ b/pytheus/metrics.py @@ -5,7 +5,18 @@ import time from contextlib import contextmanager from dataclasses import dataclass -from typing import Callable, Dict, Generator, Iterable, Optional, Self, Sequence, Tuple, Type, Union +from typing import ( + TYPE_CHECKING, + Callable, + Dict, + Generator, + Iterable, + Optional, + Sequence, + Tuple, + Type, + Union, +) from pytheus.backends import get_backend from pytheus.exceptions import ( @@ -16,6 +27,10 @@ from pytheus.registry import REGISTRY, Collector, Registry from pytheus.utils import InfFloat, MetricType +# Backwards compatibility for python<3.11 +if TYPE_CHECKING: + from typing import Self + Labels = Dict[str, str] @@ -202,7 +217,7 @@ def _raise_if_cannot_observe(self) -> None: if not self._can_observe: raise UnobservableMetricException - def labels(self, pytheus_labels_: Optional[Labels] = None, **kwargs: str) -> Self: + def labels(self, pytheus_labels_: Optional[Labels] = None, **kwargs: str) -> "Self": """ If no labels is passed to the call returns itself. If there are already present labels, they will be updated with the passed labels_ and if From fa953164440222bcc15066682d08b957910ac4e2 Mon Sep 17 00:00:00 2001 From: Pradish Bijukchhe Date: Tue, 15 Oct 2024 22:01:27 +0545 Subject: [PATCH 3/3] use typing_extension for backwards compatibility --- pyproject.toml | 1 + pytheus/metrics.py | 21 ++++----------------- 2 files changed, 5 insertions(+), 17 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 9c7b0f0..b1700c2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,6 +4,7 @@ version = "0.5.0" authors = [ { name="Llandy Riveron Del Risco", email="llandy3d@gmail.com" }, ] +dependencies = ["typing-extensions >= 4.0.0"] description = "playing with metrics" readme = "README.md" requires-python = ">=3.8" diff --git a/pytheus/metrics.py b/pytheus/metrics.py index b179906..3af8697 100644 --- a/pytheus/metrics.py +++ b/pytheus/metrics.py @@ -5,18 +5,9 @@ import time from contextlib import contextmanager from dataclasses import dataclass -from typing import ( - TYPE_CHECKING, - Callable, - Dict, - Generator, - Iterable, - Optional, - Sequence, - Tuple, - Type, - Union, -) +from typing import Callable, Dict, Generator, Iterable, Optional, Sequence, Tuple, Type, Union + +from typing_extensions import Self from pytheus.backends import get_backend from pytheus.exceptions import ( @@ -27,10 +18,6 @@ from pytheus.registry import REGISTRY, Collector, Registry from pytheus.utils import InfFloat, MetricType -# Backwards compatibility for python<3.11 -if TYPE_CHECKING: - from typing import Self - Labels = Dict[str, str] @@ -217,7 +204,7 @@ def _raise_if_cannot_observe(self) -> None: if not self._can_observe: raise UnobservableMetricException - def labels(self, pytheus_labels_: Optional[Labels] = None, **kwargs: str) -> "Self": + def labels(self, pytheus_labels_: Optional[Labels] = None, **kwargs: str) -> Self: """ If no labels is passed to the call returns itself. If there are already present labels, they will be updated with the passed labels_ and if