Skip to content

Incorrect "Alternative patterns bind different names" with mapping capture statements #19995

@A5rocks

Description

@A5rocks

Can someone reopen this bug? There's a valid repro.
@cdce8p you're correct about str, but can you reopen this bug? There’s another repro of this "Alternative patterns bind different names" bug that seems to be new in 1.18 (before it was silently Any).

d1: dict[str, tuple[str, ...]] = {}
d2: dict[str, str] = {}

match (d1, d2):
    case ({"Symbol": (sym,)}, _) | (_, {"Symbol": sym}):
        reveal_type(sym)

playground

# main.py:5: error: Alternative patterns bind different names [misc]

I added a breakpoint to the source.
This looks like an issue in checkpattern.visit_or_pattern.
Debugging shows one alternative is given Never instead of a tuple type::

mypy/mypy/checkpattern.py(187)visit_or_pattern()
-> self.msg.fail(message_registry.OR_PATTERN_ALTERNATIVE_NAMES, o.patterns[i])
(Pdb) p pattern_types[0]
PatternType(type=tuple[builtins.dict[builtins.str, tuple[builtins.str]], builtins.dict[builtins.str, builtins.str]], rest_type=tuple[builtins.dict[builtins.str, tuple[builtins.str]], Never], captures={<mypy.nodes.NameExpr object at 0x7f50c2c2b9a0>: builtins.str})
(Pdb) p pattern_types[1]
PatternType(type=Never, rest_type=tuple[Never, Never], captures={})

So mypy concludes the alternatives bind differently, even though both capture sym.

Originally posted by @Hnasar in #13950

cc @cdce8p

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions