Skip to content

Handle special case for filter(bool) #437

Open
@jmadler

Description

@jmadler

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions