Skip to content

Commit

Permalink
fix(enums): ensure consistent enum format in 3.11+ (#846)
Browse files Browse the repository at this point in the history
## Change Summary

closes #845

## Checklist

- [ ] Unit tests for the changes exist
- [ ] Tests pass without significant drop in coverage
- [ ] Documentation reflects changes where applicable
- [ ] Test snapshots have been
[updated](https://prisma-client-py.readthedocs.io/en/latest/contributing/contributing/#snapshot-tests)
if applicable

## Agreement

By submitting this pull request, I confirm that you can use, modify,
copy and redistribute this contribution, under the terms of your choice.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
RobertCraigie and pre-commit-ci[bot] committed Nov 30, 2023
1 parent 9aa12ef commit 3b7ec60
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 6 deletions.
9 changes: 9 additions & 0 deletions databases/tests/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ async def test_enum_create(client: Prisma) -> None:
record = await client.types.create({'enum': Role.ADMIN})
assert record.enum == Role.ADMIN

# ensure consistent format
assert str(record.enum) == 'ADMIN'
assert f'{record.enum}' == 'ADMIN'
assert '%s' % record.enum == 'ADMIN'

assert str(Role.ADMIN) == 'ADMIN'
assert f'{Role.ADMIN}' == 'ADMIN'
assert '%s' % Role.ADMIN == 'ADMIN'


# TODO: all other actions

Expand Down
1 change: 1 addition & 0 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ typing-extensions>=4.0.1
tomlkit
nodeenv
cached-property; python_version < '3.8'
StrEnum; python_version < '3.11'
11 changes: 11 additions & 0 deletions src/prisma/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,17 @@ def Field(*, env: str | None = None, **extra: Any) -> Any:
nodejs = None


if TYPE_CHECKING:
from enum import Enum

StrEnum = Enum
else:
try:
from enum import StrEnum
except ImportError:
from strenum import StrEnum


def removeprefix(string: str, prefix: str) -> str:
if string.startswith(prefix):
return string[len(prefix) :]
Expand Down
4 changes: 2 additions & 2 deletions src/prisma/generator/templates/enums.py.jinja
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{% include '_header.py.jinja' %}
# -- template enums.py.jinja --
from enum import Enum
from ._compat import StrEnum


{% for enum in dmmf.datamodel.enums %}
class {{ enum.name }}(str, Enum):
class {{ enum.name }}(StrEnum):
{% for value in enum.values %}
{{ value.name }} = '{{ value.name }}'
{% endfor %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ from typing_extensions import TypedDict, Literal

LiteralString = str
# -- template enums.py.jinja --
from enum import Enum
from ._compat import StrEnum


class ABeautifulEnum(str, Enum):
class ABeautifulEnum(StrEnum):
A = 'A'
B = 'B'
C = 'C'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ from typing_extensions import TypedDict, Literal

LiteralString = str
# -- template enums.py.jinja --
from enum import Enum
from ._compat import StrEnum


class ABeautifulEnum(str, Enum):
class ABeautifulEnum(StrEnum):
A = 'A'
B = 'B'
C = 'C'
Expand Down

0 comments on commit 3b7ec60

Please sign in to comment.