Skip to content

[www-authenticate] Annotate parse's return value type - #16718

Open
Dhairya-007 wants to merge 1 commit into
python:mainfrom
Dhairya-007:www-auth-parse-return
Open

Dhairya-007 wants to merge 1 commit into
python:mainfrom
Dhairya-007:www-auth-parse-return

Conversation

@Dhairya-007

Copy link
Copy Markdown
Contributor

parse returned CaseFoldedOrderedDict[Incomplete], so every challenge value came back as Any.

The value type is fully determined by one line of the implementation:

challenges[name] = (args and args[0]) or kwargs or None

It is the single token when the challenge carries one, the parsed key/value parameters otherwise, and None when the challenge is bare. Checked against www-authenticate 0.9.2:

>>> parse('Basic realm="a", Bearer, Negotiate abc==')
CaseFoldedOrderedDict([('basic', {'realm': 'a'}),
                       ('bearer', None),
                       ('negotiate', 'abc==')])

Annotated as str | dict[str, str] | None so callers can narrow the three cases:

for scheme, params in parse(header).items():
    if params is None:              # bare challenge, e.g. "Bearer"
        ...
    elif isinstance(params, str):   # single token, e.g. "Negotiate abc=="
        ...
    else:                           # key/value params, e.g. Basic/Digest
        realm = params.get("realm")

The now-unused Incomplete import is dropped. Verified with mypy --strict.

`parse` returned `CaseFoldedOrderedDict[Incomplete]`. The value stored
for each challenge is decided by one line of the implementation:

    challenges[name] = (args and args[0]) or kwargs or None

so it is the single token when the challenge carries one, the parsed
key/value parameters otherwise, and None when the challenge is bare.
Checked against www-authenticate 0.9.2:

    >>> parse('Basic realm="a", Bearer, Negotiate abc==')
    CaseFoldedOrderedDict([('basic', {'realm': 'a'}),
                           ('bearer', None),
                           ('negotiate', 'abc==')])

Annotated as `str | dict[str, str] | None`, which lets callers narrow
the three cases instead of getting `Any`.
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

This branch has not been deployed

No deployments
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

Successfully merging this pull request may close these issues.

1 participant