Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Mypy doesn't recognize None is checked when using or operator #13725

Closed
GlenNicholls opened this issue Sep 24, 2022 · 2 comments
Closed

Mypy doesn't recognize None is checked when using or operator #13725

GlenNicholls opened this issue Sep 24, 2022 · 2 comments
Labels
bug mypy got something wrong

Comments

@GlenNicholls
Copy link

Bug Report

When I use or to handle None, mypy doesn't recognize that the code handles the None case and issues errors about accessing attributes:

from typing import Optional

def func(arg: Optional[list] = None) -> list:
  arg = arg or []
  return arg.append(1)

I get a union-attr error for append. To fix this, I can do the following:

if arg is None:
  arg = []

However, I think this is wrong because the code is equivilent. Am I'm missing something here?

Expected Behavior

I would expect that when using arg or [], mypy would recognize that arg is never None after that line (assuming I don't override).

Actual Behavior

error: Item "None" of "Optional[list]" has no attribute "append" [union-attr]

Your Environment

  • Mypy version used: 0.971
  • Mypy configuration options from pyproject.toml:
    [tool.mypy]
    warn_unused_configs = true
    warn_unused_ignores = true
    install_types = true
    non_interactive = true
    check_untyped_defs = true
    show_error_codes = true
    show_error_context = true
    pretty = true
    color_output = true
  • Python version used: 3.9
  • Operating system and version: Ubuntu 20.04
@GlenNicholls GlenNicholls added the bug mypy got something wrong label Sep 24, 2022
@hauntsaninja
Copy link
Collaborator

I can't repro, the only error I get is main.py:5: error: "append" of "list" does not return a value [func-returns-value] which is correct (you need to do return arg).

@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Sep 24, 2022
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants