Skip to content

Should HTTPMessage return Optional[str] for __getitem__? #2333

@asottile

Description

@asottile
>>> from urllib.request import urlopen
>>> urlopen('https://example.com')
<http.client.HTTPResponse object at 0x7fc608f39fd0>
>>> resp = urlopen('https://example.com')
>>> resp.headers['foo']
>>> resp.headers['connection']
'close'
>>> type(resp.headers['connection'])
<class 'str'>
import urllib.request
from typing import Optional

resp = urllib.request.urlopen('https://example.com')
x: Optional[str] = resp.headers['connection']
$ mypy t.py
t.py:5: error: Incompatible types in assignment (expression has type "Union[str, Header, None]", variable has type "Optional[str]")

If this sounds sane, I'd like to add __getitem__ here?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions