PythonCharmers/python-future

Handle special case for filter(bool)

Open

#437 ouverte le 23 févr. 2019

Voir sur GitHub
 (2 commentaires) (0 réactions) (0 assignés)Python (327 forks)batch import
0.20enhancementhelp wanted

Métriques du dépôt

Stars
 (1 172 stars)
Métriques de merge PR
 (Aucune PR mergée en 30 j)

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']
>>>

Guide contributeur