diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2023-08-31 22:04:55 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-31 22:04:55 (GMT) |
commit | 8c3793a539ce950e552da390b7b24485dd655237 (patch) | |
tree | f270c1cc50d17f694166ac1b6af27c2c3006091d /Doc/howto/enum.rst | |
parent | a92c60c99bab716d1d08bccb92dcc0fe6ee93abe (diff) | |
download | cpython-8c3793a539ce950e552da390b7b24485dd655237.zip cpython-8c3793a539ce950e552da390b7b24485dd655237.tar.gz cpython-8c3793a539ce950e552da390b7b24485dd655237.tar.bz2 |
[3.12] gh-108682: [Enum] raise TypeError if super().__new__ called in custom __new__ (GH-108704) (#108733)
gh-108682: [Enum] raise TypeError if super().__new__ called in custom __new__ (GH-108704)
When overriding the `__new__` method of an enum, the underlying data type should be created directly; i.e. .
member = object.__new__(cls)
member = int.__new__(cls, value)
member = str.__new__(cls, value)
Calling `super().__new__()` finds the lookup version of `Enum.__new__`, and will now raise an exception when detected.
(cherry picked from commit d48760b2f1e28dd3c1a35721939f400a8ab619b8)
Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
Diffstat (limited to 'Doc/howto/enum.rst')
-rw-r--r-- | Doc/howto/enum.rst | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/Doc/howto/enum.rst b/Doc/howto/enum.rst index 4312b4c..2874975 100644 --- a/Doc/howto/enum.rst +++ b/Doc/howto/enum.rst @@ -426,10 +426,17 @@ enumeration, with the exception of special methods (:meth:`__str__`, :meth:`__add__`, etc.), descriptors (methods are also descriptors), and variable names listed in :attr:`_ignore_`. -Note: if your enumeration defines :meth:`__new__` and/or :meth:`__init__` then +Note: if your enumeration defines :meth:`__new__` and/or :meth:`__init__`, any value(s) given to the enum member will be passed into those methods. See `Planet`_ for an example. +.. note:: + + The :meth:`__new__` method, if defined, is used during creation of the Enum + members; it is then replaced by Enum's :meth:`__new__` which is used after + class creation for lookup of existing members. See :ref:`new-vs-init` for + more details. + Restricted Enum subclassing --------------------------- @@ -895,6 +902,8 @@ Some rules: :meth:`__str__` method has been reset to their data types' :meth:`__str__` method. +.. _new-vs-init: + When to use :meth:`__new__` vs. :meth:`__init__` ------------------------------------------------ @@ -927,6 +936,11 @@ want one of them to be the value:: >>> print(Coordinate(3)) Coordinate.VY +.. warning:: + + *Do not* call ``super().__new__()``, as the lookup-only ``__new__`` is the one + that is found; instead, use the data type directly. + Finer Points ^^^^^^^^^^^^ @@ -1353,6 +1367,13 @@ to handle any extra arguments:: members; it is then replaced by Enum's :meth:`__new__` which is used after class creation for lookup of existing members. +.. warning:: + + *Do not* call ``super().__new__()``, as the lookup-only ``__new__`` is the one + that is found; instead, use the data type directly -- e.g.:: + + obj = int.__new__(cls, value) + OrderedEnum ^^^^^^^^^^^ |