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

mypy 1.8.0 regression: Incorrectly flags unreachable with re.match() result #16770

Closed
amykyta3 opened this issue Jan 12, 2024 · 1 comment
Closed
Labels
bug mypy got something wrong

Comments

@amykyta3
Copy link

Bug Report

It seems that v1.8.0 regressed in how it handles type checking with returned match objects from Python's regex stdlib.
It now flags unreachable code when checking on the truthyness of a regex match return value.

This was not the case in v1.7.x

To Reproduce

import re
import sys

if re.match(r"\d*", sys.argv[1]):
    print("a")
else:
    print("b") # mypy 1.8.0 incorrectly flags as unreachable

Results in:

tst.py:7: error: Statement is unreachable [unreachable]
Found 1 error in 1 file (checked 1 source file)

Changing the line to explicitly cast it as a boolean avoids this:

if bool(re.match(r"\d*", sys.argv[1])):

or:

if re.match(r"\d*", sys.argv[1]) is not None:

This seems to be related to the config setting of strict_optional = False

Expected Behavior

I would not expect any unreachable lines in this example.

Actual Behavior

mypy 1.8.0 reports an unreachable violation.

Your Environment

  • Mypy version used: 1.8.0
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files):
[mypy]
warn_unreachable = True
strict_optional = False
  • Python version used: Python 3.10.12
@amykyta3 amykyta3 added the bug mypy got something wrong label Jan 12, 2024
@hauntsaninja
Copy link
Collaborator

Duplicate of #16718

@hauntsaninja hauntsaninja marked this as a duplicate of #16718 Jan 12, 2024
@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Jan 12, 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