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

boolean return inferred as Any return type #17007

Closed
Avasam opened this issue Mar 9, 2024 · 1 comment
Closed

boolean return inferred as Any return type #17007

Avasam opened this issue Mar 9, 2024 · 1 comment
Labels
bug mypy got something wrong

Comments

@Avasam
Copy link
Contributor

Avasam commented Mar 9, 2024

Bug Report

A method returning only boolean values is inferred as returning Any

To Reproduce

See the following minimal example:

def is_valid_hwnd(hwnd: int):
    if not hwnd:
        return False
    return True
    
reveal_type(is_valid_hwnd(0)) # note: Revealed type is "Any"

The real-world example is as such:

def is_valid_hwnd(hwnd: int):
    """Validate the hwnd points to a valid window and not the desktop or whatever window obtained with `""`."""
    if not hwnd:
        return False
    if sys.platform == "win32":
        return bool(win32gui.IsWindow(hwnd) and win32gui.GetWindowText(hwnd))
    return True

Which is much more readable than a boolean expression. But even that surprisingly is inferred as Any:

def is_valid_hwnd(hwnd: int):
    return bool(hwnd and (sys.platform != "win32" or (win32gui.IsWindow(hwnd) and win32gui.GetWindowText(hwnd))))

Expected Behavior

The return type should be inferred as bool. Even Literal[True, False] or Literal[True, False] | bool would be fine.

Actual Behavior

Return type is inferred as Any

Your Environment

  • Mypy version used: mypy 1.9.0 (compiled: yes)
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files):
; We don't run mypy in the CI. This is just to help anyone who would like to use it manually.
; Namely, the mypy_primer tool.
[mypy]
show_column_numbers = true
mypy_path = $MYPY_CONFIG_FILE_DIR/typings
implicit_reexport = true

strict = true
; Implicit return types !
check_untyped_defs = true
disallow_untyped_calls = false
disallow_untyped_defs = false
disallow_incomplete_defs = false
disable_error_code = return

; exclude mypyc build
exclude = .*(build)/.*

; Auto-generated code, not much we can do there
[mypy-gen.*]
disable_error_code = attr-defined, arg-type

; Of course my stubs are going to be incomplete. Otherwise they'd be on typeshed!
; Mypy becomes really whack with its errors inside these stubs though
[mypy-scipy.*]
disable_error_code = attr-defined, import-untyped
  • Python version used: 3.11.6
@Avasam Avasam added the bug mypy got something wrong label Mar 9, 2024
@JelleZijlstra
Copy link
Member

Duplicate of #10149

@JelleZijlstra JelleZijlstra marked this as a duplicate of #10149 Mar 9, 2024
@JelleZijlstra JelleZijlstra closed this as not planned Won't fix, can't repro, duplicate, stale Mar 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants