[ python-Bugs-1276587 ] dict('') doesn't raise a value error

SourceForge.net noreply at sourceforge.net
Thu Sep 1 00:17:27 CEST 2005


Bugs item #1276587, was opened at 2005-08-30 14:52
Message generated for change (Comment added) made by birkenfeld
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1276587&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Interpreter Core
Group: Python 2.4
>Status: Closed
>Resolution: Wont Fix
Priority: 5
Submitted By: Mike Foord (mjfoord)
Assigned to: Nobody/Anonymous (nobody)
Summary: dict('') doesn't raise a value error

Initial Comment:
The dict function will theoretically accept any sequence 
or bounded iterable that yields (key, value) tuples.

A side effect is that dict('') is valid - producing an emtpy 
dictionary.

dict(x) where x is *any* string other than '' fails with a 
ValueError. I suggest that dict('') ought to produce a 
ValueError to as a string is *never* a valid input to the 
dict function.

The current situation allows obscure errors to pass 
unnoticed.

----------------------------------------------------------------------

>Comment By: Reinhold Birkenfeld (birkenfeld)
Date: 2005-09-01 00:17

Message:
Logged In: YES 
user_id=1188172

Closing as Won't Fix, then.

----------------------------------------------------------------------

Comment By: Tim Peters (tim_one)
Date: 2005-08-30 16:39

Message:
Logged In: YES 
user_id=31435

It's not theoretical:  it's a fact that dict() accepts any iterable 
producing iterables each producing 2 objects (the latter don't 
have to be tuples; a (key, value) tuple is just one kind of 
iterable producing 2 objects; e.g., dict(["ab"]) == {'a': 'b'}).

An empty str meets the input requirements, so there's no 
way to stop this without special-case type-sniffing.  That's 
very unattractive, in part because it's impossible to guess 
which kinds of empty iterables would necessarily lead to an 
exception when passed to dict() had they not been empty.  
For example, passing an empty array.array (of any flavor) to 
dict() also constructs an empty dict.  The universe of iterable 
objects is vast.

Keeping it uniform and easy to explain (an empty iterable 
produces an empty dict) seems better to me than adding a 
maze of special cases that's bound to change over time 
("except for an empty str" ... "oh, or an empty 
unicode" ... "oh, or an empty array.array" ... "oh, or an empty 
file" ... "oh, oops, guess not, cuz a file with two lines works 
fine" ... etc).

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1276587&group_id=5470


More information about the Python-bugs-list mailing list