PythonCharmers/python-future

Handle special case for filter(bool)

Open

#437 geöffnet am 23. Feb. 2019

Auf GitHub ansehen
 (2 Kommentare) (0 Reaktionen) (0 zugewiesene Personen)Python (327 Forks)batch import
0.20enhancementhelp wanted

Repository-Metriken

Stars
 (1.172 Stars)
PR-Merge-Metriken
 (Keine gemergten PRs in 30 T)

Beschreibung

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