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

Add typings for web_response #3317

Closed
wants to merge 13 commits into from
2 changes: 2 additions & 0 deletions CHANGES/3267.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Don't raise a warning if ``NETRC`` environment variable is not set and ``~/.netrc`` file
doesn't exist.
1 change: 1 addition & 0 deletions CHANGES/3341.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added a `lru_cache` to the `parse_mimetype` method
71 changes: 41 additions & 30 deletions aiohttp/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import inspect
import netrc
import os
import platform
import re
import sys
import time
Expand All @@ -25,7 +26,7 @@

import async_timeout
import attr
from multidict import MultiDict
from multidict import MultiDict, MultiDictProxy
from yarl import URL

from . import hdrs
Expand Down Expand Up @@ -155,30 +156,39 @@ def strip_auth_from_url(url: URL) -> Tuple[URL, Optional[BasicAuth]]:


def netrc_from_env() -> Optional[netrc.netrc]:
netrc_obj = None
netrc_path = os.environ.get('NETRC') # type: Optional[PathLike]
try:
if netrc_path is not None:
netrc_path = Path(netrc_path)
else:
"""Attempt to load the netrc file from the path specified by the env-var
NETRC or in the default location in the user's home directory.

Returns None if it couldn't be found or fails to parse.
"""
netrc_env = os.environ.get('NETRC')

if netrc_env is not None:
netrc_path = Path(netrc_env)
else:
try:
home_dir = Path.home()
if os.name == 'nt': # pragma: no cover
netrc_path = home_dir.joinpath('_netrc')
else:
netrc_path = home_dir.joinpath('.netrc')
except RuntimeError as e: # pragma: no cover
# if pathlib can't resolve home, it may raise a RuntimeError
client_logger.warning('Could not resolve home directory when '
'trying to look for .netrc file: %s', e)
return None

if netrc_path and netrc_path.is_file():
try:
netrc_obj = netrc.netrc(str(netrc_path))
except (netrc.NetrcParseError, OSError) as e:
client_logger.warning(".netrc file parses fail: %s", e)
netrc_path = home_dir / (
'_netrc' if platform.system() == 'Windows' else '.netrc')

try:
return netrc.netrc(str(netrc_path))
except netrc.NetrcParseError as e:
client_logger.warning('Could not parse .netrc file: %s', e)
except OSError as e:
# we couldn't read the file (doesn't exist, permissions, etc.)
if netrc_env or netrc_path.is_file():
# only warn if the enviroment wanted us to load it,
# or it appears like the default file does actually exist
client_logger.warning('Could not read .netrc file: %s', e)

if netrc_obj is None:
client_logger.warning("could't find .netrc file")
except RuntimeError as e: # pragma: no cover
""" handle error raised by pathlib """
client_logger.warning("could't find .netrc file: %s", e)
return netrc_obj
return None


@attr.s(frozen=True, slots=True)
Expand Down Expand Up @@ -234,9 +244,10 @@ class MimeType:
type = attr.ib(type=str)
subtype = attr.ib(type=str)
suffix = attr.ib(type=str)
parameters = attr.ib(type=MultiDict) # type: MultiDict[str]
parameters = attr.ib(type=MultiDictProxy) # type: MultiDictProxy[str]


@functools.lru_cache(maxsize=56)
def parse_mimetype(mimetype: str) -> MimeType:
"""Parses a MIME type into its components.

Expand All @@ -252,17 +263,17 @@ def parse_mimetype(mimetype: str) -> MimeType:

"""
if not mimetype:
return MimeType(type='', subtype='', suffix='', parameters=MultiDict())
return MimeType(type='', subtype='', suffix='',
parameters=MultiDictProxy(MultiDict()))

parts = mimetype.split(';')
params_lst = []
params = MultiDict() # type: MultiDict[str]
for item in parts[1:]:
if not item:
continue
key, value = cast(Tuple[str, str],
item.split('=', 1) if '=' in item else (item, ''))
params_lst.append((key.lower().strip(), value.strip(' "')))
params = MultiDict(params_lst)
params.add(key.lower().strip(), value.strip(' "'))

