bug: obj.astimezone() raised OSError: [Errno 22] Invalid argument
Duplicate Check
- [x] I have searched the opened issues and there are no duplicates
Describe the bug
Flet encountered this error when handling a dialog containing a DateRangePicker.
Code sample
Code
import flet as ft
def main(page: ft.Page):
range_picker = ft.DateRangePicker()
page.add(range_picker)
page.show_dialog(range_picker)
if __name__ == "__main__":
ft.run(main)
To reproduce
- Run the repro code
- It fails
Expected behavior
No exceptions thrown
Screenshots / Videos
Captures
Operating System
Windows
Operating system details
Windows 11 25H2 (26200.7171)
Flet version
0.70.0.dev6999
Regression
I'm not sure / I don't know
Suggestions
No response
Logs
Logs
Unhandled error in main() handler
Traceback (most recent call last):
File "d:\[REDACTED]\.venv\Lib\site-packages\flet\app.py", line 269, in on_session_created
main(session.page)
~~~~^^^^^^^^^^^^^^
File "d:\[REDACTED]\src\include\ui\issues\daterange.py", line 6, in main
page.add(range_picker)
~~~~~~~~^^^^^^^^^^^^^^
File "d:\[REDACTED]\.venv\Lib\site-packages\flet\controls\base_page.py", line 284, in add
self.update()
~~~~~~~~~~~^^
File "d:\[REDACTED]\.venv\Lib\site-packages\flet\controls\page.py", line 448, in update
self.__update(self)
~~~~~~~~~~~~~^^^^^^
File "d:\[REDACTED]\.venv\Lib\site-packages\flet\controls\page.py", line 457, in __update
self.session.patch_control(c)
~~~~~~~~~~~~~~~~~~~~~~~~~~^^^
File "d:\[REDACTED]\.venv\Lib\site-packages\flet\messaging\session.py", line 138, in patch_control
self.__send_message(
~~~~~~~~~~~~~~~~~~~^
ClientMessage(
^^^^^^^^^^^^^^
...<2 lines>...
)
^
)
^
File "d:\[REDACTED]\.venv\Lib\site-packages\flet\messaging\session.py", line 281, in __send_message
self.__conn.send_message(message)
~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^
File "d:\[REDACTED]\.venv\Lib\site-packages\flet\messaging\flet_socket_server.py", line 221, in send_message
m = msgpack.packb(
[message.action, message.body],
default=configure_encode_object_for_msgpack(BaseControl),
)
File "d:\[REDACTED]\.venv\Lib\site-packages\msgpack\__init__.py", line 36, in packb
return Packer(**kwargs).pack(o)
~~~~~~~~~~~~~~~~~~~~~^^^
File "msgpack/_packer.pyx", line 279, in msgpack._cmsgpack.Packer.pack
File "msgpack/_packer.pyx", line 276, in msgpack._cmsgpack.Packer.pack
File "msgpack/_packer.pyx", line 265, in msgpack._cmsgpack.Packer._pack
File "msgpack/_packer.pyx", line 232, in msgpack._cmsgpack.Packer._pack_inner
File "msgpack/_packer.pyx", line 270, in msgpack._cmsgpack.Packer._pack
File "msgpack/_packer.pyx", line 213, in msgpack._cmsgpack.Packer._pack_inner
File "msgpack/_packer.pyx", line 265, in msgpack._cmsgpack.Packer._pack
File "msgpack/_packer.pyx", line 232, in msgpack._cmsgpack.Packer._pack_inner
File "msgpack/_packer.pyx", line 265, in msgpack._cmsgpack.Packer._pack
File "msgpack/_packer.pyx", line 232, in msgpack._cmsgpack.Packer._pack_inner
File "msgpack/_packer.pyx", line 265, in msgpack._cmsgpack.Packer._pack
File "msgpack/_packer.pyx", line 232, in msgpack._cmsgpack.Packer._pack_inner
File "msgpack/_packer.pyx", line 270, in msgpack._cmsgpack.Packer._pack
File "msgpack/_packer.pyx", line 213, in msgpack._cmsgpack.Packer._pack_inner
File "msgpack/_packer.pyx", line 267, in msgpack._cmsgpack.Packer._pack
File "d:\[REDACTED]\.venv\Lib\site-packages\flet\messaging\protocol.py", line 77, in encode_object_for_msgpack
obj = obj.astimezone()
OSError: [Errno 22] Invalid argument
Additional details
No response
This issue appears to be caused by the default value of first_date (1900-01-01).
Hum, it works normally on my end (macOS 15, 0.70.0.dev6999):
Can you please create a new venv and retry? (btw, no need to add the picker to the page - show_dialog is sufficient)
@ndonkoHenri Doesn't work. I'm using uv and Python 3.14 for the new venv. It's worth mentioning that I'm located in Asia.
I've found a way around this,... the issue is from the default value of first_date property which is year 1900. This error is known and it's from the python datetime module,... the year should be after 1970.
import datetime
import flet as ft
def main(page: ft.Page):
page.horizontal_alignment = ft.CrossAxisAlignment.CENTER
def handle_change(e: ft.Event[ft.DateRangePicker]):
page.add(
ft.Text(f"Start: {e.control.start_value.strftime('%m/%d/%Y')}", color=ft.Colors.AMBER_700),
ft.Text(f"End: {e.control.end_value.strftime('%m/%d/%Y')}", color=ft.Colors.BLUE_700),
)
def handle_dismissal(e: ft.Event[ft.DialogControl]):
page.add(ft.Text("DatePicker dismissed"))
today = datetime.datetime.now()
drp = ft.DateRangePicker(
start_value=datetime.datetime(today.year, today.month, 1),
end_value=datetime.datetime(today.year, today.month, 15),
first_date=datetime.datetime(1971, 1, 1),
on_change=handle_change,
on_dismiss=handle_dismissal,
)
page.add(
ft.Button(
content=ft.Text("Pick date"),
icon=ft.Icons.CALENDAR_MONTH,
on_click=lambda _: page.show_dialog(drp),
)
)
ft.run(main)
fyi: issue (https://github.com/python/cpython/issues/80940#issuecomment-3753889927) has been fixed, but will be 3.15+ only i.e. not backported.