Skip to content

Migrate checkstrformat to use ErrorMessage class #7

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

arvi18
Copy link

@arvi18 arvi18 commented Apr 24, 2025

A granular reimplementation of python#12004

Changes

  • Migrates error codes in checkstrformat.py to use ErrorMessage objects stored in message_registry
  • Modifies fail method in MessageBuilder to accept ErrorMessage objects (the overload is similar to the one here).

Summary by CodeRabbit

  • New Features

    • Expanded and standardized error messages for string and bytes formatting issues, providing clearer diagnostics for formatting errors.
  • Refactor

    • Unified error message handling to use predefined message constants, improving consistency across formatting-related errors.
    • Enhanced error reporting to support richer message objects while maintaining backward compatibility.
  • Style

    • Added explicit type annotations to internal constants for improved code clarity and maintainability.

@arvi18
Copy link
Author

arvi18 commented Apr 24, 2025

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@arvi18
Copy link
Author

arvi18 commented Apr 24, 2025

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@arvi18
Copy link
Author

arvi18 commented Apr 24, 2025

Tagging @hauntsaninja since the self.fail() signature change is based on your code. PTAL?

Copy link

coderabbitai bot commented Apr 24, 2025

Walkthrough

This update standardizes and centralizes error message handling for string and bytes formatting errors in the codebase. All explicit error message strings in mypy/checkstrformat.py are replaced with references to predefined constants in message_registry.py. Numerous new error message constants are introduced in message_registry.py to cover a wide range of formatting-related errors. The fail method in MessageBuilder is enhanced to accept both plain strings and structured error message objects, extracting the appropriate message and error code as needed. Additionally, type annotations are added to error code constants in mypy/errorcodes.py.

Changes

File(s) Change Summary
mypy/checkstrformat.py Replaced all hardcoded error message strings for string and bytes formatting with references to new constants from message_registry; removed explicit error code arguments from error reporting calls. No logic changes.
mypy/message_registry.py Added a comprehensive set of new ErrorMessage constants for string and bytes formatting errors, each associated with appropriate error codes. No changes to existing messages.
mypy/messages.py Enhanced the fail method in MessageBuilder to accept either a plain string or an ErrorMessage object, extracting the message and error code as needed; added overloads for type safety.
mypy/errorcodes.py Added explicit Final[ErrorCode] type annotations to the STRING_FORMATTING and STR_BYTES_PY3 constants.

Poem

In fields of code where errors roam,
Now messages have found their home.
No more strings adrift and wild—
Each error message neatly filed.
Type hints bloom and overloads sing,
As rabbits celebrate this tidy spring!
🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 380cb8d and 45242d8.

📒 Files selected for processing (4)
  • mypy/checkstrformat.py (17 hunks)
  • mypy/errorcodes.py (1 hunks)
  • mypy/message_registry.py (1 hunks)
  • mypy/messages.py (3 hunks)
🧰 Additional context used
🪛 Ruff (0.8.2)
mypy/checkstrformat.py

904-906: Use a single if statement instead of nested if statements

(SIM102)

🔇 Additional comments (23)
mypy/errorcodes.py (2)

115-117: Well-structured type annotations added.

Adding explicit Final[ErrorCode] type annotations to the STRING_FORMATTING constant improves type safety and makes the type information more explicit for static analysis tools.


118-120: Well-structured type annotations added.

Adding explicit Final[ErrorCode] type annotations to the STR_BYTES_PY3 constant improves type safety, consistent with the change to STRING_FORMATTING.

mypy/checkstrformat.py (18)

202-203: Good conversion to centralized error messages.

Replacing hardcoded string literals with predefined error message constants from the message registry improves maintainability and consistency. The error code is now embedded in the error message constant.


206-207: Good conversion to centralized error messages.

Similar to the previous update, this uses a predefined error message constant instead of a hardcoded string.


217-218: Good conversion to centralized error messages.

The error message is now derived from a central constant, improving consistency and maintainability.


255-256: Good conversion to centralized error messages.

Replaced inline error string with a reference to the centralized error message constant.


270-271: Good conversion to centralized error messages.

Now using a centralized error message constant instead of a hardcoded string.


356-358: Good use of ErrorMessage.format() method.

Properly using the format method of ErrorMessage to create a new error message with formatted text while preserving the error code.


419-420: Good conversion to centralized error messages.

Replaced hardcoded error message with a reference to a predefined constant.


431-432: Good conversion to centralized error messages.

Properly converted to using the centralized error message constant.


445-446: Good use of ErrorMessage.format() method.

