[issue37564] ArgumentParser should support bool type according to truth values

2019-11-13 Thread Raymond Hettinger


Change by Raymond Hettinger :


--
resolution:  -> rejected
stage: patch review -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue37564] ArgumentParser should support bool type according to truth values

2019-11-10 Thread Raymond Hettinger


Change by Raymond Hettinger :


--
Removed message: https://bugs.python.org/msg356334

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue37564] ArgumentParser should support bool type according to truth values

2019-11-10 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

Right now "type" has a clear and understandable action of invoking a callable.  
 Imbuing "type" with other semi-magical capabilities opens a new can of worms.

Unless some persuasive new commentary appears in the next few days, I'm going 
to close this feature request and the associated PR.

At some point, we should consider adding a recipes/faq section to the docs.  
That would help people bridge from their goals to the actual capabilities 
offered by the module.

Also, we should make strong suggestions about the separation of 
responsibilities between the parser and the downstream code that acts on the 
parsed variables (much like the discussion in the templating world about 
keeping business logic outside of the template and instead focusing on 
formatting logic).

For the case at hand, there are several ways to go:

  # Proposed new magic with hard-wired
  # truthy/false words: yes True 1 true Y si oui ...
  parser.add_argument("--mybool", type='bool')

  # Existing capability where the user controls
  # exactly which logic to apply
  parser.add_argument("--mybool", type=str_to_bool)

  # Existing capability with downstream processing
  parser.add_argument("--mybool", choices={'True', 'False', 'Yes', 'No'}
  args = parser.parse_args()
  mybool = args.mybool in {'True', 'Yes'}

  # Existing capability with explicit flag arguments
  # (the highly upvoted "canonical" solution StackOverflow)
  parser.add_argument('--feature', dest='feature', action='store_true')
  parser.add_argument('--no-feature', dest='feature', action='store_false')
  parser.set_defaults(feature=True)

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue37564] ArgumentParser should support bool type according to truth values

2019-11-10 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

Right now "type" has a clear and understandable action of invoking a callable.  
 Imbuing "type" with other semi-magical capabilities opens a new can of worms.

Unless some persuasive new commentary appears in the next few days, I'm going 
to close this feature request and the associated PR.

At some point, we should consider adding a recipes/faq section to the docs.  
That would help people bridge from their goals to the actual capabilities 
offered by the module.

Also, we should make strong suggestions about the separation of 
responsibilities between the parser and the downstream code that acts on the 
parsed variables (much like the discussion in the templating world about 
keeping business logic outside of the template and instead focusing on 
formatting logic).

For the case at hand, there are several ways to go:

  # proposed new magic with hard-wired
  # truthy/false words: yes True 1 true Y si oui ...
  parser.add_argument("--mybool", type='bool')

  # existing capability where the user controls
  # exactly which logic to apply
  parser.add_argument("--mybool", type=str_to_bool)

  # Existing capability with downstream processing
  parser.add_argument("--mybool", choices={'True', 'False', 'Yes', 'No'}
  args = parser.parse_args()
  mybool = args.mybool in {'True', 'Yes'}

--
assignee:  -> rhettinger
nosy: +rhettinger
type: behavior -> enhancement
versions:  -Python 2.7, Python 3.5, Python 3.6, Python 3.7, Python 3.8

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue37564] ArgumentParser should support bool type according to truth values

2019-11-10 Thread Daniel Kahn Gillmor


Daniel Kahn Gillmor  added the comment:

this is a common enough question, and enough people want this behavior, that 
argparse should supply it.

I'm imagining that:  

   type='bool'

would be fine for triggering this behavior, instead of the shadowing that could 
happen with:

   type=bool

so if type='bool' is supplied, the following things would happen:

 * argparse could use strtobool to interpret the incoming string
 * the default metavar would look something like "{true,false}"
 * followon packages like argcomplete could enumerate the range of different 
values that strtobool accepts

This is still english-specific -- but it's because strtobool is 
english-specific, and fixes in strtobool would automatically fix type='bool' 
here.

