This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: option.dest not set when callback called with optparse
Type: Stage:
Components: Library (Lib) Versions: Python 2.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: gward Nosy List: cjw296, draghuram, gvanrossum, gward
Priority: normal Keywords:

Created on 2007-10-06 16:03 by cjw296, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
test.py cjw296, 2007-10-06 16:03
Messages (12)
msg56252 - (view) Author: Chris Withers (cjw296) * (Python committer) Date: 2007-10-06 16:03
The attached script demonstrates the bug.

option.dest should be set, even if not explicitly supplied in the call
to parser.add_option, by the time the callback is called.

Unless dest='something' is specified in the call to add_option, you will
get the following error when running "python test.py --myopt":

  File "test.py", line 4, in mycallback
    setattr(parser.values,option.dest,value)
TypeError: attribute name must be string

...as options.dest is still None.

I would expect options.dest to be computed as per the algorithm in
optparse.Option._check_dest.
msg56262 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2007-10-07 23:46
Since you've done so much work debugging this, would you mind submitting
a patch, including a unit test that confirms the desired behavior?
msg56274 - (view) Author: Chris Withers (cjw296) * (Python committer) Date: 2007-10-08 12:09
I'm not sure I fully understand where the fix should go but I'll put a
note in to look into this.

In the meantime, a good workaround is to supply an explicit destination:

parser.add_option('--myopt',action='callback',callback=mycallback,dest='myopt')
msg56277 - (view) Author: Raghuram Devarakonda (draghuram) (Python triager) Date: 2007-10-08 14:30
I have never used callbacks but went through the docs/code and I am not
sure if there is a bug here. 

_check_dest() creates 'dest' only if 'type' is specified for the
'callback' option. With callbacks, it can not guess the type as there is
no 'store' action. The document does say that without 'type', optparse
does not know how many arguments to consume. I added 'type' to your
example add_option() and all is well. 

Please correct me if I am missing some thing here.
msg56278 - (view) Author: Chris Withers (cjw296) * (Python committer) Date: 2007-10-08 14:32
But what type should I specify?

I was using a callback to get a datetime object as an option...
msg56279 - (view) Author: Raghuram Devarakonda (draghuram) (Python triager) Date: 2007-10-08 14:47
>
> But what type should I specify?
>
> I was using a callback to get a datetime object as an option...
>

If you are looking for a new type of option than what are already
supported (string, int, long, choice, float and complex), you need to
extend optparse (there is a section for that). I personally prefer a
simple approach where the option value is taken as a string and any
processing is done on the value to convert to what ever type of object
one wants. For example, in this case, you can convert a string, say,
2007-10-8, internally to a datetime object.
msg59185 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2008-01-04 00:09
Ping? I was hoping for a patch?
msg59231 - (view) Author: Raghuram Devarakonda (draghuram) (Python triager) Date: 2008-01-04 15:29
I was under the impression that no change is necessary but I will take a
closer look and will see if something needs to be done.
msg59240 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2008-01-04 17:07
Hm, perhaps I misinterpreted the discussion. If no patch is necessary,
please close as won't fix or invalid.
msg59241 - (view) Author: Raghuram Devarakonda (draghuram) (Python triager) Date: 2008-01-04 17:13
> Hm, perhaps I misinterpreted the discussion. If no patch is necessary,
> please close as won't fix or invalid.

I spent some time looking at optparse code and OP's request.
_check_dest has a clear comment that says "dest" will be set iff a
"store" action is selected (which callback is not) or if a "type" is
given. But from the code itself, I don't see how setting "dest" in all
cases would be bad. Do you want to assign this to Greg Ward to ask his
opinion? Also, it doesn't look like we can directly patch optparse.py
as it seems to be auto-generated. On a side note, I don't have tracker
permissions to change status.
msg59244 - (view) Author: Raghuram Devarakonda (draghuram) (Python triager) Date: 2008-01-04 17:32
FWIW, the change to _check_dest to always set 'dest' breaks quite a few
tests.
msg59248 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2008-01-04 19:10
Then let's leave well enough alone.  I believe Greg Ward doesn't usually
respond to queries about optparse, but I'm assigning it to him anyway in
case his interest is piqued.  But for now, let's close this.
History
Date User Action Args
2022-04-11 14:56:27adminsetgithub: 45584
2008-01-04 19:10:53gvanrossumsetstatus: open -> closed
assignee: gward
resolution: not a bug
messages: + msg59248
nosy: + gward
2008-01-04 17:32:05draghuramsetmessages: + msg59244
2008-01-04 17:13:52draghuramsetmessages: + msg59241
2008-01-04 17:07:34gvanrossumsetmessages: + msg59240
2008-01-04 15:29:59draghuramsetmessages: + msg59231
2008-01-04 00:09:39gvanrossumsetmessages: + msg59185
2008-01-03 23:59:53christian.heimessetpriority: normal
versions: - Python 2.4, Python 2.3
2007-10-08 14:47:56draghuramsetmessages: + msg56279
2007-10-08 14:32:34cjw296setmessages: + msg56278
2007-10-08 14:30:43draghuramsetnosy: + draghuram
messages: + msg56277
2007-10-08 12:09:53cjw296setmessages: + msg56274
2007-10-07 23:46:46gvanrossumsetnosy: + gvanrossum
messages: + msg56262
2007-10-06 16:03:12cjw296create