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 compatibility issue with python <3.11 #45

Merged
merged 2 commits into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion python/demo/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

from collections import defaultdict
from datetime import date
from enum import StrEnum
from typing import Annotated, Literal, TypeAlias

from fastapi import APIRouter, Request, UploadFile
from fastui import AnyComponent, FastUI
from fastui import components as c
from fastui.enums import StrEnum
from fastui.events import GoToEvent, PageEvent
from fastui.forms import FormFile, FormResponse, SelectSearchResponse, fastui_form
from httpx import AsyncClient
Expand Down
4 changes: 2 additions & 2 deletions python/fastui/components/display.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import enum
import typing
from abc import ABC

Expand All @@ -7,11 +6,12 @@

from .. import class_name as _class_name
from .. import events
from ..enums import StrEnum

__all__ = 'DisplayMode', 'DisplayLookup', 'Display', 'Details'


class DisplayMode(enum.StrEnum):
class DisplayMode(StrEnum):
"""
How to a value.
"""
Expand Down
34 changes: 34 additions & 0 deletions python/fastui/enums.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from enum import Enum


class StrEnum(str, Enum):
ManiMozaffar marked this conversation as resolved.
Show resolved Hide resolved
"""
Enum where members are also (and must be) strings
"""

def __new__(cls, *values):
if len(values) > 3:
raise TypeError(f'too many arguments for str(): {values!r}')

if len(values) == 1:
# it must be a string
if not isinstance(values[0], str):
raise TypeError(f'{values[0]!r} is not a string')
if len(values) >= 2:
# check that encoding argument is a string
if not isinstance(values[1], str):
raise TypeError(f'encoding must be a string, not {values[1]!r}')
if len(values) == 3:
# check that errors argument is a string
if not isinstance(values[2], str):
raise TypeError(f'errors must be a string, not {values[2]!r}')
value = str(*values)
member = str.__new__(cls, value)
member._value_ = value
return member

def _generate_next_value_(name, start, count, last_values):
"""
Return the lower-cased version of the member name.
"""
return name.lower()