Skip to content

Inconsistent inference of sum return type when coupled with nullability. #7287

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
Sean1708 opened this issue Aug 5, 2019 · 4 comments
Closed
Labels

Comments

@Sean1708
Copy link

Sean1708 commented Aug 5, 2019

Please provide more information to help us understand the issue:

  • Are you reporting a bug, or opening a feature request? Bug

  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.

    from typing import Optional
    
    
    def foo() -> int:
        return sum([])
    
    
    reveal_type(sum([]))
    
    
    def test1(a: Optional[int]) -> None:
        b = a
        if b is None:
            b = sum([])
    
        reveal_type(b)
    
    def test2(a: Optional[int]) -> None:
        b = a
        if b is None:
            b = foo()
    
        reveal_type(b)
    
    def test3() -> None:
        b = None
        if b is None:
            b = sum([])
    
        reveal_type(b)
    
    def test4(a: Optional[int]) -> None:
        if a is None:
            b = sum([])
        else:
            b = a
    
        reveal_type(b)
  • What is the actual behavior/output?

    $ mypy test.py
    test.py:8: note: Revealed type is 'builtins.int'
    test.py:16: note: Revealed type is 'Union[builtins.int, None]'
    test.py:23: note: Revealed type is 'builtins.int'
    test.py:30: note: Revealed type is 'builtins.int'
    test.py:38: note: Revealed type is 'builtins.int'
    
  • What is the behavior/output you expect? I expect b to be inferred as an int in all cases, i.e.:

    $ mypy test.py
    test.py:8: note: Revealed type is 'builtins.int'
    test.py:16: note: Revealed type is 'builtins.int'
    test.py:23: note: Revealed type is 'builtins.int'
    test.py:30: note: Revealed type is 'builtins.int'
    test.py:38: note: Revealed type is 'builtins.int'
    
  • What are the versions of mypy and Python you are using?

    $ python3.7 --version
    Python 3.7.2
    $ mypy --version
    mypy 0.720
    

    Do you see the same issue after installing mypy from Git master? Yes (mypy 0.730+dev.eb34a7bdcceb4507459c0ef5472117f65bd0855d)

This might be a duplicate of #6697, but the details differ enough that I thought I should open a ticket just in case.

@ilevkivskyi
Copy link
Member

This is quite subtle. It looks like in the problematic case the "declared" type of b is used as an external context to infer the type variable of sum(). I don't have ideas for how to fix this and there are several alternative ways to write this that work. I am therefore setting priority to low.

@ilevkivskyi
Copy link
Member

(Note that if in the actual code the list was non-empty, then this is a known issue of mypy being too eager about external context.)

@Sean1708
Copy link
Author

Thanks for having a look. I can't think of any situations that don't have workarounds, so I think low priority is sensible.

@hynekcer
Copy link

This was fixed even before mypy 0.950. Can be closed

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

No branches or pull requests

4 participants