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

[Data] Replace lambda mutable default arguments #46493

Conversation

peytondmurray
Copy link
Contributor

@peytondmurray peytondmurray commented Jul 9, 2024

Why are these changes needed?

Across the code base there are a number of instances where lambda functions were being used as default arguments. Our documentation build system doesn't handle these correctly, resulting in rendering issues with docs. Additionally, functions are mutable, which means that these can have really weird/unintended behavior that is hard to debug.

This PR changes all the instances I could find where we were using a lambda function as a default argument and swaps it out according to the usual if arg is None: arg = <lambda function> pattern.

@angelinalg Can you confirm this fixes the documentation side of things for the functions that are part of the public API?

Related issue number

Partially addresses #45129.

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 :(

Signed-off-by: pdmurray <peynmurray@gmail.com>
@peytondmurray peytondmurray added P1 Issue that should be fixed within a few weeks docs An issue or change related to documentation data Ray Data-related issues labels Jul 9, 2024
@peytondmurray
Copy link
Contributor Author

Before:

image

After

image

@angelinalg
Copy link
Contributor

Wow! I didn't even know how broken it was when I reported it. Much better! Confirmed! Thanks.
cc: @can-anyscale

Copy link
Member

@bveeramani bveeramani left a comment

Choose a reason for hiding this comment

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

Nice

@bveeramani
Copy link
Member

@peytondmurray for my own understanding, could you help me understand how functions are mutable? I understand they're objects and you can assign attributes on them, but I don't think you can change the actual function behavior, can you?

@peytondmurray
Copy link
Contributor Author

@peytondmurray for my own understanding, could you help me understand how functions are mutable? I understand they're objects and you can assign attributes on them, but I don't think you can change the actual function behavior, can you?

No, you can't change function behavior, but functions are objects that have __setattr__, so in this case you'd have to be doing something pretty weird for it to affect anything, e.g.

def foo(x = lambda y: y):
    if hasattr(x, 'bar'):
       x.bar += 3
    else:
       x.bar = 0

    # do other stuff...

So the mutability is a little more hidden from the user than for example a list, since it's pretty unusual to be setting attributes on functions in the first place. For the record I'm not sure if any of these affect parts of the code do anything like this, but in any case it's better to just be safe since it fixes documentation rendering anyway.

@bveeramani bveeramani enabled auto-merge (squash) July 11, 2024 20:57
@github-actions github-actions bot added the go add ONLY when ready to merge, run all tests label Jul 11, 2024
@bveeramani bveeramani merged commit 2472e99 into ray-project:master Jul 11, 2024
6 checks passed
@peytondmurray peytondmurray deleted the 45129-fix-data-bad-type-annotation branch July 11, 2024 21:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
data Ray Data-related issues docs An issue or change related to documentation go add ONLY when ready to merge, run all tests P1 Issue that should be fixed within a few weeks
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants