Jason R. Coombs avatar Jason R. Coombs committed 70513bb

always_iterable now returns an empty iterable when None is supplied

Comments (0)

Files changed (2)

 Changes
 -------
 
+6.0
+~~~
+
+* `itertools.always_iterable` now returns an empty iterable when the input
+  is None. This approach appears to work better for the majority of use-cases.
+
 5.5
 ~~~
 

jaraco/util/itertools.py

 def always_iterable(item):
 	"""
 	Given an object, always return an iterable. If the item is not
-	already iterable, return a tuple containing only the item.
+	already iterable, return a tuple containing only the item. If item is
+	None, an empty iterable is returned.
 
 	>>> always_iterable([1,2,3])
 	[1, 2, 3]
 	>>> always_iterable('foo')
 	(u'foo',)
 	>>> always_iterable(None)
-	(None,)
+	()
 	>>> always_iterable(xrange(10))
 	xrange(10)
 	>>> def _test_func(): yield "I'm iterable"
 	>>> print(next(always_iterable(_test_func())))
 	I'm iterable
 	"""
+	if item is None:
+		item = ()
 	if isinstance(item, basestring) or not hasattr(item, '__iter__'):
 		item = item,
 	return item
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.