Using the format method properly to insert dynamic values into the error message.


452-453: Good use of ErrorMessage.format() method.

Properly using the format method to customize the error message with dynamic values.


526-527: Good conversion to centralized error messages.

Using a constant from the message registry instead of hardcoded string.


561-562: Good use of ErrorMessage.format() method.

Properly using the format method to include dynamic content in the error message.


611-614: Good use of ErrorMessage.format() method.

Correctly using the format method with multiple arguments to customize the error message.


750-751: Good conversion to centralized error messages.

Replaced hardcoded error message with centralized constant.


902-903: Good conversion to centralized error messages.

Replaced hardcoded error message with centralized constant.


907-908: Good conversion to centralized error messages.

Replaced hardcoded error message with centralized constant.


969-970: Good conversion to centralized error messages.

Replaced hardcoded error message with centralized constant.


972-973: Good conversion to centralized error messages.

Replaced hardcoded error message with centralized constant.

mypy/message_registry.py (1)

320-389: Well-structured error message definitions for string formatting.

The addition of these error message constants for string formatting and bytes formatting issues is a good practice. Each constant:

  1. Has a descriptive name with consistent prefix
  2. Includes an appropriate error message text
  3. Associates the correct error code (primarily STRING_FORMATTING or STR_BYTES_PY3)

This centralization makes error message management more maintainable and consistent.

mypy/messages.py (2)

272-296: Well-designed method overloading for the fail method.

Good addition of method overloads to accept both string messages and ErrorMessage objects. The overload signatures clearly document that:

  1. When using string messages, you can specify the error code explicitly
  2. When using ErrorMessage objects, the error code is taken from the object

This provides flexibility while maintaining clear type signatures.


297-314: Clean implementation for handling both message types.

The implementation properly detects the message type and extracts the appropriate values:

  1. For ErrorMessage objects, it extracts both the message text and error code
  2. For string messages, it uses the string directly with the provided code

This maintains backward compatibility while enabling the new usage pattern.

✨ 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.
    • Generate unit testing code 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 testing code 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 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 generate unit testing code.
    • @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.

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.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • 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/schema.v2.json

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.

@visz11
Copy link
Collaborator

visz11 commented Apr 24, 2025

@coderabbitai full review

Copy link

coderabbitai bot commented Apr 24, 2025

✅ Actions performed

Full review triggered.

@gandalf-repo
Copy link

/gemini review

@visz11
Copy link
Collaborator

visz11 commented Apr 24, 2025

/gemini

1 similar comment
@visz11
Copy link
Collaborator

visz11 commented Apr 24, 2025

/gemini

Copy link

It looks like you're trying to invoke a Gemini command by including /gemini in your comment. To invoke a command, you need to specify which command you want to run, e.g. /gemini review or /gemini summary. If you want a list of available commands, you can run /gemini help.

@visz11
Copy link
Collaborator

visz11 commented Apr 24, 2025

/gemini review

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

The pull request migrates error codes in checkstrformat.py to use ErrorMessage objects stored in message_registry. This change improves the consistency and maintainability of error messages related to string formatting. The use of predefined message constants makes it easier to update and manage error messages in the future. The code changes are well-structured and easy to follow.

Summary of Findings

  • Missing Error Code in UNRECOGNIZED_FORMAT_SPEC: The error message UNRECOGNIZED_FORMAT_SPEC in message_registry.py lacks an associated error code. While the other new error messages have codes.STRING_FORMATTING, this one is missing it, which could lead to inconsistencies in error reporting and filtering.

Merge Readiness

The pull request is well-structured and improves the consistency of error messages. However, the missing error code for UNRECOGNIZED_FORMAT_SPEC should be addressed before merging. I am unable to directly approve this pull request, and recommend that another reviewer also assesses these changes. After addressing the identified issue, the pull request will be ready for merging.

Comment on lines 353 to 359
):
# TODO: add support for some custom specs like datetime?
self.msg.fail(
"Unrecognized format" ' specification "{}"'.format(spec.format_spec[1:]),
message_registry.UNRECOGNIZED_FORMAT_SPEC.format(spec.format_spec[1:]),
call,
code=codes.STRING_FORMATTING,
)
continue

Choose a reason for hiding this comment

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

medium

Consider adding a code to the fail call, for consistency with the other calls in this file. This will also allow users to filter this error specifically.

                    self.msg.fail(
                        message_registry.UNRECOGNIZED_FORMAT_SPEC.format(spec.format_spec[1:])
                        call,
                        code=codes.STRING_FORMATTING,
                    )
                    continue

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.

4 participants