Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
not-my-profile opened this issue Dec 26, 2022 · 3 comments
Assignees
Labels
topic-typing type-bug An unexpected behavior, bug, or error

Comments

@not-my-profile
Copy link
Contributor

not-my-profile commented Dec 26, 2022

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

@sobolevn sobolevn added type-bug An unexpected behavior, bug, or error topic-typing labels Dec 26, 2022
@AlexWaygood
Copy link
Member

Cc. @brandtbucher for pattern-matching

@brandtbucher
Copy link
Member

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.

@brandtbucher brandtbucher self-assigned this Dec 27, 2022
@sobolevn
Copy link
Member

sobolevn commented Apr 16, 2023

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.

sobolevn added a commit to sobolevn/cpython that referenced this issue Apr 16, 2023
carljm added a commit to carljm/cpython that referenced this issue Apr 20, 2023
* main: (24 commits)
  pythongh-98040: Move the Single-Phase Init Tests Out of test_imp (pythongh-102561)
  pythongh-83861: Fix datetime.astimezone() method (pythonGH-101545)
  pythongh-102856: Clean some of the PEP 701 tokenizer implementation (python#103634)
  pythongh-102856: Skip test_mismatched_parens in WASI builds (python#103633)
  pythongh-102856: Initial implementation of PEP 701 (python#102855)
  pythongh-103583: Add ref. dependency between multibytecodec modules (python#103589)
  pythongh-83004: Harden msvcrt further (python#103420)
  pythonGH-88342: clarify that `asyncio.as_completed` accepts generators yielding tasks (python#103626)
  pythongh-102778: IDLE - make sys.last_exc available in Shell after traceback (python#103314)
  pythongh-103582: Remove last references to `argparse.REMAINDER` from docs (python#103586)
  pythongh-103583: Always pass multibyte codec structs as const (python#103588)
  pythongh-103617: Fix compiler warning in _iomodule.c (python#103618)
  pythongh-103596: [Enum] do not shadow mixed-in methods/attributes (pythonGH-103600)
  pythonGH-100530: Change the error message for non-class class patterns (pythonGH-103576)
  pythongh-95299: Remove lingering setuptools reference in installer scripts (pythonGH-103613)
  [Doc] Fix a typo in optparse.rst (python#103504)
  pythongh-101100: Fix broken reference `__format__` in `string.rst` (python#103531)
  pythongh-95299: Stop installing setuptools as a part of ensurepip and venv (python#101039)
  pythonGH-103484: Docs: add linkcheck allowed redirects entries for most cases (python#103569)
  pythongh-67230: update whatsnew note for csv changes (python#103598)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-typing type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

4 participants