fulltype = parts[0].strip().lower()
if fulltype == '*':
Expand All @@ -274,7 +285,7 @@ def parse_mimetype(mimetype: str) -> MimeType:
if '+' in stype else (stype, ''))

return MimeType(type=mtype, subtype=stype, suffix=suffix,
parameters=params)
parameters=MultiDictProxy(params))


def guess_filename(obj: Any, default: Optional[str]=None) -> Optional[str]:
Expand Down Expand Up @@ -556,8 +567,8 @@ class HeadersMixin:
ATTRS = frozenset([
'_content_type', '_content_dict', '_stored_content_type'])

_content_type = None
_content_dict = None
_content_type = None # Optional[str]
_content_dict = None # Optional[Dict[str, str]]
_stored_content_type = sentinel

def _parse_content_type(self, raw: str) -> None:
Expand Down
5 changes: 3 additions & 2 deletions aiohttp/http.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import http.server
import sys
from typing import Mapping, Tuple # noqa

from . import __version__
from .http_exceptions import HttpProcessingError
Expand Down Expand Up @@ -31,6 +32,6 @@


SERVER_SOFTWARE = 'Python/{0[0]}.{0[1]} aiohttp/{1}'.format(
sys.version_info, __version__)
sys.version_info, __version__) # type: str

RESPONSES = http.server.BaseHTTPRequestHandler.responses
RESPONSES = http.server.BaseHTTPRequestHandler.responses # type: Mapping[int, Tuple[str, str]] # noqa
18 changes: 9 additions & 9 deletions aiohttp/payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ def register(self,

class Payload(ABC):

_size = None # type: Optional[float]
_size = None # type: Optional[int]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I am pretty sure that making this float in the beginning was an err on my side.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, 3.5 bytes will never happen.

_headers = None # type: Optional[_CIMultiDict]
_content_type = 'application/octet-stream' # type: Optional[str]
_content_type = 'application/octet-stream' # type: str

def __init__(self,
value: Any,
Expand All @@ -128,11 +128,11 @@ def __init__(self,

if content_type is sentinel:
content_type = None

self._content_type = content_type
if content_type is not None:
self._content_type = content_type

@property
def size(self) -> Optional[float]:
def size(self) -> Optional[int]:
"""Size of the payload."""
return self._size

Expand All @@ -152,7 +152,7 @@ def encoding(self) -> Optional[str]:
return self._encoding

@property
def content_type(self) -> Optional[str]:
def content_type(self) -> str:
"""Content type"""
if self._content_type is not None:
return self._content_type
Expand Down Expand Up @@ -305,7 +305,7 @@ def __init__(self,
)

@property
def size(self) -> Optional[float]:
def size(self) -> Optional[int]:
try:
return os.fstat(self._value.fileno()).st_size - self._value.tell()
except OSError:
Expand All @@ -324,7 +324,7 @@ async def write(self, writer: AbstractStreamWriter) -> None:
class BytesIOPayload(IOBasePayload):

@property
def size(self) -> float:
def size(self) -> int:
position = self._value.tell()
end = self._value.seek(0, os.SEEK_END)
self._value.seek(position)
Expand All @@ -334,7 +334,7 @@ def size(self) -> float:
class BufferedReaderPayload(IOBasePayload):

@property
def size(self) -> Optional[float]:
def size(self) -> Optional[int]:
try:
return os.fstat(self._value.fileno()).st_size - self._value.tell()
except OSError:
Expand Down
4 changes: 2 additions & 2 deletions aiohttp/web_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,13 @@ def _format_r(request: BaseRequest,
@staticmethod
def _format_s(request: BaseRequest,
response: StreamResponse,
time: float) -> str:
time: float) -> int:
return response.status

@staticmethod
def _format_b(request: BaseRequest,
response: StreamResponse,
time: float) -> str:
time: float) -> int:
return response.body_length

@staticmethod
Expand Down
Loading