Skip to content

Commit

Permalink
UUID7 (#1267)
Browse files Browse the repository at this point in the history
* UUID7

* fixes and hopefully lint

* fix mypy

* fix tests

* lint

* validate new and old uuids

* remove uuid7 requirement

* remove more uuid4

* serialization

* change aliases to base uuid type

* remove ser stuff

* introduce new_id

* fix

* move
  • Loading branch information
mike0sv authored Sep 18, 2024
1 parent 68f56f7 commit 40817e1
Show file tree
Hide file tree
Showing 33 changed files with 138 additions and 115 deletions.
1 change: 1 addition & 0 deletions requirements.min.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ certifi==2024.7.4
urllib3==1.26.19
ujson==5.4.0
deprecation==2.1.0
uuid6==2024.7.10
cryptography==43.0.1

openai==1.16.2
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"fsspec>=2024.6.1",
"ujson>=5.4.0",
"deprecation>=2.1.0",
"uuid6>=2024.7.10",
"cryptography>=43.0.1",
],
extras_require={
Expand Down
3 changes: 0 additions & 3 deletions src/evidently/_pydantic_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
v = 1 if pydantic.__version__.startswith("1") else 2

if v == 2:
from pydantic.v1 import UUID4
from pydantic.v1 import BaseConfig
from pydantic.v1 import BaseModel
from pydantic.v1 import Extra
Expand All @@ -31,7 +30,6 @@
from pydantic.v1.typing import DictStrAny

else:
from pydantic import UUID4
from pydantic import BaseConfig
from pydantic import BaseModel
from pydantic import Extra
Expand All @@ -58,7 +56,6 @@


__all__ = [
"UUID4",
"BaseConfig",
"BaseModel",
"Field",
Expand Down
10 changes: 6 additions & 4 deletions src/evidently/cli/ui.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
import os
from typing import Optional

import uuid6
from typer import BadParameter
from typer import Option
from typer import echo

from evidently.cli.main import app


def setup_deterministic_generation_uuid4(seed: int = 8754):
def setup_deterministic_generation_uuid(seed: int = 8754):
import uuid

from faker import Faker

Faker.seed(seed)
fake = Faker()

def deterministic_uuid4():
def deterministic_uuid():
return fake.uuid4(cast_to=None)

uuid.uuid4 = deterministic_uuid4
uuid.uuid4 = deterministic_uuid
uuid6.uuid7 = deterministic_uuid


@app.command("ui")
Expand All @@ -36,7 +38,7 @@ def ui(
):
"""Start Evidently UI service"""
if os.environ.get("EXPERIMENTAL_DETERMINISTIC_UUID"):
setup_deterministic_generation_uuid4()
setup_deterministic_generation_uuid()

from evidently.ui.app import run_local
from evidently.ui.demo_projects import DEMO_PROJECTS
Expand Down
6 changes: 6 additions & 0 deletions src/evidently/core.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import uuid
from typing import TYPE_CHECKING
from typing import Any
from typing import ClassVar
Expand All @@ -12,6 +13,7 @@

import numpy as np
import pandas as pd
import uuid6
from typing_inspect import is_literal_type

from evidently._pydantic_compat import SHAPE_DICT
Expand Down Expand Up @@ -273,3 +275,7 @@ def get_field_tags(cls: Type[BaseModel], field_name: str) -> Set[IncludeTags]:

def get_all_fields_tags(cls: Type[BaseResult]) -> Dict[str, Set[IncludeTags]]:
return {field_name: get_field_tags(cls, field_name) for field_name in cls.__fields__}


def new_id() -> uuid.UUID:
return uuid6.uuid7()
4 changes: 2 additions & 2 deletions src/evidently/descriptors/custom_descriptor.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import uuid
from typing import Callable
from typing import Union

import pandas as pd

from evidently.core import ColumnType
from evidently.core import new_id
from evidently.features.custom_feature import CustomPairColumnFeature
from evidently.features.custom_feature import CustomSingleColumnFeature
from evidently.features.generated_features import FeatureDescriptor
Expand All @@ -24,7 +24,7 @@ def feature(self, column_name: str) -> GeneratedFeature:
column_name=column_name,
display_name=self.display_name,
feature_type=ColumnType(self.feature_type),
name=self.name or getattr(self.func, "__name__", str(uuid.uuid4())),
name=self.name or getattr(self.func, "__name__", str(new_id())),
)


Expand Down
4 changes: 2 additions & 2 deletions src/evidently/experimental/report_set.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import datetime
import os
import uuid
from typing import Dict
from typing import Optional

from evidently._pydantic_compat import ValidationError
from evidently.suite.base_suite import Snapshot
from evidently.ui.type_aliases import SnapshotID


def load_snapshots(
path: str,
date_from: Optional[datetime.datetime] = None,
date_to: Optional[datetime.datetime] = None,
skip_errors: bool = False,
) -> Dict[uuid.UUID, Snapshot]:
) -> Dict[SnapshotID, Snapshot]:
result = {}
for file in os.listdir(path):
filepath = os.path.join(path, file)
Expand Down
8 changes: 4 additions & 4 deletions src/evidently/features/custom_feature.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import uuid
from typing import Callable
from typing import Tuple

