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

Allow to ignore B008 (function calls in default arguments) in cases where the function is a method of a local variable #15062

Open
beneyal opened this issue Dec 19, 2024 · 3 comments
Labels
type-inference Requires more advanced type inference.

Comments

@beneyal
Copy link

beneyal commented Dec 19, 2024

Hey all,

First, I want to thank the maintainers of this tool for doing a spectacular job, Ruff is a joy to use! 🚀

I'll begin the issue with a small reproduction:

class A:
    pass


class B:
    def f(self) -> A:
        return A()


def foo(b: B) -> None:
    def bar(x: A = b.f()) -> None:  # Do not perform function call `b.f` in argument defaults; instead, perform the call within the function, or read the default from a module-level singleton variable [B008]
        print(x)

    bar(A())

When using flake8-bugbear's extend-immutable-calls section, I could ignore b.f. In Ruff, I can't, because I don't have a fully qualified path to it. I tried adding B.f to extend-immutable-calls, but it didn't work.

Is there a way to ignore such a thing using extend-immutable-calls? Thanks! 🙏

@MichaReiser
Copy link
Member

Thanks for the kind words.

Hmm, no, I don't think there's a way to do it today. Ruff's code that is used to resolve a name (e.g. B.b) doesn't understand following arguments but it would be required to understand that b is typed as B.

Relevant code

pub fn resolve_qualified_name<'name, 'expr: 'name>(

@MichaReiser MichaReiser added the type-inference Requires more advanced type inference. label Dec 20, 2024
@beneyal
Copy link
Author

beneyal commented Dec 21, 2024

Oh, okay, thanks! Should I keep the issue open as a "feature request", or just close it?

@MichaReiser
Copy link
Member

I'd leave it open for now. It might be possible to add (limited) support for it with Ruff's current model.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-inference Requires more advanced type inference.
Projects
None yet
Development

No branches or pull requests

2 participants