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

Raise a warning using-constant-test when casting a generator to a boolean as they are always truthey #6909

Closed
socketpair opened this issue Jun 10, 2022 · 3 comments · Fixed by #6911
Assignees
Labels
Enhancement ✨ Improvement to a component
Milestone

Comments

@socketpair
Copy link

socketpair commented Jun 10, 2022

Current problem

Suppose library code:

SOME_DATA = {1:2, 3:4}

def some_function() -> Collection[str]:
    return SOME_DATA.items()

and code that use the function:

if some_function():
    do_something()

At some point, authors decide to change signature in this way:

def some_function() -> Iterable[str]:
    return ((key, value) for key, value in SOME_DATA.items())

Obviously (actually not) the end-user code breaks.

Desired solution

I propose considering as error all cases where a generator implicitly interpreted as boolean. It will always return True even if a generator is exhausted, so this is useless. Error text should contain "to check if value is None use is not None".

@socketpair socketpair added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Jun 10, 2022
@socketpair socketpair changed the title Consider if some_generator as mistake Consider if some_generator as a mistake Jun 10, 2022
@jacobtylerwalls
Copy link
Member

This would be a nice addition to using-constant-test.

@jacobtylerwalls jacobtylerwalls added Enhancement ✨ Improvement to a component and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Jun 10, 2022
@jacobtylerwalls jacobtylerwalls self-assigned this Jun 10, 2022
@jacobtylerwalls jacobtylerwalls added this to the 2.15.0 milestone Jun 10, 2022
@Pierre-Sassoulas Pierre-Sassoulas changed the title Consider if some_generator as a mistake Raise a warning using-constant-test when casting a generator to a boolean as they are always truthey Jun 10, 2022
@socketpair
Copy link
Author

Actually, I don't understand, will it emit the warning if result of a function previously placed to a variable ?

@jacobtylerwalls
Copy link
Member

If not, then it's because the name can't be inferred by astroid as a nodes.GeneratorExp. Could you open an issue there? Another way of putting it: if n.test.inferred()[0] below was a nodes.GeneratorExp, this would already work (I think).

>>> code = """
... def get_generator():
...     return (x for x in range(0))
... x = get_generator()
... if x:  #@
...   pass
... """
>>> n = astroid.extract_node(code)
>>> n
<If l.5 at 0x102732470>
>>> n.test
<Name.x l.5 at 0x10279c0d0>
>>> n.test.inferred()
[Uninferable]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement ✨ Improvement to a component
Projects
None yet
2 participants