Skip to content

Fix broken error-message formatting in flax.nnx#5334

Open
gdrosa wants to merge 1 commit intogoogle:mainfrom
gdrosa:fix/nnx-error-message-formatting
Open

Fix broken error-message formatting in flax.nnx#5334
gdrosa wants to merge 1 commit intogoogle:mainfrom
gdrosa:fix/nnx-error-message-formatting

Conversation

@gdrosa
Copy link
Copy Markdown

@gdrosa gdrosa commented Mar 16, 2026

Summary

  • Fix malformed f-string in filterlib.to_predicate: {filter:!r}{filter!r}
  • Add missing f prefix in variablelib.variable_name_from_type error string

Both bugs cause error paths to crash with confusing secondary errors instead of showing the intended messages.

Steps to reproduce

to_predicate (raises ValueError: Invalid format specifier '!r' instead of the intended TypeError):

import flax.nnx.filterlib as filterlib

filterlib.to_predicate(37)

variable_name_from_type (shows literal {name} placeholders instead of actual values):

from flax.nnx import Variable
from flax.nnx.variablelib import variable_name_from_type

class params(Variable):
    pass

variable_name_from_type(params, allow_register=True)

Test plan

  • Verified both fixes produce correct error messages on Flax 0.12.5, Python 3.11, CPU-only

Two string formatting bugs:

1. filterlib.to_predicate: `{filter:!r}` → `{filter!r}`
   (malformed f-string raised ValueError instead of intended TypeError)

   Reproduce:
     >>> import flax.nnx.filterlib as filterlib
     >>> filterlib.to_predicate(37)
     ValueError: Invalid format specifier '!r' for object of type 'int'

2. variablelib.variable_name_from_type: added missing `f` prefix
   (error showed literal `{name}` placeholders instead of actual values)

   Reproduce:
     >>> from flax.nnx import Variable
     >>> from flax.nnx.variablelib import variable_name_from_type
     >>> class params(Variable): pass
     >>> variable_name_from_type(params, allow_register=True)
     ValueError: Name {name} is already registered in the registry as
     {VariableTypeCache[name]}. It cannot be linked with this type {typ}.
@google-cla
Copy link
Copy Markdown

google-cla bot commented Mar 16, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses two distinct issues related to error message formatting within the flax.nnx library. By fixing malformed f-strings, the changes ensure that error paths now produce accurate and informative messages, preventing secondary errors and improving the clarity of diagnostics for developers.

Highlights

  • flax.nnx.filterlib error message: Corrected a malformed f-string in filterlib.to_predicate that caused a ValueError instead of the intended TypeError when an invalid collection filter was provided.
  • flax.nnx.variablelib error message: Added a missing f prefix to an f-string in variablelib.variable_name_from_type, ensuring that error messages correctly display variable names instead of literal placeholders.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • flax/nnx/filterlib.py
    • Corrected the f-string format specifier from {filter:!r} to {filter!r} in the TypeError message within to_predicate.
  • flax/nnx/variablelib.py
    • Added the f prefix to the ValueError message string in variable_name_from_type to enable proper f-string interpolation.
Activity
  • The author, gdrosa, created this pull request to fix two specific error message formatting bugs.
  • The author provided detailed steps to reproduce both issues and confirmed the fixes produce correct error messages on Flax 0.12.5, Python 3.11, CPU-only.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@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

This pull request addresses two formatting bugs in error messages within the flax.nnx module. The first change in flax/nnx/filterlib.py corrects an invalid f-string format specifier, preventing a ValueError during error handling. The second change in flax/nnx/variablelib.py adds the necessary f prefix to an error string to ensure proper interpolation of variables. Both fixes are correct and directly resolve the issues described. The changes are well-implemented and improve the robustness of error reporting.

Copy link
Copy Markdown
Collaborator

@vfdev-5 vfdev-5 left a comment

Choose a reason for hiding this comment

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

Thanks @gdrosa

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.

2 participants