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

Warn if widgets are used as class variables. #130

Merged
merged 2 commits into from
Dec 17, 2023
Merged

Conversation

hanjinliu
Copy link
Owner

@hanjinliu hanjinliu commented Nov 11, 2023

Related to #110.
Widgets should always be used inside fields.

Summary by CodeRabbit

  • New Features
    • Updated note about using the field function to create widgets in magic classes.
  • Bug Fixes
    • Adjusted attribute in Menu class for improved functionality.
  • Chores
    • Updated version from "0.7.6" to "0.7.7.dev0".

Copy link

coderabbitai bot commented Nov 11, 2023

Walkthrough

The magicclass package has been updated to version "0.7.7.dev0". The update introduces changes to the import statements and method logic in _gui/_base.py, emphasizing the use of FieldGroup and warning against using certain class attributes directly. Additionally, a note in quick_start.rst has been updated to caution against using widgets directly as class variables.

Changes

File Change Summary
magicclass/__init__.py Version updated from "0.7.6" to "0.7.7.dev0".
magicclass/_gui/_base.py
  • Import modification for FieldGroup.
  • Method logic adjustments and removal of class methods.
  • Minor control flow changes.
    |
    | rst/main/quick_start.rst | Updated note cautioning against using widgets directly as class variables and removal of a section. |

Poem

Code evolves in leaps and bounds, 🍂🍁

Magicclass dances to new sounds. 🧙‍♂️🚀

With FieldGroup in the mix, and warnings to fix, 🎉🐇

A whimsical update, a magical fix. 💫🎭

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on X ?


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • You can reply to a review comment made by CodeRabbit.
  • You can tag CodeRabbit on specific lines of code or files in the PR by tagging @coderabbitai in a comment.
  • You can tag @coderabbitai in a PR comment and ask one-off questions about the PR and the codebase. Use quoted replies to pass the context for follow-up questions.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between b01c440 and fa0bc2c.
Files selected for processing (3)
  • magicclass/init.py (1 hunks)
  • magicclass/_gui/_base.py (2 hunks)
  • tests/test_construction.py (2 hunks)
Files skipped from review due to trivial changes (2)
  • magicclass/init.py
  • tests/test_construction.py
Additional comments: 2
magicclass/_gui/_base.py (2)
  • 83-83: The import of FieldGroup has been added correctly.

  • 722-723: The check for a string "separator" and its conversion to a Separator widget is correct.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 6

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between fa0bc2c and 088a8f3.
Files ignored due to filter (1)
  • rst/main/images/fig_1-5.png
Files selected for processing (2)
  • magicclass/_gui/_base.py (5 hunks)
  • rst/main/quick_start.rst (2 hunks)
Additional comments: 4
magicclass/_gui/_base.py (2)
  • 83-83: The addition of the FieldGroup import aligns with the PR objective and the AI-generated summary.

  • 83-83: According to the AI-generated summary, the field and vfield methods have been removed. Ensure that this change does not affect any other parts of the codebase that may rely on these methods.

rst/main/quick_start.rst (2)
  • 93-94: The updated note correctly reflects the warning system implemented for using widgets as class variables. It directs users to the appropriate documentation for using field functions.

  • 264-269: The removal of the "Make Document of Your Widgets Automatically" section is not visible in the provided hunks, but based on the AI-generated summary, it appears to have been removed to align with the PR objectives.

Comment on lines +660 to +667
elif isinstance(attr, Widget) and not isinstance(attr, FieldGroup):
warnings.warn(
f"Widget {name!r} is given as a class attribute. This is not "
"recommended, as it is shared between all the instances. "
"Please use a field or vfield instead.",
UserWarning,
stacklevel=2,
)
Copy link

Choose a reason for hiding this comment

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

The warning message in _convert_an_attribute_into_widget now advises against using widgets as class variables, which aligns with the PR objective. However, the previous suggestion to include an example in the warning message has not been implemented. Consider adding an example to the warning message for clarity.

- "Please use a field or vfield instead.",
+ "Please use a field or vfield instead. For example, instead of 'my_widget = Widget()', use 'my_widget = field(Widget())'.",

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.

Suggested change
elif isinstance(attr, Widget) and not isinstance(attr, FieldGroup):
warnings.warn(
f"Widget {name!r} is given as a class attribute. This is not "
"recommended, as it is shared between all the instances. "
"Please use a field or vfield instead.",
UserWarning,
stacklevel=2,
)
elif isinstance(attr, Widget) and not isinstance(attr, FieldGroup):
warnings.warn(
f"Widget {name!r} is given as a class attribute. This is not "
"recommended, as it is shared between all the instances. "
"Please use a field or vfield instead. For example, instead of 'my_widget = Widget()', use 'my_widget = field(Widget())'.",
UserWarning,
stacklevel=2,
)

The __init_subclass__ method in BaseGui is empty and does not seem to serve any purpose. Consider removing it if it's not intended to override any behavior.

-    def __init_subclass__(cls, **kwargs):
-        pass

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.

Suggested change
elif isinstance(attr, Widget) and not isinstance(attr, FieldGroup):
warnings.warn(
f"Widget {name!r} is given as a class attribute. This is not "
"recommended, as it is shared between all the instances. "
"Please use a field or vfield instead.",
UserWarning,
stacklevel=2,
)

In the __init_subclass__ method of MagicTemplate, ensure that super().__init_subclass__(**kwargs) is called to maintain proper inheritance behavior.

def __init_subclass__(cls, **kwargs):
+    super().__init_subclass__(**kwargs)
    check_override(cls)

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.

Suggested change
elif isinstance(attr, Widget) and not isinstance(attr, FieldGroup):
warnings.warn(
f"Widget {name!r} is given as a class attribute. This is not "
"recommended, as it is shared between all the instances. "
"Please use a field or vfield instead.",
UserWarning,
stacklevel=2,
)
elif isinstance(attr, Widget) and not isinstance(attr, FieldGroup):
super().__init_subclass__(**kwargs)
warnings.warn(
f"Widget {name!r} is given as a class attribute. This is not "
"recommended, as it is shared between all the instances. "
"Please use a field or vfield instead.",
UserWarning,
stacklevel=2,
)

Consider adding an __init_subclass__ method to ContainerLikeGui for consistency and to ensure proper inheritance behavior, especially if check_override or other subclass initialization logic is required.


Consider adding an __init_subclass__ method to ContainerLikeGui for consistency and to ensure proper inheritance behavior, especially if check_override or other subclass initialization logic is required.


Consider adding an __init_subclass__ method to ContainerLikeGui for consistency and to ensure proper inheritance behavior, especially if check_override or other subclass initialization logic is required.

@hanjinliu hanjinliu merged commit dbbad01 into main Dec 17, 2023
16 checks passed
@hanjinliu hanjinliu deleted the warn-class-var branch December 17, 2023 02:14
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

Successfully merging this pull request may close these issues.

None yet

1 participant