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

False positive following case None in match statement (missed narrowing?) #13046

Closed
mthuurne opened this issue Jun 30, 2022 · 3 comments
Closed
Labels
bug mypy got something wrong topic-match-statement Python 3.10's match statement topic-type-narrowing Conditional type narrowing / binder

Comments

@mthuurne
Copy link
Contributor

Bug Report

In match block with a case None:, mypy considers values captured by subsequent cases to still have None as a possible type.

To Reproduce

Check the following code using mypy:

def f(obj: object) -> str | None:
    return str(obj) if obj else None

def g(obj: object) -> str:
    match f(obj):
        case None:
            return "-"
        case text:
            return text

Expected Behavior

No errors would be reported: a case that matches the literal None can be treated as matching NoneType.

Actual Behavior

match_nonetype.py:9: error: Incompatible return value type (got "Optional[str]", expected "str")  [return-value]
                return text
                       ^

Your Environment

  • Python version used: 3.10.4
  • Operating system and version: openSUSE Linux Tumbleweed
  • Mypy version used: 0.961 (compiled: yes)
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files):
disallow_any_generics=True
disallow_incomplete_defs=True
disallow_untyped_defs=True
namespace_packages=True
no_implicit_optional=True
no_implicit_reexport=True
pretty=True
show_error_codes=True
strict_equality=True
warn_no_return=True
warn_redundant_casts=True
warn_return_any=True
warn_unreachable=True
warn_unused_configs=True
warn_unused_ignores=True
@mthuurne mthuurne added the bug mypy got something wrong label Jun 30, 2022
@mthuurne
Copy link
Contributor Author

The problem can be worked around by assigning the result of the function call to a local variable:

def f(obj: object) -> str | None:
    return str(obj) if obj else None

def g(obj: object) -> str:
    x = f(obj)
    match x:
        case None:
            return "-"
        case text:
            return text

So this might be a variation of #12998.

@JelleZijlstra JelleZijlstra added the topic-match-statement Python 3.10's match statement label Jun 30, 2022
@AlexWaygood AlexWaygood added the topic-type-narrowing Conditional type narrowing / binder label Jul 2, 2022
@dset0x
Copy link

dset0x commented Nov 23, 2022

For what it's worth, it appears that the following is also currently accepted by mypy:

    match _ := f(obj):
        case None:
            return "-"
        case text:
            return text

@erictraut
Copy link

This is a duplicate of #12998.

@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale Aug 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-match-statement Python 3.10's match statement topic-type-narrowing Conditional type narrowing / binder
Projects
None yet
Development

No branches or pull requests

5 participants