Skip to content

Union is incorrectly constructed when callable() is used, resulting in a false positive #9778

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
sirosen opened this issue Dec 4, 2020 · 0 comments · Fixed by #19183
Closed
Labels
bug mypy got something wrong topic-type-narrowing Conditional type narrowing / binder

Comments

@sirosen
Copy link

sirosen commented Dec 4, 2020

Bug Report

The following code fails type checking:

import typing

T = typing.TypeVar("T")

def callable_or_raise(foo: T) -> T:
    if foo and not callable(foo):
        raise ValueError("bad!")
    return foo

mypy reports foo.py:9: error: Incompatible return value type (got "Union[T, object]", expected "T")

Your Environment

  • Mypy version used: 0.790 and confirmed on master
  • Python version used: 3.6, 3.9

The oddest thing is that this will pass if I replace callable with a wrapper, e.g.

def check(x: object) -> bool:
    return callable(x)

reveal_type(check) is almost identical to reveal_type(callable):

foo.py:9: note: Revealed type is 'def (x: builtins.object) -> builtins.bool'
foo.py:10: note: Revealed type is 'def (builtins.object) -> builtins.bool'

This might be related to other false positives around Union + TypeVar (e.g. #6898) but I am unsure.

@sirosen sirosen added the bug mypy got something wrong label Dec 4, 2020
@AlexWaygood AlexWaygood added the topic-type-narrowing Conditional type narrowing / binder label Apr 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-type-narrowing Conditional type narrowing / binder
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants