Skip to content

Use the -O flag for improved performance in error handling #31 #141

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

Conversation

reyan-singh
Copy link
Collaborator

@reyan-singh reyan-singh commented Mar 29, 2025

Replacing common error code with more optimize code using assert / debug for production ready build along with maintaining error handling capabilities for development

Summary by CodeRabbit

  • Bug Fixes

    • Improved error handling so that certain exceptions are only raised in non-optimized (debug) mode, reducing interruptions in optimized runs.
    • Adjusted exception types and error messages for greater clarity when accessing uninitialized or restricted attributes.
  • Refactor

    • Updated internal logic to conditionally perform error checks based on Python's debug mode, streamlining runtime behavior.

reyan-singh and others added 2 commits March 29, 2025 12:53
Replacing common error code with more optimize code using assert / __debug__ for production ready build along with maintaining error handling capabilities for development
reyan-singh and others added 6 commits March 29, 2025 13:48
Copy link

codecov bot commented Mar 29, 2025

Codecov Report

Attention: Patch coverage is 36.36364% with 7 lines in your changes missing coverage. Please review.

Project coverage is 91.68%. Comparing base (836e31a) to head (3860981).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
mesa_frames/concrete/model.py 0.00% 5 Missing ⚠️
mesa_frames/abstract/agents.py 66.66% 1 Missing ⚠️
mesa_frames/concrete/agents.py 66.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #141      +/-   ##
==========================================
- Coverage   91.78%   91.68%   -0.10%     
==========================================
  Files          11       11              
  Lines        1680     1684       +4     
==========================================
+ Hits         1542     1544       +2     
- Misses        138      140       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@adamamer20 adamamer20 self-requested a review April 3, 2025 12:37
Copy link
Member

@adamamer20 adamamer20 left a comment

Choose a reason for hiding this comment

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

Use if __debug__ code blocks so when we raise an error to the user, it's actually informative

Updated assert code along with __debug__ to make code informtive while in debug mode but optimized when build with -O option
Copy link
Contributor

coderabbitai bot commented Apr 3, 2025

Walkthrough

The updates modify exception handling in several methods and properties across multiple modules. Exception raising is now conditional on Python's debug mode (if __debug__:), so these checks and errors are only active when not running with optimization (-O). Some exception types were also changed for accuracy.

Changes

File(s) Change Summary
examples/sugarscape_ig/ss_polars/agents.py Updated get_best_moves and _get_best_moves methods to raise NotImplementedError only when __debug__ is True.
mesa_frames/abstract/agents.py Modified AgentSetDF.__getattr__ to raise AttributeError (instead of RuntimeError) for "_agents" only if __debug__ is True.
mesa_frames/concrete/agents.py Updated AgentsDF.__getattr__ to raise AttributeError for underscore-prefixed attributes only when __debug__ is True.
mesa_frames/concrete/model.py Changed exception types and made error checks in the agents property getter and setter conditional on __debug__.

Poem

In debug mode, exceptions leap and bound,
But with -O, they're nowhere to be found!
Errors now hide, as rabbits in the grass,
Only in debug do they show their sass.
Hopping through code, we check and we learn—
In optimized fields, it's the user's turn!
🐇✨

Note

⚡️ AI Code Reviews for VS Code, Cursor, Windsurf

CodeRabbit now has a plugin for VS Code, Cursor and Windsurf. This brings AI code reviews directly in the code editor. Each commit is reviewed immediately, finding bugs before the PR is raised. Seamless context handoff to your AI code agent ensures that you can easily incorporate review feedback.
Learn more here.


Note

⚡️ Faster reviews with caching

CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.
Enjoy the performance boost—your workflow just got faster.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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>, please review it.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@reyan-singh
Copy link
Collaborator Author

@adamamer20 added debug code block and checks are passed

@adamamer20 adamamer20 self-requested a review April 19, 2025 14:52
Copy link
Member

@adamamer20 adamamer20 left a comment

Choose a reason for hiding this comment

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

Instead of assertions, raise informative errors to the user

modifying assert code with raise... to raise informative errors to the user
@reyan-singh
Copy link
Collaborator Author

Instead of assertions, raise informative errors to the user

done, used raise instead of assert

@adamamer20
Copy link
Member

Merge from main and remove the pandas file to be able to merge this to main

@reyan-singh
Copy link
Collaborator Author

Merge from main and remove the pandas file to be able to merge this to main

