Skip to content

Add 'prefix' argument to temp_flip_flag function#5343

Open
samanklesaria wants to merge 1 commit intogoogle:mainfrom
samanklesaria:flip_flag_fix
Open

Add 'prefix' argument to temp_flip_flag function#5343
samanklesaria wants to merge 1 commit intogoogle:mainfrom
samanklesaria:flip_flag_fix

Conversation

@samanklesaria
Copy link
Collaborator

New functionality needs to often be tested in both graph mode and tree mode. Currently, setting graph mode is not possible with the temp_flip_flag function. This PR allows it to be used.

@samanklesaria samanklesaria marked this pull request as ready for review March 17, 2026 20:28
@gemini-code-assist
Copy link
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 enhances the temp_flip_flag context manager by introducing a configurable prefix for feature flags. This modification significantly improves the utility of the function, allowing it to temporarily adjust a wider range of configuration variables, which is particularly beneficial for testing new features across different operational modes like graph and tree modes.

Highlights

  • Enhanced temp_flip_flag function: The temp_flip_flag context manager now accepts an optional prefix argument, defaulting to 'flax'. This allows it to be used with configuration variables that do not use the 'flax_' prefix, such as those for graph mode testing.
  • Improved Flexibility for Testing: This change enables new functionality to be tested more easily in both graph mode and tree mode, addressing a previous limitation where setting graph mode was not possible with temp_flip_flag.

🧠 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/configurations.py
    • Modified the temp_flip_flag function signature to include a prefix argument with a default value of 'flax'.
    • Updated the internal logic of temp_flip_flag to use the provided prefix when constructing configuration variable names.
    • Added a missing newline character at the end of the file.
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
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 introduces a prefix argument to the temp_flip_flag function, making it more flexible for flipping feature flags with different prefixes. The implementation is straightforward. I've left a couple of comments regarding documentation and code style. Additionally, it would be beneficial to add a unit test case to tests/configurations_test.py to cover this new functionality with a non-default prefix.


@contextmanager
def temp_flip_flag(self, var_name: str, var_value: bool):
def temp_flip_flag(self, var_name: str, var_value: bool, prefix='flax'):
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The new prefix parameter should be documented in the function's docstring. Please add a description for prefix in the Args section. Additionally, the description for var_name is now outdated and should be updated to refer to the generic prefix instead of hardcoding 'flax_'.

Comment on lines +85 to +90
old_value = getattr(self, prefix + '_' + var_name)
try:
self.update(f'flax_{var_name}', var_value)
self.update(prefix + '_' + var_name, var_value)
yield
finally:
self.update(f'flax_{var_name}', old_value)
self.update(prefix + '_' + var_name, old_value)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

For consistency with the previous implementation and for better readability, it's preferable to use f-strings for constructing the flag name instead of string concatenation. This also makes the code less repetitive if you store the f-string in a variable.

Suggested change
old_value = getattr(self, prefix + '_' + var_name)
try:
self.update(f'flax_{var_name}', var_value)
self.update(prefix + '_' + var_name, var_value)
yield
finally:
self.update(f'flax_{var_name}', old_value)
self.update(prefix + '_' + var_name, old_value)
flag_name = f'{prefix}_{var_name}'
old_value = getattr(self, flag_name)
try:
self.update(flag_name, var_value)
yield
finally:
self.update(flag_name, old_value)

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.

1 participant