classification
Title: `AutoNumber` class in enum documentation: support *args in constructor
Type: Stage: resolved
Components: Documentation Versions: Python 3.10
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: ethan.furman Nosy List: docs@python, ethan.furman, miss-islington, rrt
Priority: normal Keywords: patch

Created on 2019-05-27 06:03 by rrt, last changed 2020-09-23 03:59 by ethan.furman. This issue is now closed.

Files
File name Uploaded Description Edit
foo.py rrt, 2019-05-27 06:03 Demo code
Pull Requests
URL Status Linked Edit
PR 22349 merged ethan.furman, 2020-09-22 03:54
PR 22369 merged miss-islington, 2020-09-23 03:56
PR 22370 merged miss-islington, 2020-09-23 03:56
Messages (7)
msg343607 - (view) Author: Reuben Thomas (rrt) Date: 2019-05-27 06:03
By changing one line of AutoNumber:

    def __new__(cls):

to

    def __new__(cls, *args):

Enums derived from AutoNumber can now support constructors that take named arguments; for example:

class Color(AutoNumber):
    def __init__(self, pantone=None):
        self.pantone = pantone or 'unknown'

class Swatch(Color):
    AUBURN = ('3497')
    SEA_GREEN = ('1246')
    BLEACHED_CORAL = () # New color, no Pantone code yet!

Without the change, one gets the error:

TypeError: __new__() takes 1 positional argument but 2 were given

I attach runnable demonstration code.
msg343608 - (view) Author: Reuben Thomas (rrt) Date: 2019-05-27 06:03
Just to be clear, the proposed change to the documentation is simply to add ", *args" to the argument list of AutoNumber.__new__.
msg377291 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2020-09-21 23:21
The only `AutoNumber` in the stdlib is as an example in the docs.  Were you thinking of something else?
msg377292 - (view) Author: Reuben Thomas (rrt) Date: 2020-09-21 23:25
That's the one I was thinking of: the example in the docs.
msg377355 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2020-09-23 03:57
New changeset 5acc1b5f0b62eef3258e4bc31eba3b9c659108c9 by Miss Islington (bot) in branch '3.8':
bpo-37062: Enum: add extended AutoNumber example (GH-22349) (GH-22369)
https://github.com/python/cpython/commit/5acc1b5f0b62eef3258e4bc31eba3b9c659108c9
msg377356 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2020-09-23 03:58
New changeset 64362c2e435eddc5e84cea313d92bc0b96d227f7 by Miss Islington (bot) in branch '3.9':
bpo-37062: Enum: add extended AutoNumber example (GH-22349) (GH-22370)
https://github.com/python/cpython/commit/64362c2e435eddc5e84cea313d92bc0b96d227f7
msg377357 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2020-09-23 03:59
Thank you, Reuben!
History
Date User Action Args
2020-09-23 03:59:37ethan.furmansetstatus: open -> closed
resolution: fixed
messages: + msg377357

stage: patch review -> resolved
2020-09-23 03:58:39ethan.furmansetmessages: + msg377356
2020-09-23 03:57:56ethan.furmansetmessages: + msg377355
2020-09-23 03:56:47miss-islingtonsetpull_requests: + pull_request21410
2020-09-23 03:56:30miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request21409
2020-09-22 03:54:01ethan.furmansetkeywords: + patch
stage: patch review
pull_requests: + pull_request21388
2020-09-21 23:25:21rrtsetmessages: + msg377292
2020-09-21 23:21:03ethan.furmansetmessages: + msg377291
versions: + Python 3.10, - Python 3.9
2020-03-25 21:59:43ethan.furmansetversions: + Python 3.9, - Python 3.8
2020-03-25 21:59:24ethan.furmansetassignee: docs@python -> ethan.furman
2019-05-27 06:04:29xtreaksetnosy: + ethan.furman
2019-05-27 06:03:35rrtsetmessages: + msg343608
2019-05-27 06:03:00rrtcreate