Skip to content

Response.headers.get does not type check with pyrights strict checking mode #288

@Unigurd

Description

@Unigurd

Running pyright with strict checking will result in type errors in the reproduction script below because it cannot infer the return type of .get. I believe it's because CaseInsensitiveDict doesn't override .get and add type information like it does with the methods used for [] indexing.

Expected Result

I expect f2 to type check just like f1 does.

Actual Result

f2 fails to type check with the error message

/path/to/script.py
  /path/to/script.py:9:12 - error: Type of "get" is partially unknown
    Type of "get" is "Overload[(key: Unknown, /) -> (Unknown | None), (key: Unknown, /, default: Unknown) -> Unknown, (key: Unknown, /, default: _T@get) -> (Unknown | _T@get)]" (reportUnknownMemberType)
  /path/to/script.py:9:12 - error: Return type is unknown (reportUnknownVariableType)
2 errors, 0 warnings, 0 informations

Reproduction Steps

Install pyright 1.1.405, the latest as of this writing, and configure it to check strictly, e.g. with

[tool.pyright]
typeCheckingMode = "strict"

in your pyproject.toml.

Now running pyright on the following short script will result in the type checking errors.

import niquests

# Will pass pyright type check
def f1(resp: niquests.Response) -> str | bytes:
    return resp.headers["Content-Type"]

# Will fail pyright type check
def f2(resp: niquests.Response) -> str | bytes:
    return resp.headers.get("Content-Type", "")

System Information

$ python -m niquests.help
{
  "charset_normalizer": {
    "version": "3.4.0"
  },
  "gil": true,
  "http1": {
    "h11": "0.16.0"
  },
  "http2": {
    "jh2": "5.0.9"
  },
  "http3": {
    "enabled": true,
    "qh3": "1.5.4"
  },
  "idna": {
    "version": "3.10"
  },
  "implementation": {
    "name": "CPython",
    "version": "3.12.3"
  },
  "niquests": {
    "version": "3.15.2"
  },
  "ocsp": {
    "enabled": true
  },
  "platform": {
    "release": "6.6.87.2-microsoft-standard-WSL2",
    "system": "Linux"
  },
  "system_ssl": {
    "name": "OpenSSL 3.0.13 30 Jan 2024",
    "version": "300000d0"
  },
  "urllib3.future": {
    "cohabitation_version": null,
    "version": "2.13.906"
  },
  "wassima": {
    "version": "2.0.1"
  },
  "websocket": {
    "enabled": false,
    "wsproto": null
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    typingstatic typing related (mypy or similar)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions