Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

max() over itertools.ifilter() is broken in 0.18.0 #510

Copy link
Copy link
@xeron

Description

@xeron
Issue body actions

Python 2.7.16.
macOS 10.14.6.

Reproducible with:

import builtins
import operator

from itertools import ifilter
from random import shuffle


class Backend(object):
    def __init__(self, priority):
        super(Backend, self).__init__()
        self.priority = priority


def max(*args, **kwargs):
    """
    Add support for 'default' kwarg.

    >>> max([], default='res')
    'res'

    >>> max(default='res')
    Traceback (most recent call last):
    ...
    TypeError: ...

    >>> max('a', 'b', default='other')
    'b'
    """
    missing = object()
    default = kwargs.pop('default', missing)
    try:
        mmax = builtins.max(*args, **kwargs)
        return mmax
    except ValueError as exc:
        if 'empty sequence' in str(exc) and default is not missing:
            return default
        raise


by_priority = operator.attrgetter('priority')

backends = [Backend(5), Backend(1), Backend(0.5), Backend(0.6)]

while True:
    shuffle(backends)
    filtered_backends = ifilter(None, backends)
    max_object = max(filtered_backends, key=by_priority)
    print(max_object.priority)
    if max_object.priority != 5:
        exit(1)

Can't reproduce with 0.17.1.

This affects older versions of keyring library using similar code to select keychain backend where it wants max priority backend but returns the wrong one.

Also it looks like it depends on the order of the list.

Reactions are currently unavailable

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.