flet
flet copied to clipboard
✨ Refactor literals for Enums
Hey ! great work ! amazing what are you accomplish 🥳. It would be nice if instead of using literals e.g: ScrollMode we changed for using Enums so in the documentation examples you can write something like
page.scroll = ScrollMode.ALWAYS
and even better IDE will try to autocomplete and get a better development experience. what do you think ? Let me know if you are interesting and I'm willing to make the proper changes.
Great idea!
I'm thinking about backward compatibility and came up with this:
from enum import Enum
class Color(Enum):
RED = "red"
GREEN = "green"
BLUE = "blue"
class TestControl:
def __init__(self, color: Color) -> None:
self.color = color.value if isinstance(color, Color) else color
t1 = TestControl(Color.BLUE)
print(t1.color)
t2 = TestControl("blue")
print(t2.color)
So, initially both strings (as of now) and enums can be supported though strings after Flet module update will be errored while type checking. But it's design-time only, right?
ha ! I've just see i could create a discussion, sorry I didn't use to much that github feature. well I think in you example we need to add
class TestControl:
def __init__(self, color: Union[Color, str]) -> None:
self.color = color.value if isinstance(color, Color) else color
just to be compliant with the type checking.
Also (another question) if i use page.controls.append(t) it isn't thread safe ? if I'm right I think is worth to mention in the documentation. and this pattern of accessing to controls and updating is all around the controls so we can think in a more general thread safe to add controls. (maybe inherit)
Ah, that's a nice catch. Yes, control tree is not thread-safe. I did some experimenting with thread-safe tree in the past but it drastically increase complexity. I'd suggest to have a global lock on the entire tree (page) if you require thread safety. If you could point me to a robust thread-safe tree implementation for Python that would be helpful.
So, are you still willing to help with enums?
I don't have any experience with that kind of implementation but i could research, and yes ! this evening I'll open a pr with the refactor ! I'll let you known as soon as is finished ! do you have any suggestion to start writing tests for the sdk ?
Tests is another question! :) I'm thinking about doind intergration tests mostly. I don't have experience with Flutter integration tests, but there are some examples: https://github.com/flet-dev/flet/issues/143
Issue Should be closed.