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

[兼容性问题] 在python3.10及以上版本 dataclass 的处理方式问题 #203

Open
wang-zhuo996 opened this issue Dec 10, 2024 · 6 comments

Comments

@wang-zhuo996
Copy link

在python 3.10及以上版本,直接使用QColor 会导致出现ValueError 运行失败
image
image

@ChinaIceF
Copy link
Owner

请提供报错信息

@wang-zhuo996
Copy link
Author

请提供报错信息

Traceback (most recent call last):
  File "e:\_Project\PyQt-SiliconUI\examples\Gallery for siui\start.py", line 6, in <module>
    from ui import MySiliconApp
  File "e:\_Project\PyQt-SiliconUI\examples\Gallery for siui\ui.py", line 10, in <module>
    from components.page_refactor import RefactoredWidgets
  File "e:\_Project\PyQt-SiliconUI\examples\Gallery for siui\components\page_refactor\__init__.py", line 1, in <module>
    from .page_refactor import RefactoredWidgets
  File "e:\_Project\PyQt-SiliconUI\examples\Gallery for siui\components\page_refactor\page_refactor.py", line 8, in <module>
    from siui.components.button import SiPushButtonRefactor, SiProgressPushButton, SiLongPressButtonRefactor, SiFlatButton, \
  File "C:\Users\4070TiSpuer\.pyenv\pyenv-win\versions\3.12.7\Lib\site-packages\pyqt_siliconui-1.0.1-py3.12.egg\siui\components\button.py", line 798, in <module>
    @dataclass
     ^^^^^^^^^
  File "%USERPROFILE%\.pyenv\pyenv-win\versions\3.12.7\Lib\dataclasses.py", line 1275, in dataclass
    return wrap(cls)
           ^^^^^^^^^
  File "%USERPROFILE%\.pyenv\pyenv-win\versions\3.12.7\Lib\dataclasses.py", line 1265, in wrap
    return _process_class(cls, init, repr, eq, order, unsafe_hash,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "%USERPROFILE%\.pyenv\pyenv-win\versions\3.12.7\Lib\dataclasses.py", line 994, in _process_class
    cls_fields.append(_get_field(cls, name, type, kw_only))
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "%USERPROFILE%\.pyenv\pyenv-win\versions\3.12.7\Lib\dataclasses.py", line 852, in _get_field
    raise ValueError(f'mutable default {type(f.default)} for field '
ValueError: mutable default <class 'PyQt5.QtGui.QColor'> for field background_color_starting is not allowed: use default_factory

button.py 文件还有几处使用dataclass装饰器的类有类似错误
SwitchStyleData RadioButtonStyleData
还有一个存在于 文件 slider_.py 的 类 CoordinatePickerStyleData

@UF4OVER
Copy link

UF4OVER commented Dec 10, 2024

可是我的也是3.10版本,但是正常运行

@rainzee
Copy link
Collaborator

rainzee commented Dec 11, 2024

后续应该彻底避免直接构造 QColor 而是使用元数据

@ChaXxl
Copy link

ChaXxl commented Dec 26, 2024

Python 3.12 同样的报错

@ChaXxl
Copy link

ChaXxl commented Dec 26, 2024

将所有在 dataclass 里面涉及 QColor 的代码改成这样就行了:

from dataclasses import dataclass, field

...

class RadioButtonStyleData(QObject):

    STYLE_TYPES = ["Button"]

    # text_color = QColor("#D1CBD4")
    # description_color = QColor("#918497")

    # 替换为 default_factory
    text_color: QColor = field(default_factory=lambda: QColor("#D1CBD4"))
    description_color: QColor = field(default_factory=lambda: QColor("#918497"))

...

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

5 participants