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

Fix Qt6 events #139

Merged
merged 2 commits into from
Mar 11, 2024
Merged

Fix Qt6 events #139

merged 2 commits into from
Mar 11, 2024

Conversation

hanjinliu
Copy link
Owner

@hanjinliu hanjinliu commented Mar 11, 2024

This PR fixes bugs in Qt6, as Qt6 cannot use events to construct events.

Summary by CodeRabbit

  • Refactor
    • Improved type checking in event handling across various widgets to enhance stability and performance.

Copy link

coderabbitai bot commented Mar 11, 2024

Walkthrough

The recent updates across various widget files in the project primarily focus on improving type checking mechanisms. Instead of creating new instances or directly converting types, the changes advocate for the use of assert statements and isinstance checks to ensure the correct types are being handled. This shift enhances code safety and clarity by explicitly verifying type expectations before proceeding with further operations.

Changes

Files Change Summary
.../widgets/codeedit.py Replaced type conversion with assert for ev as QtGui.QKeyEvent.
.../widgets/color.py Changed type assertion from creating QHelpEvent to using isinstance for event check.
.../widgets/colormap.py Updated to assert a0 type as QtGui.QHelpEvent instead of type conversion.
.../widgets/eval.py Modified to include an assert statement ensuring event is an instance of QtGui.QKeyEvent.

🐇✨
In the land of code and widgetry,
A change was made, quite niftily.
No more casting, wild and free,
"Assert and check," they decree.
Safety first, the rabbit's plea.
🌟🐾

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 your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

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

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

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: 2

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 1170ecd and 8ce01d7.
Files selected for processing (4)
  • magicclass/widgets/codeedit.py (1 hunks)
  • magicclass/widgets/color.py (1 hunks)
  • magicclass/widgets/colormap.py (1 hunks)
  • magicclass/widgets/eval.py (1 hunks)
Additional comments: 3
magicclass/widgets/eval.py (1)
  • 212-212: The addition of assert isinstance(event, QtGui.QKeyEvent) ensures that the event being processed is indeed a key event. This is a good practice for type safety and clarity in the code. However, it's important to note that assertions can be disabled in Python when optimization is turned on (using the -O flag). If the type check is critical for the program's correctness or security, consider using explicit runtime checks that cannot be disabled.
magicclass/widgets/color.py (1)
  • 80-80: The change to use assert isinstance(event, QtGui.QHelpEvent) for type checking in the event method is consistent with the approach taken in other files of this PR. It enhances readability and ensures type safety. However, as mentioned earlier, consider the implications of assertions being disabled in optimized runs of Python and whether a more robust type checking mechanism is needed based on the criticality of this check.
magicclass/widgets/colormap.py (1)
  • 328-328: The addition of assert isinstance(a0, QtGui.QHelpEvent) in the event method is a good practice for ensuring that the event being processed is of the expected type. This change aligns with the type checking improvements seen in other parts of the PR. Remember to consider the potential for assertions to be disabled in optimized Python runs and evaluate if this type check is critical to the application's functionality or security.

@@ -140,7 +140,7 @@ def event(self, ev: QtCore.QEvent):
if ev.type() == QtCore.QEvent.Type.ToolTip:
return self._show_tooltip(ev)
elif ev.type() == QtCore.QEvent.Type.KeyPress:
ev = QtGui.QKeyEvent(ev)
assert isinstance(ev, QtGui.QKeyEvent)
Copy link

Choose a reason for hiding this comment

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

The change from creating a new instance of QtGui.QKeyEvent for type checking to using an assert statement with isinstance is a positive change in terms of performance and directness. However, it's important to note that assert statements can be disabled in optimized runs of Python (using the -O flag), which might lead to this type check being skipped. Consider if this behavior is acceptable or if an alternative approach that remains active in optimized runs is preferable.

Comment on lines 209 to 215

def event(self, event: QtCore.QEvent):
if event.type() == QtCore.QEvent.Type.KeyPress:
event = QtGui.QKeyEvent(event)
assert isinstance(event, QtGui.QKeyEvent)
if event.key() == Qt.Key.Key_Tab:
self.show_completion()
return True
Copy link

Choose a reason for hiding this comment

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

📝 NOTE
This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [167-167]

The use of eval is detected in this file. While eval can be powerful, it also poses a significant security risk if it's used to evaluate dynamic content that could be influenced by external sources. Ensure that the content being evaluated cannot be manipulated by external inputs to prevent code injection vulnerabilities. Consider alternative approaches if the evaluated content can come from untrusted sources.

Also applies to: 301-301

@hanjinliu hanjinliu merged commit ce08d15 into main Mar 11, 2024
17 checks passed
@hanjinliu hanjinliu deleted the qt6-event branch March 11, 2024 13:00
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