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
10.97k stars 425 forks source link

Packaging fails when using type aliases with explicit `type` #4007

Open torablien opened 1 week ago

torablien commented 1 week ago

Duplicate Check

Describe the bug

When packaging a Flet app (e.g. via flet build macos), type aliases seem to cause build failures (but works fine when running with flet run). On smaller applications, this is flagged in verbose mode as a syntax error, but in larger applications (seemingly defined as ones with many dependencies even if no real logic), this fails opaquely.

Example error in smaller application:

Listing '/var/folders/q1/dcc1rnq13f9fhs4hxd03jqy80000gn/T/serious_python_temp1QKqZ2/__pypackages__/sniffio/_tests'...
Compiling '/var/folders/q1/dcc1rnq13f9fhs4hxd03jqy80000gn/T/serious_python_temp1QKqZ2/__pypackages__/sniffio/_tests/__init__.py'...
Compiling '/var/folders/q1/dcc1rnq13f9fhs4hxd03jqy80000gn/T/serious_python_temp1QKqZ2/__pypackages__/sniffio/_tests/test_sniffio.py'...
Compiling '/var/folders/q1/dcc1rnq13f9fhs4hxd03jqy80000gn/T/serious_python_temp1QKqZ2/__pypackages__/sniffio/_version.py'...
Listing '/var/folders/q1/dcc1rnq13f9fhs4hxd03jqy80000gn/T/serious_python_temp1QKqZ2/__pypackages__/sniffio-1.3.1.dist-info'...
Listing '/var/folders/q1/dcc1rnq13f9fhs4hxd03jqy80000gn/T/serious_python_temp1QKqZ2/assets'...
Compiling '/var/folders/q1/dcc1rnq13f9fhs4hxd03jqy80000gn/T/serious_python_temp1QKqZ2/main.py'...
***   File "/var/folders/q1/dcc1rnq13f9fhs4hxd03jqy80000gn/T/serious_python_temp1QKqZ2/main.py", line 4
    type Vector = list[float]
         ^^^^^^
SyntaxError: invalid syntax
[20:24:15] Deleting Flutter bootstrap directory /var/folders/q1/dcc1rnq13f9fhs4hxd03jqy80000gn/T/flet_flutter_build_Txn9fVXWcE                                                                                                                           
           Error building Flet app - see the log of failed command above.                                                                                                                                                                                
           Run subprocess: ['/Users/neil/Code/flutter/bin/flutter', 'doctor']      

Example error in larger application:

Compiling '/var/folders/q1/dcc1rnq13f9fhs4hxd03jqy80000gn/T/serious_python_tempWb6cOI/venv/lib/python3.12/site-packages/yaml/scanner.py'...
Compiling '/var/folders/q1/dcc1rnq13f9fhs4hxd03jqy80000gn/T/serious_python_tempWb6cOI/venv/lib/python3.12/site-packages/yaml/serializer.py'...
Compiling '/var/folders/q1/dcc1rnq13f9fhs4hxd03jqy80000gn/T/serious_python_tempWb6cOI/venv/lib/python3.12/site-packages/yaml/tokens.py'...
Listing '/var/folders/q1/dcc1rnq13f9fhs4hxd03jqy80000gn/T/serious_python_tempWb6cOI/venv/share'...
Listing '/var/folders/q1/dcc1rnq13f9fhs4hxd03jqy80000gn/T/serious_python_tempWb6cOI/venv/share/man'...
Listing '/var/folders/q1/dcc1rnq13f9fhs4hxd03jqy80000gn/T/serious_python_tempWb6cOI/venv/share/man/man1'...
[20:32:35] Deleting Flutter bootstrap directory /var/folders/q1/dcc1rnq13f9fhs4hxd03jqy80000gn/T/flet_flutter_build_JsOzp4wfPt                                                                                                                           
           Error building Flet app - see the log of failed command above.                                                                                                                                                                                
           Run subprocess: ['/Users/neil/Code/flutter/bin/flutter', 'doctor']       

Code sample

Running flet build macos -vv on simple Hello World app.

Failing:

import flet as ft

type Vector = list[float]

class VectorDB:
    def __init__(self):
        self.db: dict[str, Vector] = {}

def main(page: ft.Page):
    vector_db = VectorDB()
    page.add(ft.SafeArea(ft.Text("Hello, Flet!")))

ft.app(main)

Works:

import flet as ft

class VectorDB:
    def __init__(self):
        self.db: dict[str, list[float]] = {}

def main(page: ft.Page):
    vector_db = VectorDB()
    page.add(ft.SafeArea(ft.Text("Hello, Flet!")))

ft.app(main)

Both work when running via flet run

The only difference between smaller vs larger app re the missing error logs is the amount of requirements in requirements.txt. Small app has few to no requirements where the large app has many (even though the app remains just hello world).

To reproduce

Run flet create hello_world and add a type alias declaration to main.py (or any file). Then run flet build macos -vv.

Operating System

macOS - M1 Pro

Operating system details

Sonoma 14.5

Flet version

0.24.1

Python version

Running Python 3.12.3

Regression

I'm not sure / I don't know

torablien commented 5 days ago

Notably, this doesn't seem to fail if you just do Vector = list[float] instead of type Vector = list[float] (with the type statement). Python docs explicitly say to use the a type operator here but this workaround seems to work.

ndonkoHenri commented 5 days ago

This happens because type was introduced in Python 3.12, but when packaging an app atm, Py3.11.6 is used by Flet(serious-python) instead.

Upgrade to a higher Py version will happen through serious-python.

torablien commented 5 days ago

Thanks, that's helpful and also explains why I am running into various other SyntaxError issues like with @override which was also introduced in 3.12.

As a suggestion, maybe the build command can provide a warning if the current Python version is > than the one used in packaging?

ndonkoHenri commented 4 days ago

As a suggestion, maybe the build command can provide a warning if the current Python version is > than the one used in packaging?

Nice suggestion.

kentbull commented 17 hours ago

Is there anything I could do to help upgrade serious-python to Python 3.12.3+? I'd gladly submit a PR because I need this.

kentbull commented 16 hours ago

This may be addressed by https://github.com/flet-dev/serious-python/pull/109.

There are number of dependent PRs in Kivy/kivy-ios and python-for-android that need to be tested, merged, and added to serious-python in order to unblock this.