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

Warn about non-optional conditions that don't provide __nonzero__? #8284

Open
JukkaL opened this issue Jan 15, 2020 · 1 comment
Open

Warn about non-optional conditions that don't provide __nonzero__? #8284

JukkaL opened this issue Jan 15, 2020 · 1 comment

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Jan 15, 2020

Maybe mypy should complain about Iterable[...] as an if condition, for example, since it generally doesn't define __nonzero__, so the check is likely unsafe? More generally, we could do this for all classes that don't define __nonzero__, but this could generate too many errors. One option would be to make this an optional check enabled by --strict, at least initially.

Examples:

def f(x: Iterable[int]) -> None:
    if not x:  # Might never be taken, or at least this could be inconsistently taken
        print('x')

class A:
    pass  # No __nonzero__

def g(a: A) -> None:
    if not a:   # Similar to above? What if there are subclasses of 'A'?
        print('x')
@lazytype
Copy link
Contributor

If cheap to implement, would it make sense to test this internally in Dropbox's code-base and get a sense of what is the ratio of errors to false positives?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants