From 75263045c8dc54b704b94f5e5df75bf4167af6e2 Mon Sep 17 00:00:00 2001 From: Brian Kohan Date: Tue, 10 Sep 2024 16:12:19 -0700 Subject: [PATCH] fix tests for django-enum >= 2.0 --- doc/source/templatetags.rst | 11 ++++++++--- pyproject.toml | 2 +- tests/enum_app/enums.py | 8 +++++--- tests/enum_app/models.py | 22 +++++++++++++++++----- tests/examples/models.py | 18 ++++++++++++++---- tests/traverse/models.py | 2 +- 6 files changed, 46 insertions(+), 17 deletions(-) diff --git a/doc/source/templatetags.rst b/doc/source/templatetags.rst index 97eac93..8e95a9c 100644 --- a/doc/source/templatetags.rst +++ b/doc/source/templatetags.rst @@ -527,13 +527,18 @@ we might define a simple color enumeration like so: .. code:: python + import typing as t from django.db import models - from django_enum import EnumField, TextChoices - from enum_properties import p, s + from django_enum import EnumField + from django_enum.choices import TextChoices + from enum_properties import Symmetric, s class ExampleModel(models.Model): - class Color(TextChoices, s('rgb'), s('hex', case_fold=True)): + class Color(TextChoices): + + rgb: t.Annotated[t.Tuple[int, int, int], Symmetric()] + hex: t.Annotated[str, Symmetric(case_fold=True)] # name value label rgb hex RED = 'R', 'Red', (1, 0, 0), 'ff0000' diff --git a/pyproject.toml b/pyproject.toml index a1b418a..63befba 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -52,7 +52,7 @@ PyYAML = { version = ">=5.1,<7.0", optional = true } django-typer = "^2.1.1" [tool.poetry.group.dev.dependencies] -django-enum = ">=1.1.0" +django-enum = ">=2.0.0" enum-properties = ">=1.1.1" pytest = "^8.0" pytest-django = ">=4.8.0" diff --git a/tests/enum_app/enums.py b/tests/enum_app/enums.py index f18b582..3dee47f 100644 --- a/tests/enum_app/enums.py +++ b/tests/enum_app/enums.py @@ -1,6 +1,6 @@ from enum import Enum - -from enum_properties import EnumProperties, s +from typing_extensions import Annotated +from enum_properties import EnumProperties, Symmetric class IndependentEnum(Enum): @@ -9,7 +9,9 @@ class IndependentEnum(Enum): VALUE2 = 22 -class DependentEnum(EnumProperties, s("indep")): +class DependentEnum(EnumProperties): + indep: Annotated[IndependentEnum, Symmetric()] + VALUE0 = 0, IndependentEnum.VALUE2 VALUE1 = 1, IndependentEnum.VALUE1 VALUE2 = 2, IndependentEnum.VALUE0 diff --git a/tests/enum_app/models.py b/tests/enum_app/models.py index 4c22f3b..8f52d18 100644 --- a/tests/enum_app/models.py +++ b/tests/enum_app/models.py @@ -1,6 +1,9 @@ +import typing as t +from typing_extensions import Annotated from django.db import models -from django_enum import EnumField, IntegerChoices, TextChoices -from enum_properties import p, s +from django_enum import EnumField +from django_enum.choices import IntegerChoices, TextChoices +from enum_properties import Symmetric, s try: from django.utils.decorators import classproperty @@ -12,7 +15,10 @@ class EnumTester(models.Model): class NotAnEnum: pass - class Color(TextChoices, s("rgb"), s("hex", case_fold=True)): + class Color(TextChoices): + rgb: Annotated[t.Tuple[int, int, int], Symmetric()] + hex: Annotated[str, Symmetric(case_fold=True)] + # name value label rgb hex RED = "R", "Red", (1, 0, 0), "ff0000" GREEN = "G", "Green", (0, 1, 0), "00ff00" @@ -22,13 +28,16 @@ class Color(TextChoices, s("rgb"), s("hex", case_fold=True)): def class_name(cls): return cls.__name__ - class MapBoxStyle(IntegerChoices, s("slug", case_fold=True), p("version")): + class MapBoxStyle(IntegerChoices): """ https://docs.mapbox.com/api/maps/styles/ """ _symmetric_builtins_ = ["name", "uri", "label"] + slug: Annotated[str, Symmetric(case_fold=True)] + version: int + # name value label slug version STREETS = 1, "Streets", "streets", 11 OUTDOORS = 2, "Outdoors", "outdoors", 11 @@ -54,9 +63,12 @@ def docs(cls): # def __str__(self): # return self.uri - class AddressRoute(TextChoices, s("alt", case_fold=True), p("str")): + class AddressRoute(TextChoices): _symmetric_builtins_ = [s("name", case_fold=True)] + alt: Annotated[t.List[str], Symmetric(case_fold=True)] + str: str + # name value alt ALLEY = ( "ALY", diff --git a/tests/examples/models.py b/tests/examples/models.py index 38abcb3..002a188 100644 --- a/tests/examples/models.py +++ b/tests/examples/models.py @@ -1,6 +1,9 @@ +import typing as t from django.db import models -from django_enum import EnumField, IntegerChoices, TextChoices -from enum_properties import p, s +from django_enum import EnumField +from django_enum.choices import IntegerChoices, TextChoices +from enum_properties import Symmetric, s +from typing_extensions import Annotated try: from django.utils.decorators import classproperty @@ -16,19 +19,26 @@ class ExampleModel(models.Model): define_field = models.CharField(choices=DEFINES, max_length=2) - class Color(TextChoices, s("rgb"), s("hex", case_fold=True)): + class Color(TextChoices): + + rgb: Annotated[t.Tuple[int, int, int], Symmetric()] + hex: Annotated[str, Symmetric(case_fold=True)] + # name value label rgb hex RED = "R", "Red", (1, 0, 0), "ff0000" GREEN = "G", "Green", (0, 1, 0), "00ff00" BLUE = "B", "Blue", (0, 0, 1), "0000ff" - class MapBoxStyle(IntegerChoices, s("slug", case_fold=True), p("version")): + class MapBoxStyle(IntegerChoices): """ https://docs.mapbox.com/api/maps/styles/ """ _symmetric_builtins_ = ["name", "uri", "label"] + slug: Annotated[str, Symmetric(case_fold=True)] + version: int + # name value label slug version STREETS = 1, "Streets", "streets", 11 OUTDOORS = 2, "Outdoors", "outdoors", 11 diff --git a/tests/traverse/models.py b/tests/traverse/models.py index 9e3ea6d..5276ee0 100644 --- a/tests/traverse/models.py +++ b/tests/traverse/models.py @@ -1,4 +1,4 @@ -from django_enum import IntegerChoices, TextChoices +from django_enum.choices import IntegerChoices, TextChoices from enum_properties import p, s