Skip to content

Mark classes as not instantiable #2066

@srittau

Description

@srittau

From python/typeshed#14558: Some classes (like re.Pattern) are not instantiable at runtime:

>>> import re
>>> re.Pattern()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot create 're.Pattern' instances

Currently, there is no working way to mark classes as such. Ideas to mark classes:

  • Annotate the return type of __new__ as NoReturn/Never.
  • Set __new__ to None (similar to how __hash__ works).
  • A new decorator.

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: featureDiscussions about new features for Python's type annotations

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions