PythonCharmers/python-future

Handle special case for filter(bool)

Open

#437 opened on Feb 23, 2019

View on GitHub
 (2 comments) (0 reactions) (0 assignees)Python (327 forks)batch import
0.20enhancementhelp wanted

Repository metrics

Stars
 (1,172 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

lib2to3.fixes.fix_filter currently special cases filter(None, ...) to rewrite it as a list comprehension. However, the Python interpreter behavior is identical for filter(bool, ...), but it is not rewritten as a list comprehension.

[04:25:25] ~/Downloads $ python3
Python 3.7.0 (default, Oct 19 2018, 14:34:21)
[Clang 10.0.0 (clang-1000.11.45.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> foo = ['bar', 'baz', '0', 0, 1, None, True, False, 'bar2']
>>> list(filter(None, foo))
['bar', 'baz', '0', 1, True, 'bar2']
>>> list(filter(bool, foo))
['bar', 'baz', '0', 1, True, 'bar2']
>>>
[04:25:56] ~/Downloads $ python2
Python 2.7.15 (default, Dec 13 2018, 11:08:09)
[GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.11.45.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> foo = ['bar', 'baz', '0', 0, 1, None, True, False, 'bar2']
>>> filter(None, foo)
['bar', 'baz', '0', 1, True, 'bar2']
>>> filter(bool, foo)
['bar', 'baz', '0', 1, True, 'bar2']
>>>

Contributor guide