[Python-3000] Warning about future-unsafe usage patterns in Python 2.x e.g. dict.keys().sort()

Brian Quinlan brian at sweetapp.com
Mon Aug 28 11:35:39 CEST 2006

It is my understanding that, in Python 3000, certain functions and 
methods that currently return lists will return some sort of view type 
(e.g. dict.values()) or an iterator (e.g. zip). So certain usage 
patterns will no longer be supported e.g. d.keys().sort().

The attached patch, which is a diff against the subversion "trunk" of 
Python 2.x, tries to warn the user about these kind of future-unsafe 
usage patterns. It works by storing the type that the list will become 
in the future, at creation time, and checking to see if called list 
functions will be supported by that type in the future.

Currently the patch if very incomplete and the idea itself may be 
flawed. But I thought it was interesting to run against my own code to 
see what potential problems it has. Example:

Type "help", "copyright", "credits" or "license" for more information.
 >>> d = {"apple" : "sweet", "orange" : "tangy"}
 >>> "juicy" in d.values()
 >>> d.keys().sort()
__main__:1: DeprecationWarning: dictionary view will not support sort
 >>> "a" in zip([1,2,3,4], "abcd")
__main__:1: DeprecationWarning: iterator will not support contains

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: warn_list_usage.diff
Url: http://mail.python.org/pipermail/python-3000/attachments/20060828/8f08a2a7/attachment-0001.diff 

More information about the Python-3000 mailing list