Expand All @@ -7,6 +6,7 @@
from evidently import ColumnType
from evidently._pydantic_compat import Field
from evidently.base_metric import ColumnName
from evidently.core import new_id
from evidently.features.generated_features import FeatureTypeFieldMixin
from evidently.features.generated_features import GeneratedFeature
from evidently.pydantic_utils import FingerprintPart
Expand All @@ -15,7 +15,7 @@

class CustomFeature(FeatureTypeFieldMixin, GeneratedFeature):
display_name: str
name: str = Field(default_factory=lambda: str(uuid.uuid4()))
name: str = Field(default_factory=lambda: str(new_id()))
func: Callable[[pd.DataFrame, DataDefinition], pd.Series]
feature_type: ColumnType = ColumnType.Numerical

Expand All @@ -30,7 +30,7 @@ def _as_column(self) -> "ColumnName":
class CustomSingleColumnFeature(FeatureTypeFieldMixin, GeneratedFeature):
display_name: str
func: Callable[[pd.Series], pd.Series]
name: str = Field(default_factory=lambda: str(uuid.uuid4()))
name: str = Field(default_factory=lambda: str(new_id()))
column_name: str
feature_type: ColumnType = ColumnType.Numerical

Expand All @@ -52,7 +52,7 @@ def get_fingerprint_parts(self) -> Tuple[FingerprintPart, ...]:
class CustomPairColumnFeature(FeatureTypeFieldMixin, GeneratedFeature):
display_name: str
func: Callable[[pd.Series, pd.Series], pd.Series]
name: str = Field(default_factory=lambda: str(uuid.uuid4()))
name: str = Field(default_factory=lambda: str(new_id()))
first_column: str
second_column: str
feature_type: ColumnType = ColumnType.Numerical
Expand Down
4 changes: 2 additions & 2 deletions src/evidently/features/generated_features.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import abc
import dataclasses
import uuid
from typing import Any
from typing import ClassVar
from typing import Generic
Expand All @@ -9,6 +8,7 @@

import deprecation
import pandas as pd
import uuid6

from evidently._pydantic_compat import BaseModel
from evidently._pydantic_compat import Field
Expand Down Expand Up @@ -173,7 +173,7 @@ def _feature_display_name(self):

class DataFeature(GeneratedFeature):
display_name: str
name: str = Field(default_factory=lambda: str(uuid.uuid4()))
name: str = Field(default_factory=lambda: str(uuid6.uuid7()))

@abc.abstractmethod
def generate_data(self, data: pd.DataFrame, data_definition: DataDefinition) -> pd.Series:
Expand Down
4 changes: 2 additions & 2 deletions src/evidently/features/openai_feature.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import uuid
from itertools import repeat
from typing import List
from typing import Optional
Expand All @@ -8,6 +7,7 @@

