flet-dev / flet

Flet enables developers to easily build realtime web, mobile and desktop apps in Python. No frontend experience required.
https://flet.dev
Apache License 2.0
11.55k stars 452 forks source link

Using `ft.colors.with_opacity` returns exception, should be warning #4336

Closed InesaFitsner closed 2 weeks ago

InesaFitsner commented 2 weeks ago

Duplicate Check

Describe the bug

This code used to work before:

tooltip_bgcolor=ft.Colors.with_opacity(0.5, ft.Colors.GREY_300)

Now it returns exception:

ERROR:asyncio:Future exception was never retrieved future: <Future finished exception=TypeError("'<=' not supported between instances of 'int' and 'Colors'")> Traceback (most recent call last): File "/Users/inesa/.pyenv/versions/3.12.6/lib/python3.12/concurrent/futures/thread.py", line 58, in run result = self.fn(*self.args, *self.kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/inesa/Library/Caches/pypoetry/virtualenvs/controls-gallery-WumZyC-d-py3.12/lib/python3.12/site-packages/flet/core/page.py", line 944, in wrapper handler(args) File "/Users/inesa/projects/flet-dev/examples/python/apps/controls-gallery/main.py", line 36, in route_change gallery_view.display_control_examples(route_list[0], route_list[1]) File "/Users/inesa/projects/flet-dev/examples/python/apps/controls-gallery/components/gallery_view.py", line 30, in display_control_examples self.examples_view.display( File "/Users/inesa/projects/flet-dev/examples/python/apps/controls-gallery/components/examples_view.py", line 55, in display content=example.example(), ^^^^^^^^^^^^^^^^^ File "/Users/inesa/projects/flet-dev/examples/python/apps/controls-gallery/examples/charts/barchart/01_barchart_1.py", line 87, in example tooltip_bgcolor=ft.colors.with_opacity(0.5, ft.Colors.GREY_300), ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/inesa/Library/Caches/pypoetry/virtualenvs/controls-gallery-WumZyC-d-py3.12/lib/python3.12/site-packages/flet/core/colors.py", line 60, in with_opacity assert 0 <= opacity <= 1, "opacity must be between 0 and 1"

Code sample

Code ```python import flet as ft def main(page: ft.Page): chart = ft.BarChart( bar_groups=[ ft.BarChartGroup( x=0, bar_rods=[ ft.BarChartRod( from_y=0, to_y=40, width=40, color=ft.colors.AMBER, tooltip="Apple", border_radius=0, ), ], ), ft.BarChartGroup( x=1, bar_rods=[ ft.BarChartRod( from_y=0, to_y=100, width=40, color=ft.colors.BLUE, tooltip="Blueberry", border_radius=0, ), ], ), ft.BarChartGroup( x=2, bar_rods=[ ft.BarChartRod( from_y=0, to_y=30, width=40, color=ft.colors.RED, tooltip="Cherry", border_radius=0, ), ], ), ft.BarChartGroup( x=3, bar_rods=[ ft.BarChartRod( from_y=0, to_y=60, width=40, color=ft.colors.ORANGE, tooltip="Orange", border_radius=0, ), ], ), ], border=ft.border.all(1, ft.colors.GREY_400), left_axis=ft.ChartAxis( labels_size=40, title=ft.Text("Fruit supply"), title_size=40 ), bottom_axis=ft.ChartAxis( labels=[ ft.ChartAxisLabel( value=0, label=ft.Container(ft.Text("Apple"), padding=10) ), ft.ChartAxisLabel( value=1, label=ft.Container(ft.Text("Blueberry"), padding=10) ), ft.ChartAxisLabel( value=2, label=ft.Container(ft.Text("Cherry"), padding=10) ), ft.ChartAxisLabel( value=3, label=ft.Container(ft.Text("Orange"), padding=10) ), ], labels_size=40, ), horizontal_grid_lines=ft.ChartGridLines( color=ft.colors.GREY_300, width=1, dash_pattern=[3, 3] ), tooltip_bgcolor=ft.colors.with_opacity(0.5, ft.colors.GREY_300), max_y=110, interactive=True, expand=True, ) page.add(chart) ft.app(main) ```

To reproduce

Run the repro code -> BarChart is not displayed, error in in log: Unhandled error processing page session : Traceback (most recent call last): File "/Users/inesa/projects/flet-dev/flet/sdk/python/packages/flet/src/flet/app.py", line 247, in on_session_created await asyncio.get_running_loop().run_in_executor( File "/Users/inesa/.pyenv/versions/3.12.6/lib/python3.12/concurrent/futures/thread.py", line 58, in run result = self.fn(*self.args, **self.kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/inesa/projects/flet-dev/flet/sdk/python/playground/bar_chart_test.py", line 84, in main tooltip_bgcolor=ft.colors.with_opacity(0.5, ft.colors.GREY_300), ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/inesa/projects/flet-dev/flet/sdk/python/packages/flet/src/flet/core/colors.py", line 60, in with_opacity assert 0 <= opacity <= 1, "opacity must be between 0 and 1" ^^^^^^^^^^^^^^^^^ TypeError: '<=' not supported between instances of 'int' and 'colors`

Expected behavior

Expected to see a warning

Screenshots / Videos

Captures [Upload media here]

Operating System

macOS

Operating system details

15

Flet version

0.25.0.dev3679

Regression

Yes, it used to work in a previous Flet version (please specify the version in additional details)

Suggestions

No response

Logs

Logs ```console [Paste your logs here] ```

Additional details

No response