Skip to content

NewType in pattern should be rejected #10784

Closed

Description

from typing import NewType

A = NewType("A", int)
B = NewType("B", str)

def test(x: A | B):
    match x:
        case A():
            print("A")
        case B():
            print("B")

test(A(123))

"""
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    test(A(123))
    ~~~~^^^^^^^^
  File "<stdin>", line 3, in test
    case A():
         ~^^
TypeError: called match pattern must be a class
"""

This code causes an exception at runtime, but pyright doesn't produce any errors.

Pyright Playground

Related: #6828

Activity

  1. added
    bug
    on Aug 12, 2025
  2. erictraut commented on Aug 12, 2025

    @erictraut
    Collaborator

    This isn't really a type error, but pyright does provide similar checks, so I think this is a reasonable enhancement request.

  3. added
    enhancement request
    and removed
    bug
    on Aug 12, 2025
  4. added
    addressed in next version
    on Sep 28, 2025
  5. erictraut commented on Oct 1, 2025

    @erictraut
    Collaborator

    This is addressed in 1.1.406

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    addressed in next versionenhancement request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions