|
From: Stefan P. <pa...@ik...> - 2011-01-04 14:19:05
|
I see what the source of confusion might be. I am talking about values after validation has been performed. So when I say that I assign something and then the value is of type something I mean that it is after validation. Maybe this clears up some issues? On Mon, Jan 3, 2011 at 4:49 PM, Michael Foord <fuz...@vo...> wrote: >> So when parsing the config file "1,2" is indeed interpreted as a list, > No. 1,2 without quotes is a list. '1,2" with quotes is a string that happens > to contain a comma. My point is that I can do foo = 1,2 in a config file and it works correctly, but if I do config["foo"] = '1,2' it does not work. I would fully understand that "foo = '1,2'" and config["foo"] = "'1,2'" would not work (Notice the quotes inside the quotes). I think it's confusing that I can write one thing in a file, but not the exact same thing in the source. In this case I assign 1,2 (n.b. without any quotes) in both cases. > Otherwise, as I indicated, string values that contain commas can be incorrectly split into lists. Can you name a situation when splitting a string when something expects a list would lead to unwanted results? Not splitting the string, and instead assigning it directly _always_ leads to incorrect results when a list is expected. > However quoted strings can contain commas that don't represent lists > which is why your suggestion of always splitting on commas is bad. I'm certainly not suggesting that a string containing commas is _always_ interpreted as a list. Only when a list is expected! Maybe there is an error in my code, and it is always split, but that is certainly not the intention. > What would be the use case of force_int? I don't really understand your > point here. Assigning a string to something expecting an integer "just works". Assigning a string to something expecting a list does not, instead force_list is needed. Don't you see the inconsistent behavior? I don't see why integers can be converted automatically (when needed) but lists require a separate type for this to work. > If you want a list value then assign a list. I could similarly say that if you want an int value then assign an int. Yet integer conversion from a string happens automatically. Why does not list conversion happen? > Alternatively, it sounds like a reasonable feature request to ask for a > method to parse a string using the ConfigObj syntax rules - so that you can > use ConfigObj syntax for assigning strings. I think that an assignment should work the same, regardless if it's in a config file or in the source code. Once again: Assume the spec: p = list() Case 1, Conf file: p = 1,2 Case 2, in source: config['p'] = '1,2' In both cases the right-hand side is: 1,2. The quotation marks in case 2 is there because of Python syntax. I am still assigning the value 1,2 without any quotes, just like in case 1. Given the above spec, in both cases, after validation, I expect p to be the list ['1','2']. Currently case 1 gives ['1','2'] while case 2 gives a validation error. I don't think it makes sense to get different results given that the right-hand side is the same in both cases. -- Stefan Parviainen |