Skip to content

[1.19 regression] Can no longer use types.NoneType() as a condition in match/case #20367

@chadhpaine

Description

@chadhpaine

Bug Report

Prior to mypy 1.19, it was possible to use types.NoneType() as a pattern-matcher in match/case syntax for values whose 'revealed type' includes 'the None type'.

To Reproduce

import re
import types

def fun(val: str | re.Pattern | None):
    match val:
        case str():
            print('string')
        case re.Pattern():
            print('pattern')
        case types.NoneType():  # main.py:10: error: Expected type in class pattern; found "type[None]"  [misc]
            print('None')

fun('123')
fun(re.compile('456'))
fun(None)

Expected Behavior

No error or warning is raised.

Actual Behavior

main.py:10: error: Expected type in class pattern; found "type[None]"  [misc]
Found 1 error in 1 file (checked 1 source file)

Your Environment

Reproduces on mypy-play.net.
https://mypy-play.net/?mypy=latest&python=3.12&gist=3dc5c105d72ba997443f1c4f58a179a0

  • Mypy version used: 1.19.0
  • Mypy command-line flags: n/a
  • Mypy configuration options from mypy.ini (and other config files): n/a
  • Python version used: 3.12

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions