classification
Title: Argument Clinic: add the boolint converter
Type: enhancement Stage: resolved
Components: Argument Clinic Versions: Python 3.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: larry, serhiy.storchaka, taleinat
Priority: normal Keywords: patch

Created on 2015-04-23 10:33 by serhiy.storchaka, last changed 2017-03-24 22:25 by serhiy.storchaka. This issue is now closed.

Files
File name Uploaded Description Edit
clinic_boolint_converter.patch serhiy.storchaka, 2015-04-23 10:33 review
clinic_boolint_converter_2.patch serhiy.storchaka, 2015-05-12 10:52 review
clinic-boolint-converter-3.patch serhiy.storchaka, 2017-03-04 20:28
Pull Requests
URL Status Linked Edit
PR 485 merged serhiy.storchaka, 2017-03-05 21:43
Messages (9)
msg241860 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-04-23 10:33
The 'i' format unit is often used for semantically boolean values. When the parameter has a default value, in Argument clinic you should specify it twice, as Python and C values:

    closefd: int(c_default="1") = True

or

    keepends: int(py_default="False") = 0

Proposed patch adds the boolint converter which makes a conversion for you.

    closefd: boolint = True
    keepends: boolint = False
msg241882 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2015-04-23 19:40
I think this is silly.

Python has a well-understood concept of "truth":
    https://docs.python.org/3/library/stdtypes.html#truth-value-testing

I assert that the reason people used the "i" format unit for what are really boolean values is because a) the "p" format unit wasn't added until very recently, and b) they were lazy and it was easy to copy-and-paste from other code.

Rather than perpetuate these misguided hacks, when converting code to Argument Clinic we should convert them to properly support truth as defined in Python.
msg241883 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-04-23 19:56
I consider this as transitional style, for simpler converting to Argument Clinic without changing the behaviour. In future it can be replaced with bool.

But the bool converter has a downside. It is harder to extend it. We can't add a support of say a tuple, or callable, or string, or ternary logic value without potential breaking some code. I propose first convert all semantic boolean ints to the boolint converter, and then, after fundamental analysis, replace boolint with bool if there is no any sense to extend concrete parameter.
msg242008 - (view) Author: Tal Einat (taleinat) * (Python committer) Date: 2015-04-25 08:31
If this was for internal use only, intended to ease the transition to Argument Clinic, then extensibility isn't an issue.

An upside to this is that it would make it easy in the future to find all of the places in the stdlib using integers instead of bools.

A downside is that "boolint" isn't a very obvious name IMO.
msg242010 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-04-25 09:30
I proposed boolint or intbool. Are there better variants?

An alternative is add accept={int} parameter to the bool converter. But this will complicate the implementation and the use without the need.
msg242140 - (view) Author: Tal Einat (taleinat) * (Python committer) Date: 2015-04-27 17:53
If I was writing a function/method with a conceptually boolean parameter (True/False), I wouldn't want that to accept any Python object. For example, I would want passing a tuple or list to raise a TypeError. But according to the docs[1], that's what the 'p' converter does.

If Python had a separate boolean type, this would be simple, but bool is a subclass of int, and it is easy to get a 0 or a 1 instead of True or False without noticing. So it seems reasonable when using the C API to accept an int when you want to get a boolean. I'd want a converter to convert it to a C bool, of course.
msg242957 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-05-12 10:52
Updated to the tip. Used new converter in recently converted _tkinter and _codecs modules. Now it is used 30 times.
msg288993 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-03-04 20:28
Updated patch uses bool(accept={int}) rather of boolint. It also updates more functions converted to Argument Clinic.
msg290218 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-03-24 22:25
New changeset 202fda55c2dffe27125703225e5af92254602dc6 by Serhiy Storchaka in branch 'master':
bpo-24037: Add Argument Clinic converter `bool(accept={int})`. (#485)
https://github.com/python/cpython/commit/202fda55c2dffe27125703225e5af92254602dc6
History
Date User Action Args
2017-03-24 22:25:30serhiy.storchakasetmessages: + msg290218
2017-03-17 21:18:08serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2017-03-17 21:06:24larrysetpull_requests: - pull_request601
2017-03-17 21:00:34larrysetpull_requests: + pull_request601
2017-03-05 21:43:13serhiy.storchakasetpull_requests: + pull_request398
2017-03-04 20:28:22serhiy.storchakasetfiles: + clinic-boolint-converter-3.patch

messages: + msg288993
versions: + Python 3.7, - Python 3.5
2015-05-12 10:52:01serhiy.storchakasetfiles: + clinic_boolint_converter_2.patch

messages: + msg242957
2015-04-27 17:53:07taleinatsetmessages: + msg242140
2015-04-25 09:30:54serhiy.storchakasetmessages: + msg242010
2015-04-25 08:31:39taleinatsetnosy: + taleinat
messages: + msg242008
2015-04-23 19:56:46serhiy.storchakasetmessages: + msg241883
2015-04-23 19:40:38larrysetmessages: + msg241882
2015-04-23 10:33:08serhiy.storchakacreate