Skip to content

Misleading documentation of AcceptLanguageValidHeader.lookup #442

Open
@romuald

Description

@romuald

URL of the documentation: https://docs.pylonsproject.org/projects/webob/en/stable/api/webob.html#webob.acceptparse.AcceptLanguageValidHeader.lookup

language_tags is described as being an iterable, however this is not strictly true, as passing a dict (with languages as keys) will break the call for example.

See the test bellow for an actual example

I'm guessing the actual type is a Sequence instead

from unittest.mock import Mock

from webob.acceptparse import AcceptLanguageValidHeader


def test_lookup_ok():
    languages = ('en', 'fr')

    header = AcceptLanguageValidHeader('fr, *')

    assert header.lookup(languages, default='fallback') == 'fr'


def test_lookup_ko():
    languages = dict.fromkeys(('en', 'fr'))

    for key in languages:
        # Real world: associate with a gettext instance
        languages[key] = Mock()

    header = AcceptLanguageValidHeader('fr, *')

    assert header.lookup(languages, default='fallback') == 'fr'

""" <<< output <<<<<<
____________________ test_lookup_ko ____________________

    def test_lookup_ko():
        known = dict.fromkeys(('en', 'fr'))
    
        for key in known:
            # Real world: associate with a gettext instance
            known[key] = Mock()
    
        header = AcceptLanguageValidHeader('fr, *')
    
>       assert header.lookup(known, default='fallback') == 'fr'

test_accept.py:23: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
.../webob/acceptparse.py:4686: in lookup
    match = best_match(range_=range_.lower())
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

range_ = 'fr'

    def best_match(range_):
        subtags = range_.split('-')
        while True:
            for index, tag in enumerate(lowered_tags):
                if tag in not_acceptable_ranges:
                    continue
                    # We think a non-'*' range with q=0 represents only
                    # itself as a tag, and there should be no falling back
                    # with subtag truncation. For example, with
                    # 'en-gb;q=0', it should not mean 'en;q=0': the client
                    # is unlikely to expect that specifying 'en-gb' as not
                    # acceptable would mean that 'en' is also not
                    # acceptable. There is no guidance on this at all in
                    # the RFCs, so it is left to us to decide how it should
                    # work.
    
                if tag == range_:
>                   return tags[index]  # return the pre-lowered tag
E                   KeyError: 1

.../webob/acceptparse.py:4670: KeyError
================ short test summary info ================
FAILED test_accept.py::test_lookup_ko - KeyError: 1

"""

Metadata

Metadata

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