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

ValueError: mutable default <class 'flet.alignment.Alignment'> for field begin is not allowed: use default_factory #173

Closed
LeMeteore opened this issue Aug 9, 2022 · 4 comments

Comments

@LeMeteore
Copy link

Context:

  • Ubuntu 21.10
  • flet 0.1.48
  • Python 3.11.0b4

I was trying to follow the introduction and I received this traceback:

>> python3 app.py 
Traceback (most recent call last):
  File "/home/nsukami/GIT/flet_example/app.py", line 1, in <module>
    import flet
  File "/home/nsukami/vrtlnvs/flet/lib/python3.11/site-packages/flet/__init__.py", line 11, in <module>
    from flet.container import Container
  File "/home/nsukami/vrtlnvs/flet/lib/python3.11/site-packages/flet/container.py", line 9, in <module>
    from flet.gradients import Gradient
  File "/home/nsukami/vrtlnvs/flet/lib/python3.11/site-packages/flet/gradients.py", line 25, in <module>
    @dataclasses.dataclass
     ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dataclasses.py", line 1221, in dataclass
    return wrap(cls)
           ^^^^^^^^^
  File "/usr/local/lib/python3.11/dataclasses.py", line 1211, in wrap
    return _process_class(cls, init, repr, eq, order, unsafe_hash,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dataclasses.py", line 959, in _process_class
    cls_fields.append(_get_field(cls, name, type, kw_only))
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dataclasses.py", line 816, in _get_field
    raise ValueError(f'mutable default {type(f.default)} for field '
ValueError: mutable default <class 'flet.alignment.Alignment'> for field begin is not allowed: use default_factory
(flet) ~/GIT/flet_example 

Is there something I'm doing correctly ?

The only Python class with an attribute named begin is here. If there is really a bug somewhere, is there anything I can do to help you fix this ?

o/

@FeodorFitsner
Copy link
Contributor

Thanks for reporting that!

Could you please try replacing begin: Alignment = field(default=alignment.center_left) with begin: Alignment = field(default_factory=alignment.center_left) in "/home/nsukami/vrtlnvs/flet/lib/python3.11/site-packages/flet/gradients.py", line 27 to see if that fixes that error?

@FeodorFitsner
Copy link
Contributor

This explains an issue more:
https://stackoverflow.com/a/52064202
https://stackoverflow.com/a/53633297

I'm not sure why it's not reproducible with Python 3.10, but definitely it looks like a bug.

@LeMeteore
Copy link
Author

Could you please try replacing [...] to see if that fixes that error?

I've made the following modifications inside /home/nsukami/vrtlnvs/flet/lib/python3.11/site-packages/flet/gradients.py and the error was fixed:

@dataclasses.dataclass
class LinearGradient(Gradient):
    # begin: Alignment = field(default=alignment.center_left)
    begin: Alignment = field(default_factory=alignment.center_left)
    # end: Alignment = field(default=alignment.center_right)
    end: Alignment = field(default_factory=alignment.center_right)
    type: str = field(default="linear")


@dataclasses.dataclass
class RadialGradient(Gradient):
    # center: Alignment = field(default=alignment.center)
    center: Alignment = field(default_factory=alignment.center)
    radius: Union[float, int] = field(default=0.5)
    focal: Alignment = field(default=None)
    focal_radius: Union[float, int] = field(default=0.0)
    type: str = field(default="radial")


@dataclasses.dataclass
class SweepGradient(Gradient):
    # center: Alignment = field(default=alignment.center)
    center: Alignment = field(default_factory=alignment.center)
    start_angle: Union[float, int] = field(default=0.0)
    end_angle: Union[float, int] = field(default=math.pi * 2)
    type: str = field(default="sweep")

@FeodorFitsner
Copy link
Contributor

So, it's been fixed in https://pypi.org/project/flet/0.1.49/ - please update flet module and give it another try.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants