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

If can be used to avoid a TypeError, but try can't be #9736

Closed
Jayman2000 opened this issue Nov 19, 2020 · 1 comment
Closed

If can be used to avoid a TypeError, but try can't be #9736

Jayman2000 opened this issue Nov 19, 2020 · 1 comment
Labels
bug mypy got something wrong

Comments

@Jayman2000
Copy link

Bug Report

from typing import Optional


# mypy is OK with this function,
def allowed(foo: Optional[int]) -> None:
    if foo is not None:
        foo + 0


# but it's not OK with this one.
def disallowed(foo: Optional[int]) -> None:
    try:
        foo + 0
    except TypeError:
        pass

To Reproduce

  1. Create a file called bug_triggering_code.py.
  2. Copy the above example code.
  3. Paste the code into bug_triggering_code.py.
  4. Run mypy bug_triggering_code.py.

Expected Behavior

mypy reports no errors. Even if mypy does report errors, it should report that both functions have errors.

Actual Behavior

mypy reports an error for the disallowed() function but not for the allowed() function:
bug_triggering_code.py:13: error: Unsupported operand types for + ("None" and "int")

Your Environment

  • Mypy version used: 0.790
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): I have no config files.
  • Python version used: 3.8.6
  • Operating system and version: Arch Linux. I'm not sure how I would give a version for this.
@Jayman2000 Jayman2000 added the bug mypy got something wrong label Nov 19, 2020
@hauntsaninja
Copy link
Collaborator

Closing as a duplicate of #2420 and others

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