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

Don't mutate dict in Annotated #143

Merged
merged 2 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 22 additions & 20 deletions magicclass/_gui/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
)
from macrokit import Symbol

from .keybinding import as_shortcut
from .mgui_ext import (
from magicclass._gui.keybinding import as_shortcut
from magicclass._gui.mgui_ext import (
AbstractAction,
FunctionGuiPlus,
Action,
Expand All @@ -51,22 +51,22 @@
_LabeledWidgetAction,
mguiLike,
)
from .utils import (
from magicclass._gui.utils import (
copy_class,
callable_to_classes,
show_dialog_from_mgui,
connect_magicclasses,
)
from ._macro import GuiMacro, DummyMacro
from ._macro_utils import (
from magicclass._gui._macro import GuiMacro, DummyMacro
from magicclass._gui._macro_utils import (
inject_recorder,
inject_silencer,
inject_validator_only,
value_widget_callback,
MagicGuiPostRunCallback,
)
from ._icon import get_icon
from ._gui_modes import PopUpMode, ErrorMode
from magicclass._gui._icon import get_icon
from magicclass._gui._gui_modes import PopUpMode, ErrorMode

from magicclass.utils import (
get_signature,
Expand Down Expand Up @@ -107,6 +107,7 @@
"macro-signature-check": True,
"macro-name-check": True,
"undo-max-history": 100,
"raise-conversion-error": False,
}

_RESERVED = frozenset(
Expand Down Expand Up @@ -1053,15 +1054,9 @@ def func(*args, **kwargs):
)

if isinstance(param, MagicParameter):
_param = MagicParameter(
name=param.name,
kind=param.kind,
default=param.default,
annotation=split_annotated_type(param.annotation)[0],
gui_options=param.options.copy(),
)
_arg_bind = _param.options.get("bind", None)
_arg_choices = _param.options.get("choices", None)
_arg_bind = param.options.get("bind", None)
_arg_choices = param.options.get("choices", None)
_new_option = param.options.copy()

# If bound method is a class method, use self.method(widget).
if isinstance(_arg_bind, str):
Expand All @@ -1081,19 +1076,26 @@ def func(*args, **kwargs):
_arg_bind = _arg_bind.eval(type(self))

if is_instance_method(_arg_bind):
_param.options["bind"] = method_as_getter(self, _arg_bind)
_new_option["bind"] = method_as_getter(self, _arg_bind)

# If a MagicFiled is bound, bind the value of the connected widget.
elif isinstance(_arg_bind, MagicField):
_param.options["bind"] = _arg_bind.as_remote_getter(self)
_new_option["bind"] = _arg_bind.as_remote_getter(self)

# If choices are provided by a class method, use self.method(widget).
if isinstance(_arg_choices, str):
_arg_choices = eval_attribute(type(self), _arg_choices)

if is_instance_method(_arg_choices):
_param.options["choices"] = method_as_getter(self, _arg_choices)

_new_option["choices"] = method_as_getter(self, _arg_choices)

_param = MagicParameter(
name=param.name,
kind=param.kind,
default=param.default,
annotation=split_annotated_type(param.annotation)[0],
gui_options=_new_option,
)
else:
_param = param

Expand Down
4 changes: 4 additions & 0 deletions magicclass/_gui/class_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
PopUpMode,
ErrorMode,
normalize_insertion,
defaults,
)
from .utils import format_error, connect_magicclasses
from ._macro_utils import value_widget_callback
Expand Down Expand Up @@ -227,10 +228,13 @@ def _convert_attributes_into_widgets(self):
# construction.
widget = self._create_widget_from_method(widget)
except AttributeError as e:
if defaults["raise-conversion-error"]:
raise
warnings.warn(
Comment on lines +231 to 233
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a stacklevel argument to the warnings.warn call to improve the debuggability of the warning.

- warnings.warn(
+ warnings.warn(
+   stacklevel=2,
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if defaults["raise-conversion-error"]:
raise
warnings.warn(
if defaults["raise-conversion-error"]:
raise
warnings.warn(
stacklevel=2,
Tools
Ruff

233-233: No explicit stacklevel keyword argument found (B028)

f"Could not convert {widget!r} into a widget "
f"due to AttributeError: {e}",
UserWarning,
stacklevel=2,
)
continue

Expand Down
Loading