Skip to content

and expression with bool() does not work with Optionals #5540

Open
@sk-

Description

@sk-

For the following code, mypy reports an incompatible type error on the function f

from typing import Optional

def f(x:Optional[str]) -> bool:
  return bool(x) and g(x)

def f2(x:Optional[str]) -> bool:
  if x:
    return g(x)
  return False

def g(x: str) -> bool:
  return bool(x)
test.py:4: error: Argument 1 to "g" has incompatible type "Optional[str]"; expected "str"

however the code is equivalent to the one in f2, for which no error is raised.

  • What are the versions of mypy and Python you are using?
    Mypy: 0.620, Python: 3.6.5

  • What are the mypy flags you are using? (For example --strict-optional)

[mypy]
# This flag should be removed here and enabled per module when we have a
# considerable number of stubs for external libraries.
ignore_missing_imports = True
strict_optional = True
warn_unused_ignores = True
warn_redundant_casts = True
warn_unused_configs = True
check_untyped_defs = True

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions