[Python-ideas] Change magic strings to enums

Nick Coghlan ncoghlan at gmail.com
Tue Apr 24 11:49:12 EDT 2018


On 25 April 2018 at 01:06, Jacco van Dorp <j.van.dorp at deonet.nl> wrote:
> I guess we could add inconsistency as a con, then, since if the import
> system isn't working at places where you'd like to use the Enums (or
> even executing python code ?). This would mean that to the casual
> observer, it'd be arbitrary where they could be used instead.

Running './python -X importtime -Wd -c "pass"' with Python 3.7 gives a
pretty decent list of the parts of the standard library that
constitute the low level core that we try to keep independent of
everything else (there's a slightly smaller core that omits the
warning module and it's dependencies - leaving "-Wd" off the command
line will give that list).

> I wonder how many of these would be in places used by most people,
> though.

> I don't mind putting in some time to figure it out, but I have
> no idea where to start. Is there any easily searchable place where I
> could scan the standard library for occurences of magic strings ?

Searching the documentation for :data: fields, and then checking those
to see which ones had already been converted to enums would likely be
your best bet.

You wouldn't be able to get a blanket approval for "Let's convert all
the magic strings to Enums" though - you'd need to make the case that
each addition of a new Enum provided a genuine API improvement for the
affected module (e.g. I suspect a plausible case could potentially be
made for converting some of the inspect module state introspection
APIs over to StringEnum, so it was easier to iterate over the valid
states in a consistent way, but even there I'd need to see a concrete
proposal before I made up my mind).

Making the case for IntEnum usage tends to be much easier, simply due
to the runime introspection benefits that it brings.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list