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

Upgrade to mypy 0.930 #878

Merged
merged 1 commit into from
Dec 25, 2021
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
4 changes: 3 additions & 1 deletion kopf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
MultiProgressStorage,
SmartProgressStorage,
)
from kopf._cogs.helpers.typedefs import (
Logger,
)
from kopf._cogs.helpers.versions import (
version as __version__,
)
Expand Down Expand Up @@ -95,7 +98,6 @@
lifecycles, # as a separate name on the public namespace
)
from kopf._core.actions.execution import (
Logger,
ErrorsMode,
TemporaryError,
PermanentError,
Expand Down
9 changes: 5 additions & 4 deletions kopf/_cogs/aiokits/aiotasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
so there is no added overhead; instead, the implicit overhead is made explicit.
"""
import asyncio
import logging
import sys
from typing import TYPE_CHECKING, Any, Awaitable, Callable, Collection, Coroutine, \
Generator, NamedTuple, Optional, Set, Tuple, TypeVar, Union

from kopf._cogs.helpers import typedefs

_T = TypeVar('_T')

# A workaround for a difference in tasks at runtime and type-checking time.
Expand Down Expand Up @@ -79,7 +80,7 @@ async def guard(
flag: Optional[asyncio.Event] = None,
finishable: bool = False,
cancellable: bool = False,
logger: Optional[Union[logging.Logger, logging.LoggerAdapter]] = None,
logger: Optional[typedefs.Logger] = None,
) -> None:
"""
A guard for a presumably eternal (never-finishing) task.
Expand Down Expand Up @@ -125,7 +126,7 @@ def create_guarded_task(
flag: Optional[asyncio.Event] = None,
finishable: bool = False,
cancellable: bool = False,
logger: Optional[Union[logging.Logger, logging.LoggerAdapter]] = None,
logger: Optional[typedefs.Logger] = None,
) -> Task:
"""
Create a guarded eternal task. See :func:`guard` for explanation.
Expand Down Expand Up @@ -165,7 +166,7 @@ async def stop(
quiet: bool = False,
cancelled: bool = False,
interval: Optional[float] = None,
logger: Optional[Union[logging.Logger, logging.LoggerAdapter]] = None,
logger: Optional[typedefs.Logger] = None,
) -> Tuple[Set[Task], Set[Task]]:
"""
Cancel the tasks and wait for them to finish; log if some are stuck.
Expand Down
16 changes: 8 additions & 8 deletions kopf/_cogs/clients/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
import collections.abc
import itertools
import json
import logging
import ssl
import urllib.parse
from typing import Any, AsyncIterator, Mapping, Optional, Tuple, Union
from typing import Any, AsyncIterator, Mapping, Optional, Tuple

import aiohttp

from kopf._cogs.aiokits import aiotasks
from kopf._cogs.clients import auth, errors
from kopf._cogs.configs import configuration
from kopf._cogs.helpers import typedefs


@auth.authenticated
Expand Down Expand Up @@ -50,7 +50,7 @@ async def request(
headers: Optional[Mapping[str, str]] = None,
timeout: Optional[aiohttp.ClientTimeout] = None,
context: Optional[auth.APIContext] = None, # injected by the decorator
logger: Union[logging.Logger, logging.LoggerAdapter],
logger: typedefs.Logger,
) -> aiohttp.ClientResponse:
if context is None: # for type-checking!
raise RuntimeError("API instance is not injected by the decorator.")
Expand Down Expand Up @@ -106,7 +106,7 @@ async def get(
payload: Optional[object] = None,
headers: Optional[Mapping[str, str]] = None,
timeout: Optional[aiohttp.ClientTimeout] = None,
logger: Union[logging.Logger, logging.LoggerAdapter],
logger: typedefs.Logger,
) -> Any:
response = await request(
method='get',
Expand All @@ -128,7 +128,7 @@ async def post(
payload: Optional[object] = None,
headers: Optional[Mapping[str, str]] = None,
timeout: Optional[aiohttp.ClientTimeout] = None,
logger: Union[logging.Logger, logging.LoggerAdapter],
logger: typedefs.Logger,
) -> Any:
response = await request(
method='post',
Expand All @@ -150,7 +150,7 @@ async def patch(
payload: Optional[object] = None,
headers: Optional[Mapping[str, str]] = None,
timeout: Optional[aiohttp.ClientTimeout] = None,
logger: Union[logging.Logger, logging.LoggerAdapter],
logger: typedefs.Logger,
) -> Any:
response = await request(
method='patch',
Expand All @@ -172,7 +172,7 @@ async def delete(
payload: Optional[object] = None,
headers: Optional[Mapping[str, str]] = None,
timeout: Optional[aiohttp.ClientTimeout] = None,
logger: Union[logging.Logger, logging.LoggerAdapter],
logger: typedefs.Logger,
) -> Any:
response = await request(
method='delete',
Expand All @@ -195,7 +195,7 @@ async def stream(
headers: Optional[Mapping[str, str]] = None,
timeout: Optional[aiohttp.ClientTimeout] = None,
stopper: Optional[aiotasks.Future] = None,
logger: Union[logging.Logger, logging.LoggerAdapter],
logger: typedefs.Logger,
) -> AsyncIterator[Any]:
response = await request(
method='get',
Expand Down
6 changes: 3 additions & 3 deletions kopf/_cogs/clients/creating.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import logging
from typing import Optional, Union, cast
from typing import Optional, cast

from kopf._cogs.clients import api
from kopf._cogs.configs import configuration
from kopf._cogs.helpers import typedefs
from kopf._cogs.structs import bodies, references


Expand All @@ -13,7 +13,7 @@ async def create_obj(
namespace: references.Namespace = None,
name: Optional[str] = None,
body: Optional[bodies.RawBody] = None,
logger: Union[logging.Logger, logging.LoggerAdapter],
logger: typedefs.Logger,
) -> Optional[bodies.RawBody]:
"""
Create a resource.
Expand Down
5 changes: 2 additions & 3 deletions kopf/_cogs/clients/events.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import copy
import datetime
import logging
from typing import Union

import aiohttp

from kopf._cogs.clients import api, errors
from kopf._cogs.configs import configuration
from kopf._cogs.helpers import typedefs
from kopf._cogs.structs import bodies, references

MAX_MESSAGE_LENGTH = 1024
Expand All @@ -21,7 +20,7 @@ async def post_event(
message: str = '',
resource: references.Resource,
settings: configuration.OperatorSettings,
logger: Union[logging.Logger, logging.LoggerAdapter],
logger: typedefs.Logger,
) -> None:
"""
Issue an event for the object.
Expand Down
6 changes: 3 additions & 3 deletions kopf/_cogs/clients/fetching.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import logging
from typing import Collection, List, Tuple, Union
from typing import Collection, List, Tuple

from kopf._cogs.clients import api
from kopf._cogs.configs import configuration
from kopf._cogs.helpers import typedefs
from kopf._cogs.structs import bodies, references


Expand All @@ -11,7 +11,7 @@ async def list_objs(
settings: configuration.OperatorSettings,
resource: references.Resource,
namespace: references.Namespace,
logger: Union[logging.Logger, logging.LoggerAdapter],
logger: typedefs.Logger,
) -> Tuple[Collection[bodies.RawBody], str]:
"""
List the objects of specific resource type.
Expand Down
6 changes: 3 additions & 3 deletions kopf/_cogs/clients/patching.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import logging
from typing import Optional, Union
from typing import Optional

from kopf._cogs.clients import api, errors
from kopf._cogs.configs import configuration
from kopf._cogs.helpers import typedefs
from kopf._cogs.structs import bodies, patches, references


Expand All @@ -13,7 +13,7 @@ async def patch_obj(
namespace: references.Namespace,
name: Optional[str],
patch: patches.Patch,
logger: Union[logging.Logger, logging.LoggerAdapter],
logger: typedefs.Logger,
) -> Optional[bodies.RawBody]:
"""
Patch a resource of specific kind.
Expand Down
14 changes: 7 additions & 7 deletions kopf/_cogs/clients/scanning.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import asyncio
import logging
from typing import Collection, Mapping, Optional, Set, Union
from typing import Collection, Mapping, Optional, Set

from kopf._cogs.clients import api, errors
from kopf._cogs.configs import configuration
from kopf._cogs.helpers import typedefs
from kopf._cogs.structs import references


async def read_version(
*,
settings: configuration.OperatorSettings,
logger: Union[logging.Logger, logging.LoggerAdapter],
logger: typedefs.Logger,
) -> Mapping[str, str]:
rsp: Mapping[str, str] = await api.get('/version', settings=settings, logger=logger)
return rsp
Expand All @@ -19,7 +19,7 @@ async def read_version(
async def scan_resources(
*,
settings: configuration.OperatorSettings,
logger: Union[logging.Logger, logging.LoggerAdapter],
logger: typedefs.Logger,
groups: Optional[Collection[str]] = None,
) -> Collection[references.Resource]:
coros = {
Expand All @@ -35,7 +35,7 @@ async def scan_resources(
async def _read_old_api(
*,
settings: configuration.OperatorSettings,
logger: Union[logging.Logger, logging.LoggerAdapter],
logger: typedefs.Logger,
groups: Optional[Collection[str]],
) -> Collection[references.Resource]:
resources: Set[references.Resource] = set()
Expand All @@ -60,7 +60,7 @@ async def _read_old_api(
async def _read_new_apis(
*,
settings: configuration.OperatorSettings,
logger: Union[logging.Logger, logging.LoggerAdapter],
logger: typedefs.Logger,
groups: Optional[Collection[str]],
) -> Collection[references.Resource]:
resources: Set[references.Resource] = set()
Expand Down Expand Up @@ -91,7 +91,7 @@ async def _read_version(
version: str,
preferred: bool,
settings: configuration.OperatorSettings,
logger: Union[logging.Logger, logging.LoggerAdapter],
logger: typedefs.Logger,
) -> Collection[references.Resource]:
try:
rsp = await api.get(url, settings=settings, logger=logger)
Expand Down
4 changes: 1 addition & 3 deletions kopf/_cogs/configs/conventions.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,7 @@ def __init__(
v1: bool,
**kwargs: Any,
) -> None:
# TODO: Remove type-ignore when this is fixed: https://github.com/python/mypy/issues/5887
# Too many arguments for "__init__" of "object" -- but this is typical for mixins!
super().__init__(*args, **kwargs) # type: ignore
super().__init__(*args, **kwargs)
self.prefix = prefix
self.v1 = v1

Expand Down
22 changes: 22 additions & 0 deletions kopf/_cogs/helpers/typedefs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""
Rudimentary type [re-]definitions for cross-versioned Python & mypy.

The problem is that new mypy versions often bring type-sheds with StdLib types
defined as generics, while the old Python runtime (down to 3.7)
does not support the usual syntax.
Examples: asyncio.Task, asyncio.Future, logging.LoggerAdapter, and others.

This modules defines them in a most suitable and reusable way. Plus it adds
some common plain type definitions used across the codebase (for convenience).
"""
import logging
from typing import TYPE_CHECKING, Any, Union

if TYPE_CHECKING:
LoggerAdapter = logging.LoggerAdapter[Any]
else:
LoggerAdapter = logging.LoggerAdapter

# As publicly exposed: we only promise that it is based on one of the built-in loggable classes.
# Mind that these classes have multi-versioned stubs, so we avoid redefining the protocol ourselves.
Logger = Union[logging.Logger, LoggerAdapter]
2 changes: 1 addition & 1 deletion kopf/_cogs/structs/dicts.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
FieldSpec = Union[None, str, FieldPath, List[str]]

_T = TypeVar('_T')
_K = TypeVar('_K')
_K = TypeVar('_K', bound=str) # int & bool keys are possible but discouraged
_V = TypeVar('_V')


Expand Down
6 changes: 3 additions & 3 deletions kopf/_core/actions/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
"""
import asyncio
import datetime
import logging
from typing import Collection, Optional, Union
from typing import Collection, Optional

from kopf._cogs.aiokits import aiotime
from kopf._cogs.clients import patching
from kopf._cogs.configs import configuration
from kopf._cogs.helpers import typedefs
from kopf._cogs.structs import bodies, dicts, diffs, patches, references
from kopf._core.actions import loggers

Expand Down Expand Up @@ -110,7 +110,7 @@ async def patch_and_check(
resource: references.Resource,
body: bodies.Body,
patch: patches.Patch,
logger: Union[logging.Logger, logging.LoggerAdapter],
logger: typedefs.Logger,
) -> None:
"""
Apply a patch and verify that it is applied correctly.
Expand Down
10 changes: 3 additions & 7 deletions kopf/_core/actions/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
import dataclasses
import datetime
import enum
import logging
from contextvars import ContextVar
from typing import Any, AsyncContextManager, AsyncIterator, Callable, Collection, Iterable, \
Mapping, MutableMapping, NewType, Optional, Sequence, Set, TypeVar, Union
Mapping, MutableMapping, NewType, Optional, Sequence, Set, TypeVar

from typing_extensions import Protocol

from kopf._cogs.configs import configuration
from kopf._cogs.helpers import typedefs
from kopf._cogs.structs import ids
from kopf._core.actions import invocation

Expand Down Expand Up @@ -61,10 +61,6 @@ class ErrorsMode(enum.Enum):
PERMANENT = enum.auto()


# As publicly exposed: we only promise that it is based on one of the built-in loggable classes.
# Mind that these classes have multi-versioned stubs, so we avoid redefining the protocol ourselves.
Logger = Union[logging.Logger, logging.LoggerAdapter]

# A specialised type to highlight the purpose or origin of the data of type Any,
# to not be mixed with other arbitrary Any values, where it is indeed "any".
Result = NewType('Result', object)
Expand Down Expand Up @@ -137,7 +133,7 @@ class State(Mapping[ids.HandlerId, HandlerState]):
@dataclasses.dataclass
class Cause(invocation.Kwargable):
""" Base non-specific cause as used in the framework's reactor. """
logger: Logger
logger: typedefs.Logger

@property
def _kwargs(self) -> Mapping[str, Any]:
Expand Down
7 changes: 3 additions & 4 deletions kopf/_core/actions/invocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@
import contextlib
import contextvars
import functools
from typing import Any, Callable, Coroutine, Iterable, Iterator, List, \
Mapping, Optional, Tuple, TypeVar, Union, cast
from typing import Any, Callable, Coroutine, Iterable, Iterator, \
List, Mapping, Optional, Tuple, TypeVar, Union

from typing_extensions import final

from kopf._cogs.aiokits import aiotasks
from kopf._cogs.configs import configuration

# An internal typing hack shows that the handler can be sync fn with the result,
Expand Down Expand Up @@ -133,7 +132,7 @@ async def invoke(
# Note: the docs say the result is a future, but typesheds say it is a coroutine => cast()!
loop = asyncio.get_running_loop()
executor = settings.execution.executor if settings is not None else None
future = cast(aiotasks.Future, loop.run_in_executor(executor, real_fn))
future = loop.run_in_executor(executor, real_fn)
cancellation: Optional[asyncio.CancelledError] = None
while not future.done():
try:
Expand Down
Loading