Skip to content

Fix type annotations on MultipartWriter.append#8211

Merged
Dreamsorcerer merged 8 commits intoaio-libs:masterfrom
cakemanny:fix-types-multipart-writer-append
Mar 8, 2024
Merged

Fix type annotations on MultipartWriter.append#8211
Dreamsorcerer merged 8 commits intoaio-libs:masterfrom
cakemanny:fix-types-multipart-writer-append

Conversation

@cakemanny
Copy link
Copy Markdown
Contributor

@cakemanny cakemanny commented Mar 7, 2024

What do these changes do?

They adjust the annotations on the MultipartWriter.append method (and two similar methods), so that type-checkers admit plain python dictionaries for the second argument; the headers parameter.

I chose to use LooseHeaders as I knew it was already used as the headers parameter for the web.Response constructor, among others. Changed to Mapping[str, str] , thank you Dreamsorcerer

Are there changes in behavior for the user?

No, this doesn't change any behaviour.

Is it a substantial burden for the maintainers to support this?

Not that I can imagine.

Related issue number

Fixes #7741

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes
    • Documentation already gives examples
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt
    • The format is <Name> <Surname>.
    • Please keep alphabetical order, the file is sorted by names.
  • Add a new news fragment into the CHANGES/ folder
    • name it <issue_or_pr_num>.<type>.rst (e.g. 588.bugfix.rst)

    • if you don't have an issue number, change it to the pull request
      number after creating the PR

      • .bugfix: A bug fix for something the maintainers deemed an
        improper undesired behavior that got corrected to match
        pre-agreed expectations.
      • .feature: A new behavior, public APIs. That sort of stuff.
      • .deprecation: A declaration of future API removals and breaking
        changes in behavior.
      • .breaking: When something public is removed in a breaking way.
        Could be deprecated in an earlier release.
      • .doc: Notable updates to the documentation structure or build
        process.
      • .packaging: Notes for downstreams about unobvious side effects
        and tooling. Changes in the test invocation considerations and
        runtime assumptions.
      • .contrib: Stuff that affects the contributor experience. e.g.
        Running tests, building the docs, setting up the development
        environment.
      • .misc: Changes that are hard to assign to any of the above
        categories.
    • Make sure to use full sentences with correct case and punctuation,
      for example:

      Fixed issue with non-ascii contents in doctest text files
      -- by :user:`contributor-gh-handle`.

      Use the past tense or the present tense a non-imperative mood,
      referring to what's changed compared to the last released version
      of this project.

@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Mar 7, 2024
Comment thread CHANGES/7741.bugfix.rst Outdated
@Dreamsorcerer
Copy link
Copy Markdown
Member

Looks reasonable to me. Though I feel like LooseHeaders itself seems redundant. I'd have thought Mapping[str, str] would accept the same things (i.e. everything else in the union is redundant)...

@cakemanny
Copy link
Copy Markdown
Contributor Author

cakemanny commented Mar 7, 2024

Looks reasonable to me. Though I feel like LooseHeaders itself seems redundant. I'd have thought Mapping[str, str] would accept the same things (i.e. everything else in the union is redundant)...

Seems you are right! I had been a bit unsure about the MultiDict and the case insensitive istr.

I checked by type checking this, with the annotation updated to Mapping[str, str] and all seems healthy:

from multidict import CIMultiDict
import aiohttp
from aiohttp.hdrs import CONTENT_TYPE


def type_check_append_with_headers() -> None:
    writer = aiohttp.MultipartWriter()
    writer.append("hello, world!", {"X-Foo": "bar"})
    writer.append("hello, world!", {CONTENT_TYPE: "text/plain"})

    multidict: CIMultiDict[str] = CIMultiDict()
    multidict.add("x-foo", "bar")
    writer.append("hello, world!", multidict)

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 7, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.53%. Comparing base (0e91eb0) to head (313f476).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #8211   +/-   ##
=======================================
  Coverage   97.53%   97.53%           
=======================================
  Files         107      107           
  Lines       32843    32843           
  Branches     3851     3851           
=======================================
  Hits        32032    32032           
  Misses        610      610           
  Partials      201      201           
Flag Coverage Δ
CI-GHA 97.44% <100.00%> (ø)
OS-Linux 97.11% <100.00%> (ø)
OS-Windows 95.62% <100.00%> (ø)
OS-macOS 96.93% <100.00%> (ø)
Py-3.10.11 95.54% <100.00%> (ø)
Py-3.10.13 96.92% <100.00%> (ø)
Py-3.11.8 96.59% <100.00%> (ø)
Py-3.12.2 96.68% <100.00%> (-0.03%) ⬇️
Py-3.8.10 95.52% <100.00%> (ø)
Py-3.8.18 96.85% <100.00%> (ø)
Py-3.9.13 95.51% <100.00%> (ø)
Py-3.9.18 96.88% <100.00%> (-0.01%) ⬇️
Py-pypy7.3.15 96.42% <100.00%> (ø)
VM-macos 96.93% <100.00%> (ø)
VM-ubuntu 97.11% <100.00%> (ø)
VM-windows 95.62% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@cakemanny
Copy link
Copy Markdown
Contributor Author

With regards to the three failing tests, I see that other recently merged PRs had test_unsupported_upgrade failing on those three platforms. So am I right in thinking that this one is good to go?

@Dreamsorcerer Dreamsorcerer merged commit 7725f5a into aio-libs:master Mar 8, 2024
@patchback
Copy link
Copy Markdown
Contributor

patchback bot commented Mar 8, 2024

Backport to 3.9: 💚 backport PR created

✅ Backport PR branch: patchback/backports/3.9/7725f5a22f4ca64dfb01478d640763910b036192/pr-8211

Backported as #8214

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

@patchback
Copy link
Copy Markdown
Contributor

patchback bot commented Mar 8, 2024

Backport to 3.10: 💚 backport PR created

✅ Backport PR branch: patchback/backports/3.10/7725f5a22f4ca64dfb01478d640763910b036192/pr-8211

Backported as #8215

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

patchback bot pushed a commit that referenced this pull request Mar 8, 2024
Dreamsorcerer pushed a commit that referenced this pull request Mar 9, 2024
…iter.append (#8214)

**This is a backport of PR #8211 as merged into master
(7725f5a).**

Co-authored-by: Daniel Golding <[email protected]>
Dreamsorcerer pushed a commit that referenced this pull request Mar 10, 2024
…riter.append (#8215)

**This is a backport of PR #8211 as merged into master
(7725f5a).**

Co-authored-by: Daniel Golding <[email protected]>
@cakemanny cakemanny deleted the fix-types-multipart-writer-append branch March 10, 2024 20:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided There is a change note present in this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Invalid type definition for mpwriter.append

2 participants