@adamamer20 I have merged the changes

@adamamer20 adamamer20 requested review from adamamer20 and Copilot May 20, 2025 09:17
@adamamer20
Copy link
Member

@coderabbitai review

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces performance optimizations by wrapping error checks in debug conditions so that certain exceptions are only raised in non-optimized (debug) mode. The key changes include:

  • Using debug blocks to conditionally raise exceptions in production builds.
  • Changing some exception types for missing initialization or attribute errors.
  • Applying the debug wraps across multiple modules, including model, abstract agents, and example agent implementations.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

File Description
mesa_frames/concrete/model.py Wrapped error checks in debug and changed the exception type on missing init
mesa_frames/concrete/agents.py Applied debug flag in getattr, potentially bypassing error checks
mesa_frames/abstract/agents.py Modified exception type in getattr when _agents is missing
examples/sugarscape_ig/ss_polars/agents.py Added debug conditions around NotImplementedError for unimplemented methods

Copy link
Contributor

@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.

Actionable comments posted: 2

🧹 Nitpick comments (5)
mesa_frames/concrete/model.py (2)

178-181: Good optimization for runtime error handling.

The change conditionally checks for missing attribute initialization only in debug mode, which aligns with the PR objective of performance optimization.

Consider using a more explicit error cascade by using raise ... from None:

if __debug__:  # Only execute in non-optimized mode
-   raise RuntimeError(
+   raise RuntimeError(
        "You haven't called super().__init__() in your model. Make sure to call it in your __init__ method."
-   )
+   ) from None
🧰 Tools
🪛 Ruff (0.11.9)

179-181: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)

🪛 GitHub Check: codecov/patch

[warning] 178-179: mesa_frames/concrete/model.py#L178-L179
Added lines #L178 - L179 were not covered by tests


185-188: Type validation optimization aligned with PR goals.

Making type checking conditional on debug mode is consistent with the optimization objective.

For better readability, you could simplify the nested if statements:

-if __debug__:  # Only execute in non-optimized mode
-   if not isinstance(agents, AgentsDF):
-       raise TypeError("agents must be an instance of AgentsDF")
+if __debug__ and not isinstance(agents, AgentsDF):  # Only execute in non-optimized mode
+   raise TypeError("agents must be an instance of AgentsDF")
🧰 Tools
🪛 Ruff (0.11.9)

185-186: Use a single if statement instead of nested if statements

(SIM102)

🪛 GitHub Check: codecov/patch

[warning] 185-187: mesa_frames/concrete/model.py#L185-L187
Added lines #L185 - L187 were not covered by tests

mesa_frames/concrete/agents.py (1)

442-446: Good optimization for attribute error handling.

The change conditionally raises AttributeError for private attributes only in debug mode, which aligns with the PR objective.

To improve readability, consider simplifying the nested if statements:

-# Avoids infinite recursion of private attributes
-if __debug__:  # Only execute in non-optimized mode
-   if name.startswith("_"):
-       raise AttributeError(
-           f"'{self.__class__.__name__}' object has no attribute '{name}'"
-       )
+# Avoids infinite recursion of private attributes
+if __debug__ and name.startswith("_"):  # Only execute in non-optimized mode
+   raise AttributeError(
+       f"'{self.__class__.__name__}' object has no attribute '{name}'"
+   )
🧰 Tools
🪛 Ruff (0.11.9)

442-443: Use a single if statement instead of nested if statements

(SIM102)

🪛 GitHub Check: codecov/patch

[warning] 444-444: mesa_frames/concrete/agents.py#L444
Added line #L444 was not covered by tests

mesa_frames/abstract/agents.py (1)

1001-1005: Good improvement for error handling optimization and type.

Converting from RuntimeError to AttributeError for missing attributes is more semantically appropriate, and making it conditional in debug mode aligns with the optimization goal.

To improve readability, consider simplifying the nested if statements:

-if __debug__:  # Only execute in non-optimized mode
-   if name == "_agents":
-       raise AttributeError(
-           "The _agents attribute is not set. You probably forgot to call super().__init__ in the __init__ method."
-       )
+if __debug__ and name == "_agents":  # Only execute in non-optimized mode
+   raise AttributeError(
+       "The _agents attribute is not set. You probably forgot to call super().__init__ in the __init__ method."
+   )
🧰 Tools
🪛 Ruff (0.11.9)

