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

[Refactor] fix ruff rule B039: mutable-contextvar-default #49854

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

Conversation

LeoLiao123
Copy link
Contributor

Why are these changes needed?

Replace mutable default value with None to prevent shared state across ContextVar.get() calls

ref : https://docs.astral.sh/ruff/rules/mutable-contextvar-default/

Related issue number

#47991

Checks

  • I've signed off every commit(by using the -s flag, i.e., git commit -s) in this PR.
  • I've run scripts/format.sh to lint the changes in this PR.
  • I've included any doc changes needed for https://docs.ray.io/en/master/.
    • I've added any new APIs to the API Reference. For example, if I added a
      method in Tune, I've added it in doc/source/tune/api/ under the
      corresponding .rst file.
  • I've made sure the tests are passing. Note that there might be a few flaky tests, see the recent failures at https://flakey-tests.ray.io/
  • Testing Strategy
    • Unit tests
    • Release tests
    • This PR is not tested :(

@LeoLiao123
Copy link
Contributor Author

@MortalHappiness PTAL

@jcotant1 jcotant1 added the core Issues that should be addressed in Ray Core label Jan 15, 2025
@MortalHappiness MortalHappiness added the go add ONLY when ready to merge, run all tests label Jan 15, 2025
@@ -195,7 +195,7 @@ def _set_request_context(
"""Set the request context. If the value is not set,
the current context value will be used."""

current_request_context = _serve_request_context.get()
current_request_context = _serve_request_context.get() or _RequestContext()
Copy link
Member

Choose a reason for hiding this comment

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

It's better to follow the reference and check it with if current_request_context is None, as None is not the only value with a False boolean value in Python.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion. Updated to check with is None .

Copy link
Member

@MortalHappiness MortalHappiness Jan 16, 2025

Choose a reason for hiding this comment

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

Still need to update other files that use this variable. You can create an utility function like

def get_serve_request_context():
    if _serve_request_context.get() is None:
        _serve_request_context.set(_RequestContext())
    return serve_request_context.get()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've updated all related files and implemented the utility function for get_serve_request_context().

@aslonnie aslonnie requested a review from zcin January 16, 2025 06:52
@aslonnie aslonnie requested a review from edoakes January 16, 2025 19:08
Copy link
Collaborator

@aslonnie aslonnie left a comment

Choose a reason for hiding this comment

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

@zcin @edoakes could you help review & approve?

Copy link
Contributor

@edoakes edoakes left a comment

Choose a reason for hiding this comment

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

Looks great pending my one comment -- thanks for the fix!

Comment on lines 188 to 189
@DeveloperAPI
def get_serve_request_context():
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
@DeveloperAPI
def get_serve_request_context():
def _get_serve_request_context():

this is an internal API, not public at the moment

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed, thanks for the review.

…quest_context as an internal API

Signed-off-by: LeoLiao123 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Issues that should be addressed in Ray Core go add ONLY when ready to merge, run all tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants