GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Confusing error message when attempting to pass a newtype as a match pattern · Issue #100530 · python/cpython

Confusing error message when attempting to pass a newtype as a match pattern #100530

New issue
Copy link
New issue
Copy link
Closed
Closed
Confusing error message when attempting to pass a newtype as a match pattern#100530
Copy link
Assignees
brandtbucher
Labels
topic-typingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@not-my-profile
not-my-profile
opened on Dec 26, 2022
Issue body actions
from typing import NewType

T = NewType('T', str)

match 'test':
    case T(): ...

fails with:

Traceback (most recent call last):
  File "/tmp/foo.py", line 6, in <module>
    case T(): ...
         ^^^
TypeError: called match pattern must be a type

The error message is confusing because T is a type. The error message should probably say class instead of type, since as per the specification it's a class pattern.

Linked PRs

  • gh-100530: Change the error message for MatchClass #103576

Activity

  1. sobolevn
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    topic-typing
    on Dec 26, 2022
  2. AlexWaygood commented on Dec 26, 2022

    @AlexWaygood
    AlexWaygood
    on Dec 26, 2022
    Member
    More actions

    Cc. @brandtbucher for pattern-matching

  3. brandtbucher commented on Dec 27, 2022

    @brandtbucher
    brandtbucher
    on Dec 27, 2022
    Member
    More actions

    Hm... I'm not really sure there's anything to do here. I think the main issue is Python's general overloading of the word "type" to refer to both static types (for annotations) and types of objects. It's this second group that we're checking for here:

    >>> import typing
    >>> T = typing.NewType("T", str)
    >>> isinstance(T, type)
    False

    With that said, I'm open to changing the wording of the error message from "type" to "class" if others agree that the current wording is confusing.

    Reacted by Martin Fischer
  4. brandtbucher
    self-assigned this
    on Dec 27, 2022
  5. sobolevn commented on Apr 16, 2023

    @sobolevn
    sobolevn
    on Apr 16, 2023
    Member
    More actions

    I agree that class is a much better term here than type. Because, here's one more example where type is not quite correct:

    >>> import typing
    >>> A = typing.Annotated[str, 'meta']
    >>> match 'abc':
    ...    case A(): ...
    ... 
    Traceback (most recent call last):
      File "<stdin>", line 2, in <module>
    TypeError: called match pattern must be a type

    typing.Annotated[str, 'meta'] is a type. But it is not a class.
    Moreover, the ast node is also called MatchClass which is another argument for the wording change.

    Reacted by Terry Jan Reedy and Peter Zaitcev / [USSX] Hares
  6. sobolevn
    added a commit that references this issue on Apr 16, 2023

    pythongh-100530: Change the error message for `MatchClass`

    26702d0
  7. brandtbucher
    added a commit that references this issue on Apr 18, 2023

    GH-100530: Change the error message for non-class class patterns (GH-…

    07804ce
  8. brandtbucher
    closed this as completedon Apr 18, 2023
  9. carljm
    added a commit that references this issue on Apr 20, 2023

    Merge branch 'main' into superopt

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

Metadata

Metadata

Assignees

  • @brandtbucher
    brandtbucher

Labels

topic-typingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.