1001-1002: Use a single if statement instead of nested if statements

(SIM102)

🪛 GitHub Check: codecov/patch

[warning] 1003-1003: mesa_frames/abstract/agents.py#L1003
Added line #L1003 was not covered by tests

examples/sugarscape_ig/ss_polars/agents.py (1)

170-314: Document the effects of running with -O flag.

The optimization strategy is well-implemented, but could benefit from documentation explaining the behavior differences when running with the -O flag.

Consider adding a class-level docstring that explains the optimization strategy:

class AntPolarsBase(AgentSetPolars):
    """Base class for ant agents using Polars dataframes.
    
    Note: When running Python with the -O flag, certain runtime checks 
    (like NotImplementedError for abstract methods) are disabled for 
    performance optimization. Ensure all subclasses properly implement 
    required methods to avoid unexpected behavior in production.
    """
🧰 Tools
🪛 Ruff (0.11.9)

240-248: Do not assign a lambda expression, use a def

Rewrite map_batches_func as a def

(E731)


251-258: Do not assign a lambda expression, use a def

Rewrite map_batches_func as a def

(E731)

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 836e31a and 89a1583.

📒 Files selected for processing (4)
  • examples/sugarscape_ig/ss_polars/agents.py (2 hunks)
  • mesa_frames/abstract/agents.py (1 hunks)
  • mesa_frames/concrete/agents.py (1 hunks)
  • mesa_frames/concrete/model.py (1 hunks)
🧰 Additional context used
🪛 Ruff (0.11.9)
mesa_frames/concrete/model.py

179-181: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)


185-186: Use a single if statement instead of nested if statements

(SIM102)

mesa_frames/concrete/agents.py

442-443: Use a single if statement instead of nested if statements

(SIM102)

mesa_frames/abstract/agents.py

1001-1002: Use a single if statement instead of nested if statements

(SIM102)

🪛 GitHub Check: codecov/patch
mesa_frames/concrete/model.py

[warning] 178-179: mesa_frames/concrete/model.py#L178-L179
Added lines #L178 - L179 were not covered by tests


[warning] 185-187: mesa_frames/concrete/model.py#L185-L187
Added lines #L185 - L187 were not covered by tests

mesa_frames/concrete/agents.py

[warning] 444-444: mesa_frames/concrete/agents.py#L444
Added line #L444 was not covered by tests

mesa_frames/abstract/agents.py

[warning] 1003-1003: mesa_frames/abstract/agents.py#L1003
Added line #L1003 was not covered by tests

🔇 Additional comments (2)
examples/sugarscape_ig/ss_polars/agents.py (2)

170-171: Consistent application of optimization strategy.

Making NotImplementedError conditional on debug mode allows for performance optimization in production code.


313-314: Consistent application of optimization strategy.

Making NotImplementedError conditional on debug mode allows for performance optimization in production code.

Copy link
Member

@adamamer20 adamamer20 left a comment

Choose a reason for hiding this comment

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

We can merge this !

@adamamer20 adamamer20 linked an issue May 20, 2025 that may be closed by this pull request
@adamamer20 adamamer20 added this to the 0.1.0-stable milestone May 20, 2025
@adamamer20 adamamer20 added the feature New functionality added to the project. label May 20, 2025
@adamamer20 adamamer20 merged commit c1d83f1 into projectmesa:main May 20, 2025
7 of 9 checks passed
Ben-geo pushed a commit to Ben-geo/mesa-frames that referenced this pull request May 27, 2025
…a#31 (projectmesa#141)

* Replacing error code with optimized code

Replacing common error code with more optimize code using assert / __debug__ for production ready build along with maintaining error handling capabilities for development

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Updating few changes

fixing optimization code

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fixed build test

fixed build test

* fixed build code

fixed build code

* updating agent to fix build

updating agent to fix build

* Updated assert code along with __debug__

Updated assert code along with __debug__ to make code informtive while in debug mode but optimized when build with -O option

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Instead of assert raising informative errors

modifying assert code with raise... to raise informative errors to the user

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update examples/sugarscape_ig/ss_polars/agents.py

Co-authored-by: Copilot <[email protected]>

* Update examples/sugarscape_ig/ss_polars/agents.py

Co-authored-by: Copilot <[email protected]>

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Adam Amer <[email protected]>
Co-authored-by: Copilot <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New functionality added to the project.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use the -O flag for improved performance in error handling
2 participants