--
nosy: +dkg

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue37564] ArgumentParser should support bool type according to truth values

2019-09-13 Thread Stéphane Wirtel

Stéphane Wirtel  added the comment:

What will happen if we want to translate "true", "yes", ...?

-1

--
nosy: +matrixise

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue37564] ArgumentParser should support bool type according to truth values

2019-08-12 Thread paul j3


paul j3  added the comment:

This patch lacks proper documentation, and is too English language specific.

For users who assume 'type' means a class rather than function (callable) this 
change does not need documentation, since it accommodates that assumption.  But 
more experienced users, or ones who read the documentation with care, this 
change could be puzzling.  "Why does 'bool' work?"  Admittedly they are 
unlikely to try it.  Still, we are deviating the documented behavior.

We also need to pay attention to internationalization.  Argparse wraps most 
error messages and string displays in 'gettext'.  There isn't any other part of 
argparse that assumes that sys.argv strings have English meanings.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue37564] ArgumentParser should support bool type according to truth values

2019-07-15 Thread paul j3


paul j3  added the comment:

The use of `bool` as registry key only works because that object already exists 
as a builtin.  This amounts a form of shadowing, and sets a dangerous precedent.

type=bool

should work as documented at

https://docs.python.org/3/library/functions.html?highlight=bool#bool

even if that is not what a casual user would expect.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue37564] ArgumentParser should support bool type according to truth values

2019-07-11 Thread paul j3


paul j3  added the comment:

This is an old topic, though it may be easier to find a relevant StackOverflow 
thread.

https://stackoverflow.com/a/19233287/901925  (a 2013 thread)

I and others have explained that the `type` parameter is supposed to be a 
callable.  The default Python `bool` function only returns False for an empty 
string.  Previous discussions have suggested writing a function that parses 
strings like 'False' and 'No'.  It's nice that you have found a 
'distutils.utils.strtobool' that does this well.

But I have several qualms about the proposed patch:

- do we need to define a 'bool' type?  We already has 'store_true' and 
'store_false' actions.

- does this function need a 'bool' wrapper?

- do we need another import?  There's a relatively recent bug/issue that tries 
to speed up argparse by reducing the number of imports.

- if added, should this type be registered as the object bool or string 'bool'? 
 

Seems to me that the patch could be simplified to:

 self.register('type', 'bool', _strtobool)

According the original developer `type=bool` should be allowed (even if it 
doesn't do what many users expect):

https://bugs.python.org/issue14392

I point out that users are used to providing strings for 'action', but not for 
'type'.  The registry mechanism can be used to provide specialized type but so 
far only 'None' is registered.

https://bugs.python.org/issue26994

https://bugs.python.org/issue24754

This proposed patch might be innocuous, but it does break a consistent behavior 
- that 'type' should be a callable, and 'bool' is a standard Python function.

An alternative is to add to the docs a note (in the 'type' section) that

from distutils.util import strtobool
type=strtobool

could be used to parse strings as booleans.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue37564] ArgumentParser should support bool type according to truth values

2019-07-11 Thread Karthikeyan Singaravelan


Change by Karthikeyan Singaravelan :


--
nosy: +paul.j3

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue37564] ArgumentParser should support bool type according to truth values

2019-07-11 Thread Roundup Robot


Change by Roundup Robot :


--
keywords: +patch
pull_requests: +14509
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/14709

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue37564] ArgumentParser should support bool type according to truth values

2019-07-11 Thread Zach Beniash


New submission from Zach Beniash :

Today when using argparse.ArgumentParser it seems that the bool type is not 
supported in a logical way.
Foe example:
-
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--mybool", type=bool)
parsed_args = parser.parse(["--mybool", "False"])
--
parsed_args.my_bool evaluates to True

Instead we should expect to evaluate False here.

--
components: Library (Lib)
messages: 347686
nosy: Zach Beniash
priority: normal
severity: normal
status: open
title: ArgumentParser should support bool type according to truth values
type: behavior
versions: Python 2.7, Python 3.5, Python 3.6, Python 3.7, Python 3.8, Python 3.9

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com