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

Bump mypy from 0.982 to 1.4.1 #7427

Merged
merged 7 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@ check_untyped_defs = True
follow_imports_for_stubs = True
#disallow_any_decorated = True
disallow_any_generics = True
disallow_any_unimported = True
disallow_incomplete_defs = True
disallow_subclassing_any = True
disallow_untyped_calls = True
disallow_untyped_decorators = True
disallow_untyped_defs = True
enable_error_code = ignore-without-code, possibly-undefined, redundant-expr, redundant-self, truthy-bool, truthy-iterable, unused-awaitable
implicit_reexport = False
no_implicit_optional = True
pretty = True
show_column_numbers = True
show_error_codes = True
strict_equality = True
warn_incomplete_stub = True
warn_redundant_casts = True
warn_return_any = True
#warn_unreachable = True
warn_unused_ignores = True
disallow_any_unimported = True
warn_return_any = True

[mypy-aiodns]
ignore_missing_imports = True
Expand Down
13 changes: 5 additions & 8 deletions aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
Any,
Awaitable,
Callable,
Collection,
Coroutine,
FrozenSet,
Generator,
Expand Down Expand Up @@ -450,11 +451,7 @@ async def _request(
auth = self._default_auth
# It would be confusing if we support explicit
# Authorization header with auth argument
if (
headers is not None
and auth is not None
and hdrs.AUTHORIZATION in headers
):
if auth is not None and hdrs.AUTHORIZATION in headers:
raise ValueError(
"Cannot combine AUTHORIZATION header "
"with AUTH argument or credentials "
Expand Down Expand Up @@ -666,7 +663,7 @@ def ws_connect(
url: StrOrURL,
*,
method: str = hdrs.METH_GET,
protocols: Iterable[str] = (),
protocols: Collection[str] = (),
timeout: Union[ClientWSTimeout, float, _SENTINEL, None] = sentinel,
receive_timeout: Optional[float] = None,
autoclose: bool = True,
Expand Down Expand Up @@ -712,7 +709,7 @@ async def _ws_connect(
url: StrOrURL,
*,
method: str = hdrs.METH_GET,
protocols: Iterable[str] = (),
protocols: Collection[str] = (),
timeout: Union[ClientWSTimeout, float, _SENTINEL, None] = sentinel,
receive_timeout: Optional[float] = None,
autoclose: bool = True,
Expand Down Expand Up @@ -1103,7 +1100,7 @@ def send(self, arg: None) -> "asyncio.Future[Any]":
return self._coro.send(arg)

def throw(self, arg: BaseException) -> None: # type: ignore[override]
self._coro.throw(arg)
self._coro.throw(arg) # type: ignore[unused-awaitable]

def close(self) -> None:
return self._coro.close()
Expand Down
6 changes: 3 additions & 3 deletions aiohttp/client_proto.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
ServerTimeoutError,
)
from .helpers import BaseTimerContext, set_exception, set_result
from .http import HttpResponseParser, RawResponseMessage
from .http import HttpResponseParser, RawResponseMessage, WebSocketReader
from .streams import EMPTY_PAYLOAD, DataQueue, StreamReader


Expand All @@ -25,7 +25,7 @@ def __init__(self, loop: asyncio.AbstractEventLoop) -> None:

self._payload: Optional[StreamReader] = None
self._skip_payload = False
self._payload_parser = None
self._payload_parser: Optional[WebSocketReader] = None

self._timer = None

Expand All @@ -46,7 +46,7 @@ def upgraded(self) -> bool:

@property
def should_close(self) -> bool:
if self._payload is not None and not self._payload.is_eof() or self._upgraded:
if self._payload is not None and not self._payload.is_eof():
return True

return (
Expand Down
9 changes: 5 additions & 4 deletions aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
try:
import cchardet as chardet
except ImportError: # pragma: no cover
import charset_normalizer as chardet # type: ignore[no-redef]
import charset_normalizer as chardet


__all__ = ("ClientRequest", "ClientResponse", "RequestInfo", "Fingerprint")
Expand Down Expand Up @@ -697,7 +697,7 @@ def __init__(
*,
writer: "asyncio.Task[None]",
continue100: Optional["asyncio.Future[bool]"],
timer: BaseTimerContext,
timer: Optional[BaseTimerContext],
request_info: RequestInfo,
traces: List["Trace"],
loop: asyncio.AbstractEventLoop,
Expand Down Expand Up @@ -922,7 +922,7 @@ def close(self) -> None:
return

self._closed = True
if self._loop is None or self._loop.is_closed():
if self._loop.is_closed():
return

if self._connection is not None:
Expand Down Expand Up @@ -974,7 +974,8 @@ def _cleanup_writer(self) -> None:

def _notify_content(self) -> None:
content = self.content
if content and content.exception() is None:
# content can be None here, but the types are cheated elsewhere.
if content and content.exception() is None: # type: ignore[truthy-bool]
content.set_exception(ClientConnectionError("Connection closed"))
self._released = True

Expand Down
5 changes: 3 additions & 2 deletions aiohttp/client_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def _send_heartbeat(self) -> None:
# fire-and-forget a task is not perfect but maybe ok for
# sending ping. Otherwise we need a long-living heartbeat
# task in the class.
self._loop.create_task(self._writer.ping())
self._loop.create_task(self._writer.ping()) # type: ignore[unused-awaitable]

if self._pong_response_cb is not None:
self._pong_response_cb.cancel()
Expand Down Expand Up @@ -280,7 +280,8 @@ async def receive(self, timeout: Optional[float] = None) -> WSMessage:
if msg.type == WSMsgType.CLOSE:
self._closing = True
self._close_code = msg.data
if not self._closed and self._autoclose:
# Could be closed elsewhere while awaiting reader
if not self._closed and self._autoclose: # type: ignore[redundant-expr]
await self.close()
elif msg.type == WSMsgType.CLOSING:
self._closing = True
Expand Down
4 changes: 2 additions & 2 deletions aiohttp/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,7 @@ async def _wrap_create_connection(
async with ceil_timeout(
timeout.sock_connect, ceil_threshold=timeout.ceil_threshold
):
return await self._loop.create_connection(*args, **kwargs) # type: ignore[return-value] # noqa
return await self._loop.create_connection(*args, **kwargs)
except cert_errors as exc:
raise ClientConnectorCertificateError(req.connection_key, exc) from exc
except ssl_errors as exc:
Expand Down Expand Up @@ -1355,7 +1355,7 @@ async def _create_connection(
async with ceil_timeout(
timeout.sock_connect, ceil_threshold=timeout.ceil_threshold
):
_, proto = await self._loop.create_pipe_connection( # type: ignore[attr-defined] # noqa: E501
_, proto = await self._loop.create_pipe_connection( # type: ignore[attr-defined]
self._factory, self._path
)
# the drain is required so that the connection_made is called
Expand Down
13 changes: 8 additions & 5 deletions aiohttp/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ def _weakref_handle(info: "Tuple[weakref.ref[object], str]") -> None:
def weakref_handle(
ob: object,
name: str,
timeout: float,
timeout: Optional[float],
loop: asyncio.AbstractEventLoop,
timeout_ceil_threshold: float = 5,
) -> Optional[asyncio.TimerHandle]:
Expand All @@ -602,7 +602,7 @@ def weakref_handle(

def call_later(
cb: Callable[[], Any],
timeout: float,
timeout: Optional[float],
loop: asyncio.AbstractEventLoop,
timeout_ceil_threshold: float = 5,
) -> Optional[asyncio.TimerHandle]:
Expand Down Expand Up @@ -716,7 +716,7 @@ def __exit__(
exc_tb: Optional[TracebackType],
) -> Optional[bool]:
if self._tasks:
self._tasks.pop()
self._tasks.pop() # type: ignore[unused-awaitable]

if exc_type is asyncio.CancelledError and self._cancelled:
raise asyncio.TimeoutError from None
Expand Down Expand Up @@ -762,7 +762,7 @@ def _parse_content_type(self, raw: str) -> None:
else:
msg = HeaderParser().parsestr("Content-Type: " + raw)
self._content_type = msg.get_content_type()
params = msg.get_params()
params = msg.get_params(())
self._content_dict = dict(params[1:]) # First element is content type again

@property
Expand Down Expand Up @@ -823,8 +823,11 @@ def __init__(self, name: str, t: Optional[Type[_T]] = None):
module: str = frame.f_globals["__name__"]
break
frame = frame.f_back
else:
raise RuntimeError("Failed to get module name.")

self._name = module + "." + name
# https://github.com/python/mypy/issues/14209
self._name = module + "." + name # type: ignore[possibly-undefined]
self._t = t

def __lt__(self, other: object) -> bool:
Expand Down
3 changes: 2 additions & 1 deletion aiohttp/http_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,8 @@ def feed_eof(self) -> None:

if chunk or self.size > 0:
self.out.feed_data(chunk, len(chunk))
if self.encoding == "deflate" and not self.decompressor.eof: # type: ignore
# decompressor is not brotli unless encoding is "br"
if self.encoding == "deflate" and not self.decompressor.eof: # type: ignore[union-attr]
raise ContentEncodingError("deflate")

self.out.feed_eof()
Expand Down
2 changes: 1 addition & 1 deletion aiohttp/http_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ async def _send_frame(
await self.protocol._drain_helper()

def _write(self, data: bytes) -> None:
if self.transport is None or self.transport.is_closing():
if self.transport.is_closing():
raise ConnectionResetError("Cannot write to closing transport")
self.transport.write(data)

Expand Down
4 changes: 2 additions & 2 deletions aiohttp/multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,8 @@ async def release(self) -> None:
async def text(self, *, encoding: Optional[str] = None) -> str:
"""Like read(), but assumes that body part contains text data."""
data = await self.read(decode=True)
# see https://www.w3.org/TR/html5/forms.html#multipart/form-data-encoding-algorithm # NOQA
# and https://dvcs.w3.org/hg/xhr/raw-file/tip/Overview.html#dom-xmlhttprequest-send # NOQA
# see https://www.w3.org/TR/html5/forms.html#multipart/form-data-encoding-algorithm
# and https://dvcs.w3.org/hg/xhr/raw-file/tip/Overview.html#dom-xmlhttprequest-send
encoding = encoding or self.get_charset(default="utf-8")
return data.decode(encoding)

Expand Down
4 changes: 1 addition & 3 deletions aiohttp/streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ def __aiter__(self) -> AsyncStreamIterator[bytes]:

def iter_chunked(self, n: int) -> AsyncStreamIterator[bytes]:
"""Returns an asynchronous iterator that yields chunks of size n."""
return AsyncStreamIterator(
lambda: self.read(n) # type: ignore[attr-defined,no-any-return]
)
return AsyncStreamIterator(lambda: self.read(n)) # type: ignore[attr-defined]

def iter_any(self) -> AsyncStreamIterator[bytes]:
"""Yield all available data as soon as it is received."""
Expand Down
7 changes: 3 additions & 4 deletions aiohttp/web_fileresponse.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,13 @@ async def prepare(self, request: "BaseRequest") -> Optional[AbstractStreamWriter
):
return await self._not_modified(request, etag_value, last_modified)

ct = None
if hdrs.CONTENT_TYPE not in self.headers:
ct, encoding = mimetypes.guess_type(str(filepath))
if not ct:
ct = "application/octet-stream"
should_set_ct = True
else:
encoding = "gzip" if gzip else None
should_set_ct = False

status = self._status
file_size = st.st_size
Expand Down Expand Up @@ -250,8 +249,8 @@ async def prepare(self, request: "BaseRequest") -> Optional[AbstractStreamWriter
# return a HTTP 206 for a Range request.
self.set_status(status)

if should_set_ct:
self.content_type = ct # type: ignore[assignment]
if ct:
self.content_type = ct
if encoding:
self.headers[hdrs.CONTENT_ENCODING] = encoding
if gzip:
Expand Down
8 changes: 5 additions & 3 deletions aiohttp/web_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def __init__(
tcp_keepalive: bool = True,
logger: Logger = server_logger,
access_log_class: _AnyAbstractAccessLogger = AccessLogger,
access_log: Logger = access_logger,
access_log: Optional[Logger] = access_logger,
access_log_format: str = AccessLogger.LOG_FORMAT,
max_line_size: int = 8190,
max_field_size: int = 8190,
Expand Down Expand Up @@ -545,7 +545,8 @@ async def start(self) -> None:

# Drop the processed task from asyncio.Task.all_tasks() early
del task
if reset:
# https://github.com/python/mypy/issues/14309
if reset: # type: ignore[possibly-undefined]
self.log_debug("Ignored premature client disconnection 2")
break

Expand All @@ -555,7 +556,8 @@ async def start(self) -> None:
# check payload
if not payload.is_eof():
lingering_time = self._lingering_time
if not self._force_close and lingering_time:
# Could be force closed while awaiting above tasks.
if not self._force_close and lingering_time: # type: ignore[redundant-expr]
self.log_debug(
"Start lingering close timer for %s sec.", lingering_time
)
Expand Down
6 changes: 2 additions & 4 deletions aiohttp/web_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ def etag(self, value: Optional[Union[ETag, str]]) -> None:
elif isinstance(value, str):
validate_etag_value(value)
self._headers[hdrs.ETAG] = f'"{value}"'
elif isinstance(value, ETag) and isinstance(value.value, str):
elif isinstance(value, ETag) and isinstance(value.value, str): # type: ignore[redundant-expr]
validate_etag_value(value.value)
hdr_value = f'W/"{value.value}"' if value.is_weak else f'"{value.value}"'
self._headers[hdrs.ETAG] = hdr_value
Expand Down Expand Up @@ -605,9 +605,7 @@ def text(self) -> Optional[str]:

@text.setter
def text(self, text: str) -> None:
assert text is None or isinstance(
text, str
), "text argument must be str (%r)" % type(text)
assert isinstance(text, str), "text argument must be str (%r)" % type(text)

if self.content_type == "application/octet-stream":
self.content_type = "text/plain"
Expand Down
7 changes: 4 additions & 3 deletions aiohttp/web_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,11 @@ def _reset_heartbeat(self) -> None:

def _send_heartbeat(self) -> None:
if self._heartbeat is not None and not self._closed:
assert self._loop is not None
assert self._loop is not None and self._writer is not None
# fire-and-forget a task is not perfect but maybe ok for
# sending ping. Otherwise we need a long-living heartbeat
# task in the class.
self._loop.create_task(self._writer.ping()) # type: ignore[union-attr]
self._loop.create_task(self._writer.ping()) # type: ignore[unused-awaitable]

if self._pong_response_cb is not None:
self._pong_response_cb.cancel()
Expand Down Expand Up @@ -469,7 +469,8 @@ async def receive(self, timeout: Optional[float] = None) -> WSMessage:
if msg.type == WSMsgType.CLOSE:
self._closing = True
self._close_code = msg.data
if not self._closed and self._autoclose:
# Could be closed while awaiting reader.
if not self._closed and self._autoclose: # type: ignore[redundant-expr]
await self.close()
elif msg.type == WSMsgType.CLOSING:
self._closing = True
Expand Down
4 changes: 2 additions & 2 deletions examples/background_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ async def on_shutdown(app: web.Application) -> None:


async def listen_to_redis(app: web.Application) -> None:
sub = await aioredis.Redis(host="localhost", port=6379)
ch, *_ = await sub.subscribe("news")
try:
sub = await aioredis.Redis(host="localhost", port=6379)
ch, *_ = await sub.subscribe("news")
async for msg in ch.iter(encoding="utf-8"):
# Forward message to all connected websockets:
for ws in app[websockets]:
Expand Down
2 changes: 1 addition & 1 deletion requirements/constraints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ multidict==6.0.4
# via
# -r requirements/multidict.txt
# yarl
mypy==0.982 ; implementation_name == "cpython"
mypy==1.4.1 ; implementation_name == "cpython"
# via
# -r requirements/lint.txt
# -r requirements/test.txt
Expand Down
2 changes: 1 addition & 1 deletion requirements/lint.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-r typing-extensions.txt
aioredis==2.0.1
mypy==0.982; implementation_name=="cpython"
mypy==1.4.1; implementation_name=="cpython"
pre-commit==3.3.3
pytest==7.4.0
slotscheck==0.8.0
Expand Down
Loading