Skip to content

Type narrowing using "in tuple" #12123

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

Closed
yabirgb opened this issue Feb 3, 2022 · 3 comments
Closed

Type narrowing using "in tuple" #12123

yabirgb opened this issue Feb 3, 2022 · 3 comments
Labels
feature priority-2-low topic-type-narrowing Conditional type narrowing / binder

Comments

@yabirgb
Copy link

yabirgb commented Feb 3, 2022

Mypy fails to recognize after a explicit check that a set of variables can't be None

To Reproduce

Run mypy on the following script

from typing import Optional

class Test:    
    def double(self, x: int) -> int:
        return x * 2

def check_value(a: Optional[Test], b: Optional[int]) -> Optional[int]:
    if None in (a, b):
        return None
    
    return a.double(b)
    
if __name__ == '__main__':
    check_value(Test(), 2)

Expected Behavior

The linter should not complain as after the if check both variables a and b can't be None.

Actual Behavior

The linter says that the None possibility for the variable lacks the method. Is not correct since we have
taken action to prevent this.

testmypy.py:11: error: Item "None" of "Optional[Test]" has no attribute "double"
testmypy.py:11: error: Argument 1 to "double" of "Test" has incompatible type "Optional[int]"; expected "int"

Your Environment

  • Mypy version used: 0.931
  • Mypy command-line flags: mypy testmypy.py
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.9.10 and 3.7
  • Operating system and version: EndeavourOS Linux x86_64 - 5.16.4-arch1-1
@yabirgb yabirgb added the bug mypy got something wrong label Feb 3, 2022
@erictraut
Copy link

The pattern None in (a, b) is not current supported for type narrowing by mypy. If you use the pattern a is None or b is None, it will work as expected.

@hauntsaninja hauntsaninja added feature priority-2-low and removed bug mypy got something wrong labels Feb 4, 2022
@JelleZijlstra JelleZijlstra added the topic-type-narrowing Conditional type narrowing / binder label Mar 19, 2022
@nunesvictor
Copy link

nunesvictor commented Nov 4, 2022

I have a similar problem. See, if I try to check if several elements are not None with all function, mypy ins't capable to infer the variable isn't Optional anymore. Example:

my_model: Optional[MyModel] = optionalDict.get('my_model')
my_second_model: Optional[MySecondModel] = optionalDict.get('my_second_model')

if not all([my_model, my_second_model]):
    raise BaseException('Models cannot be None')

my_model.attr # <--- still infers like Optional[MyModel]

@JelleZijlstra JelleZijlstra changed the title Mypy fails to recognize after a explicit check that a set of variables can't be None Type narrowing using "in tuple" Nov 5, 2022
@JelleZijlstra
Copy link
Member

Duplicate of #3229

@JelleZijlstra JelleZijlstra marked this as a duplicate of #3229 Nov 5, 2022
@JelleZijlstra JelleZijlstra closed this as not planned Won't fix, can't repro, duplicate, stale Nov 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature priority-2-low topic-type-narrowing Conditional type narrowing / binder
Projects
None yet
Development

No branches or pull requests

5 participants