Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types): remove definition of StrEnum as Enum #881

Merged
merged 9 commits into from
Jan 28, 2024
19 changes: 15 additions & 4 deletions src/prisma/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,14 +339,25 @@ def Field(*, env: str | None = None, **extra: Any) -> Any:
nodejs = None


# Note: this shim is due to an inconsistency with string enums
# that was fixed in Python3.11, for reference see:
# - https://blog.pecar.me/python-enum#there-be-dragons
# - https://github.com/python/cpython/issues/100458
if TYPE_CHECKING:
from enum import Enum
if sys.version_info >= (3, 11):
from enum import StrEnum as StrEnum
else:
# Note: we have to define our own `StrEnum`
# class as the backport we're using doesn't
# define good types.
from enum import Enum

StrEnum = Enum
class StrEnum(str, Enum):
...
else:
try:
if sys.version_info >= (3, 11):
from enum import StrEnum as StrEnum
except ImportError:
else:
from strenum import StrEnum as StrEnum


Expand Down
1 change: 1 addition & 0 deletions typesafety/pyright/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ model Types {
json_obj Json @default("{}")
datetime DateTime @default(now())
decimal Decimal @default(22.99)
role Role @default(USER)
}

model Lists {
Expand Down
20 changes: 20 additions & 0 deletions typesafety/pyright/types/enum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from prisma import Prisma
from prisma.enums import Role


async def filtering(client: Prisma) -> None:
# case: all valid filter fields
await client.types.find_first(
where={
'role': Role.USER,
},
)


def use_str_enum_as_str():
# case: StrEnum is compatible with str typing
_test_string: str = Role.USER


def raise_error_on_invalid_type():
_test_int: int = Role.USER # E: Expression of type "Literal[Role.USER]" cannot be assigned to declared type "int"
Loading