You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[1.19 regression] Can no longer use types.NoneType() as a condition in match/case #20367
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
importreimporttypesdeffun(val: str|re.Pattern|None):
matchval:
casestr():
print('string')
casere.Pattern():
print('pattern')
casetypes.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)
Activity
hauntsaninja commentedon Dec 6, 2025
Thanks, bisects to #20042
hauntsaninja commentedon Dec 6, 2025
Something like this would fix, but probably want something a little more general
A5rocks commentedon Dec 9, 2025
I think that is the best we can do, unfortunately.
type_infois a tempting variable to use, but doesn't work on type aliases.Probably I should look at the logic in
ExpressionCheckerto see what other cases might be returned as aTypeType, but for now I'll just make a PR.Allow `types.NoneType` in match cases (python#20383)
Allow `types.NoneType` in match cases (#20383)
Better match narrowing for type objects
Better match narrowing for type objects
Better match narrowing for type objects
Better match narrowing for type objects (#20872)