from evidently.base_metric import ColumnName
from evidently.core import ColumnType
from evidently.core import new_id
from evidently.features.generated_features import FeatureTypeFieldMixin
from evidently.features.generated_features import GeneratedFeature
from evidently.utils.data_preprocessing import DataDefinition
Expand Down Expand Up @@ -43,7 +43,7 @@ def __init__(
openai_params: Optional[dict] = None,
display_name: Optional[str] = None,
):
self.feature_id = str(uuid.uuid4())
self.feature_id = str(new_id())
self.prompt = prompt
self.prompt_replace_string = prompt_replace_string
if context is not None and context_column is not None:
Expand Down
7 changes: 4 additions & 3 deletions src/evidently/model/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# coding: utf-8
# pylint: disable=invalid-name
import dataclasses
import uuid
from dataclasses import dataclass
from dataclasses import field
from enum import Enum
Expand All @@ -12,6 +11,8 @@
from typing import Optional
from typing import Union

import uuid6

from evidently.pydantic_utils import EvidentlyBaseModel
from evidently.pydantic_utils import Fingerprint

Expand Down Expand Up @@ -66,7 +67,7 @@ class AdditionalGraphInfo:
class PlotlyGraphInfo:
data: Any
layout: Any
id: str = dataclasses.field(default_factory=lambda: str(uuid.uuid4()))
id: str = dataclasses.field(default_factory=lambda: str(uuid6.uuid7()))


class WidgetType(Enum):
Expand All @@ -84,7 +85,7 @@ class BaseWidgetInfo:
type: str
title: str
size: int
id: str = dataclasses.field(default_factory=lambda: str(uuid.uuid4()))
id: str = dataclasses.field(default_factory=lambda: str(uuid6.uuid7()))
details: str = ""
alertsPosition: Optional[str] = None
alertStats: Optional[AlertStats] = None
Expand Down
19 changes: 13 additions & 6 deletions src/evidently/pydantic_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,28 @@

if TYPE_CHECKING:
from evidently._pydantic_compat import DictStrAny


T = TypeVar("T")


def pydantic_type_validator(type_: Type[Any]):
def pydantic_type_validator(type_: Type[Any], prioritize: bool = False):
def decorator(f):
from evidently._pydantic_compat import _VALIDATORS

for cls, validators in _VALIDATORS:
if cls is type_:
validators.append(f)
if prioritize:
validators.insert(0, f)
else:
validators.append(f)
return

_VALIDATORS.append(
(type_, [f]),
)
if prioritize:
_VALIDATORS.insert(0, (type_, [f]))
else:
_VALIDATORS.append(
(type_, [f]),
)

return decorator

Expand Down
4 changes: 2 additions & 2 deletions src/evidently/renderers/base_renderer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import dataclasses
import uuid
import warnings
from typing import TYPE_CHECKING
from typing import Dict
Expand All @@ -8,6 +7,7 @@
from typing import Union

import pandas as pd
import uuid6

from evidently.model.widget import AdditionalGraphInfo
from evidently.model.widget import BaseWidgetInfo
Expand Down Expand Up @@ -62,7 +62,7 @@ def render_html(self, obj) -> List[BaseWidgetInfo]:
class DetailsInfo:
title: str
info: BaseWidgetInfo
id: str = dataclasses.field(default_factory=lambda: str(uuid.uuid4()))
id: str = dataclasses.field(default_factory=lambda: str(uuid6.uuid7()))


@dataclasses.dataclass
Expand Down
6 changes: 3 additions & 3 deletions src/evidently/renderers/html_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
from typing import Optional
from typing import Tuple
from typing import Union
from uuid import uuid4

import numpy as np
import pandas as pd
from plotly import graph_objs as go
from plotly.subplots import make_subplots
from uuid6 import uuid7

from evidently.metric_results import Distribution
from evidently.metric_results import HistogramData
Expand Down Expand Up @@ -140,7 +140,7 @@ def plotly_graph_tabs(*, title: str, figures: List[GraphData], size: WidgetSize
params={
"graphs": [
{
"id": str(uuid4()),
"id": str(uuid7()),
"title": graph.title,
"graph": {
"data": graph.data,
Expand Down Expand Up @@ -339,7 +339,7 @@ def widget_tabs(*, title: str = "", size: WidgetSize = WidgetSize.FULL, tabs: Li
title=title,
type=WidgetType.TABS.value,
size=size.value,
tabs=[TabInfo(str(uuid4()), tab.title, tab.widget) for tab in tabs],
tabs=[TabInfo(str(uuid7()), tab.title, tab.widget) for tab in tabs],
)


Expand Down
Loading

0 comments on commit 40817e1

Please sign in to comment.