Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misleading documentation of AcceptLanguageValidHeader.lookup #442

Open
romuald opened this issue Nov 18, 2022 · 1 comment
Open

Misleading documentation of AcceptLanguageValidHeader.lookup #442

romuald opened this issue Nov 18, 2022 · 1 comment
Assignees

Comments

@romuald
Copy link

romuald commented Nov 18, 2022

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

"""
@mmerickel
Copy link
Member

I think this is correct. The issue is the tags[index] is more strict than an iterable contract and a Sequence would be more accurate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants