Skip to content

Mypy complains about the type hint for quic_cache_layer.add_domain. #189

@nakoo

Description

@nakoo

Expected Result

Success: no issues found in 1 source file

Actual Result

test.py:15: error: Item "MutableMapping[tuple[str, int], tuple[str, int] | None]" of "MutableMapping[tuple[str, int], tuple[str, int] | None] | QuicSharedCache" has no attribute "add_domain"  [union-attr]
Found 1 error in 1 file (checked 1 source file)

Reproduction Steps

import asyncio
import time
import niquests
from urllib.parse import urlparse

async def request(url: str) -> niquests.Response | None:
    hostname = str(urlparse(url).hostname)
    response = None
    error_msg = None

    try:
        start = time.perf_counter()
        async with niquests.AsyncSession(happy_eyeballs=True) as session:
            async with asyncio.timeout(30):
                session.quic_cache_layer.add_domain(hostname)
                response = await session.get(url, timeout=5)
                response.raise_for_status()
    except niquests.RequestException as e:
        error_msg = f"Niquests Error: {e}"
    except TimeoutError:
        error_msg = f"Niquests were forcibly timed out"
    except Exception as e:
        error_msg = f"Unexpected Error: {e}"
    finally:
        end = time.perf_counter()
        if error_msg:
            print(f"{error_msg} | {round(end - start, 2)}")

    return response

async def test() -> None:
    url = "https://http3check.net/"
    response = await request(url)

    if not response:
        raise ValueError("No response")

    print(response.headers)

def main() -> None:
    asyncio.run(test())

if __name__ == "__main__":
    main()

System Information

$ python -m niquests.help
{
  "charset_normalizer": {
    "version": "3.4.0"
  },
  "http1": {
    "h11": "0.14.0"
  },
  "http2": {
    "jh2": "5.0.4"
  },
  "http3": {
    "enabled": true,
    "qh3": "1.2.1"
  },
  "idna": {
    "version": "3.10"
  },
  "implementation": {
    "name": "CPython",
    "version": "3.12.7"
  },
  "niquests": {
    "version": "3.11.3"
  },
  "ocsp": {
    "enabled": true
  },
  "platform": {
    "release": "6.6.64",
    "system": "Linux"
  },
  "system_ssl": {
    "name": "OpenSSL 3.3.2 3 Sep 2024",
    "version": "30300020"
  },
  "urllib3.future": {
    "cohabitation_version": null,
    "version": "2.12.903"
  },
  "wassima": {
    "certifi_fallback": false,
    "enabled": true,
    "version": "1.1.5"
  },
  "websocket": {
    "enabled": false,
    "wsproto": null
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions