Skip to content

Invalid type definition for mpwriter.append #7741

Closed
@JP-Ellis

Description

Describe the bug

It would appear that the append method of the MultiPartWriter has an incorrect type definition:

def append(self, obj: Any, headers: Optional[MultiMapping[str]] = None) -> Payload:

Resulting in type checking tools to incorrectly errpr what (I believe) should be correct.

To Reproduce

The following test file generates the error in type checkers.

import aiohttp

with aiohttp.MultipartWriter() as mpwriter:
    mpwriter.append("Hello, World", {"Content-Type": "text/plain"})

which generates the following errors:

mypy test.py
test.py:4: error: Argument 2 to "append" of "MultipartWriter" has incompatible type "dict[str, str]"; expected "MultiMapping[str] | None"  [arg-type]
Found 1 error in 1 file (checked 1 source file)pyright test.py
/Users/joshua.ellis/src/pact-foundation/pact-python/tests/v3/test.py
  /Users/joshua.ellis/src/pact-foundation/pact-python/tests/v3/test.py:4:37 - error: Argument of type "dict[str, str]" cannot be assigned to parameter "headers" of type "MultiMapping[str] | None" in function "append"
    Type "dict[str, str]" cannot be assigned to type "MultiMapping[str] | None"
      "dict[str, str]" is incompatible with "MultiMapping[str]"
      Type cannot be assigned to type "None" (reportGeneralTypeIssues)
1 error, 0 warnings, 0 informations

Expected behavior

I expected that a regular Python dictionary be compatible with the header argument.

Logs/tracebacks

_see above_

Python Version

python --version
Python 3.11.6

aiohttp Version

python -m pip show aiohttp
Name: aiohttp
Version: 3.8.6
Summary: Async http client/server framework (asyncio)
Home-page: https://github.com/aio-libs/aiohttp
Author: 
Author-email: 
License: Apache 2
Requires: aiosignal, async-timeout, attrs, charset-normalizer, frozenlist, multidict, yarl
Required-by:

multidict Version

python -m pip show multidict
Name: multidict
Version: 6.0.4
Summary: multidict implementation
Home-page: https://github.com/aio-libs/multidict
Author: Andrew Svetlov
Author-email: andrew.svetlov@gmail.com
License: Apache 2
Requires: 
Required-by: aiohttp, yarl

yarl Version

python -m pip show yarl
Name: yarl
Version: 1.9.2
Summary: Yet another URL library
Home-page: https://github.com/aio-libs/yarl/
Author: Andrew Svetlov
Author-email: andrew.svetlov@gmail.com
License: Apache-2.0
Requires: idna, multidict
Required-by: aiohttp

OS

uname -mprs
Darwin 22.6.0 arm64 arm

Related component

Client

Additional context

No response

Code of Conduct

  • I agree to follow the aio-libs Code of Conduct

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions