Skip to content

Add typed get() for RequestKey and ResponseKey - #14573

Open
hyzyla wants to merge 2 commits into
aio-libs:masterfrom
hyzyla:typed-request-response-get
Open

hyzyla wants to merge 2 commits into
aio-libs:masterfrom
hyzyla:typed-request-response-get

Conversation

@hyzyla

@hyzyla hyzyla commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

What do these changes do?

#11766 added RequestKey and ResponseKey and overloaded __getitem__ and __setitem__ on BaseRequest and StreamResponse. It did not overload get(), so request.get(key) and response.get(key) fall back to MutableMapping.get() and return Any, even for a typed key:

user_key = web.RequestKey("user", User)

request[user_key]      # User
request.get(user_key)  # Any

This PR adds the same get() overloads that Application.get() already has for AppKey:

@overload  # type: ignore[override]
def get(self, key: RequestKey[_T], default: None = ...) -> _T | None: ...
@overload
def get(self, key: RequestKey[_T], default: _U) -> _T | _U: ...
@overload
def get(self, key: str, default: Any = ...) -> Any: ...

StreamResponse.get() gets the same overloads with ResponseKey.

Are there changes in behavior for the user?

No runtime change: get() returns self._state.get(key, default), which gives the same result as the inherited MutableMapping.get(). Type checkers now infer T | None (or T | U with a default) for a typed key. String keys still give Any.

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

No. It copies the existing Application.get() overloads. The existing test_request_get_requestkey and test_response_get_responsekey tests now also have assert_type() checks, so mypy fails if the types regress (6 assert-type errors on master, none with this change).

Related issue number

Follow-up to #11766 (issue #11762).

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes
  • If you provide code modifications, please add yourself to CONTRIBUTORS.txt
  • Add a new news fragment into the CHANGES/ folder

@agentscanapp

agentscanapp Bot commented Dec 3, 2026

Copy link
Copy Markdown

Mixed activity

Activity patterns show a mix of organic and automated signals.

View full analysis →

Evidence
  • AI agent branches: 7/26 branches use AI agent tool prefixes (codex/)

This is an automated analysis by AgentScan

@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided There is a change note present in this PR label Dec 3, 2026
@greptile-apps

greptile-apps Bot commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

security Security Review

This PR appears safe to merge from a security standpoint.

What was checked
  • The PR only adds typed get() overloads to BaseRequest and StreamResponse; it touches no auth, parsing, or access-control code.
  • The new get() calls self._state.get(key, default), which is behaviorally identical to the inherited MutableMapping.get() since getitem is a plain dict lookup that only raises KeyError.
  • Keys in this state mapping come from application code (RequestKey/ResponseKey), never from attacker-controlled HTTP input, so no untrusted data reaches this code.
  • Library security checks (WebSocket handshake, Expect header handling) use headers.get() on CIMultiDict, not the request/response state mapping changed here.
  • The pre-review scan reported zero findings and no hotspots.

@codecov

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 68.75000% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 48.23%. Comparing base (b5acd43) to head (92a9e82).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
tests/test_web_request.py 20.00% 4 Missing ⚠️
aiohttp/web_request.py 66.66% 1 Missing ⚠️

❗ There is a different number of reports uploaded between BASE (b5acd43) and HEAD (92a9e82). Click for more details.

HEAD has 99 uploads less than BASE
Flag BASE (b5acd43) HEAD (92a9e82)
cython-coverage 4 2
OS-Windows 10 0
Py-3.15 4 0
VM-windows-latest 10 0
CI-GHA 24 0
Py-3.15t 2 0
VM-ubuntu-latest 4 0
OS-Linux 4 0
Py-3.12 4 0
Py-3.13 4 0
Py-3.11 4 0
Py-3.14 4 0
VM-macos-latest 10 0
OS-macOS 10 0
Autobahn 2 1
Py-pypy-3.12 2 0
Additional details and impacted files
@@             Coverage Diff             @@
##           master   #14573       +/-   ##
===========================================
- Coverage   99.13%   48.23%   -50.91%     
===========================================
  Files         135      135               
  Lines       54630    54622        -8     
  Branches     2808     2808               
===========================================
- Hits        54158    26346    -27812     
- Misses        357    27637    +27280     
- Partials      115      639      +524     
Flag Coverage Δ
Autobahn 21.53% <37.50%> (+<0.01%) ⬆️
CI-GHA ?
OS-Linux ?
OS-Windows ?
OS-macOS ?
Py-3.11 ?
Py-3.12 ?
Py-3.13 ?
Py-3.14 ?
Py-3.15 ?
Py-3.15t ?
Py-pypy-3.12 ?
VM-macos-latest ?
VM-ubuntu-latest ?
VM-windows-latest ?
cython-coverage 84.04% <90.90%> (+<0.01%) ⬆️

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

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

@greptile-apps

greptile-apps Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 4/5 Tier: apexRetriggerView in Greptile

[Low impact] The change reaches type checking for request and response lookups using typed keys.

The required API documentation update must be added before merging.

Reviews (1) · Last reviewed commit: "Use the PR number for the change note" · Reviewed by Greptile

Comment thread aiohttp/web_request.py Resolved
@codspeed

codspeed Bot commented Dec 3, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 49 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 102 untouched benchmarks
⏩ 83 skipped benchmarks1


Comparing hyzyla:typed-request-response-get (92a9e82) with master (b5acd43)

Open in CodSpeed

Footnotes

  1. 83 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agentscan:mixed-signals 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.

1 participant