Skip to content

pyright/pylance typing error with tenacity "Unknown | Awaitable[Unknown]" #532

@Mac0501

Description

@Mac0501

i get multiple warnings with typing is this a problem of the libery ?

import logging
from abc import ABC, abstractmethod
from datetime import timedelta
from typing import Any

import aiohttp
from aiobreaker import CircuitBreaker
from aiohttp import ClientError, ClientTimeout
from tenacity import (
    retry,
    retry_if_exception_type,
    stop_after_attempt,
    wait_exponential,
)

from bot.const import (
    CIRCUIT_FAILURE_THRESHOLD,
    CIRCUIT_RECOVERY_TIMEOUT,
    HTTP_TIMEOUT,
    RETRY_MAX_ATTEMPTS,
)

logger = logging.getLogger(__name__)

# Circuit Breaker konfigurieren
breaker = CircuitBreaker(
    fail_max=CIRCUIT_FAILURE_THRESHOLD,
    timeout_duration=timedelta(seconds=CIRCUIT_RECOVERY_TIMEOUT),
)


class BaseApiClient(ABC):
    def __init__(self, session: aiohttp.ClientSession):
        timeout = ClientTimeout(total=HTTP_TIMEOUT)
        self._session = session
        self._session._default_timeout = timeout

    @retry(
        stop=stop_after_attempt(RETRY_MAX_ATTEMPTS),
        wait=wait_exponential(multiplier=1, min=1, max=10),
        retry=retry_if_exception_type(ClientError),
        reraise=True,
    )
    @breaker
    async def _get(self, url: str, **kwargs: Any) -> dict[Any, Any]:
        """
        Führt einen GET-Request mit:
          - Timeout aus HTTP_TIMEOUT
          - automatischen Retries bei Netzwerkfehlern
          - Circuit Breaker bei too many failures
        """
        try:
            async with self._session.get(url, **kwargs) as resp:
                resp.raise_for_status()
                return await resp.json()
        except Exception:
            logger.exception("API GET failed for URL: %s", url)
            raise

    @abstractmethod
    async def close(self) -> None:
        """Optionales Cleanup, falls nötig."""
        ...
from typing import Any, Dict

from pydantic import BaseModel

from bot.const import WAIFU_API_BASE
from bot.helpers.integrations.base import BaseApiClient


class WaifuResponse(BaseModel):
    url: str


class WaifuApiClient(BaseApiClient):

    async def get_wave_gif(self) -> WaifuResponse:
        data: Dict[Any, Any] = await self._get(url=f"{WAIFU_API_BASE}/sfw/wave")
        return WaifuResponse.model_validate(data)

    async def close(self) -> None:
        # kein Cleanup nötig
        pass
Der Typ von "data" ist teilweise unbekannt.
  Der Typ von "data" ist "Unknown | Awaitable[Unknown]".PylancereportUnknownVariableType
(variable) data: Dict[Any, Any]

Der Typ „Unknown | Awaitable[Unknown]“ kann dem deklarierten Typ „Dict[Any, Any]“ nicht zugewiesen werden
  Der Typ „Unknown | Awaitable[Unknown]“ kann dem Typ „Dict[Any, Any]“ nicht zugewiesen werden.
    „Awaitable[Unknown]“ kann „Dict[Any, Any]“ nicht zugewiesen werden.PylancereportAssignmentType


Kein Parameter mit dem Namen "url"PylancereportCallIssue
(function) url: Unknown

error translated to english:

The type of "data" is partially unknown.
The type of "data" is "Unknown | Awaitable[Unknown]". PylancereportUnknownVariableType
(variable) data: Dict[Any, Any]

The type "Unknown | Awaitable[Unknown]" cannot be assigned to the declared type "Dict[Any, Any]".
The type "Unknown | Awaitable[Unknown]" cannot be assigned to the type "Dict[Any, Any]".
"Awaitable[Unknown]" cannot be assigned to "Dict[Any, Any]". PylancereportAssignmentType

No parameter named "url" PylancereportCallIssue
(function) url: Unknown

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions