Junior — Senior
Refactoring code according to clean code principles and test coverage
livecode
Task condition
It is necessary to bring the provided code fragment in line with clean code requirements:
- Add missing type annotations.
- Format docstrings for all classes and their methods.
- Improve readability (correct indentation, more meaningful variable names if necessary).
- Split the code into separate modules if needed.
- Implement unit tests that check key parts of the program.
from collections.abc import Callable
from typing import Any, TypeVar, Concatenate, overload
from contextlib import asynccontextmanager
from dataclasses import dataclass
from functools import partial
from psycopg import AsyncConnection
from psycopg.types import TypeInfo
from certmanager.storages import sql
T = TypeVar('T')
P = TypeVar('P')
BaseActionT = Callable[Concatenate[AsyncConnection[Any], P], T]
@dataclass(slots=True)
class Action:
action_function: BaseActionT[T, P]
@overload
def __get__(self, client: None, *args: Any) -> 'Action[T, P]': ...
@overload
def __get__(self, client: AsyncConnection[Any], *args: Any) -> Callable[P, T]: ...
def __get__(self, client: AsyncConnection[Any] | None, *args: Any) -> Any:
if client is None:
return self
return partial(self.action_function, client.connection)
class PositiveValue:
def get(self) -> int:
"""Returns a positive integer value."""
return 1 # example value
class MyClass:
def __init__(self) -> None:
self.number = PositiveValue()
obj = MyClass()
obj.number