codejump

Scopes, Cleanup & Testing — Series 3

Preview — 3 of 10 questions

A request-scoped yield dependency consumes another yield dependency. Why must that sub-dependency also be request-scoped in FastAPI 0.121.0+?

ARequest scope disables sub-dependencies
BThe consumer may need its provider during cleanup after the response
CFunction scope always runs before setup
DRequest scope turns the provider into a singleton

A dependency opens a file inside with open(path) as file: yield file. When does the file's context manager exit?

AAs soon as yield produces the file
BBefore the route begins
CWhen the yield dependency is resumed for teardown
DOnly when Python's garbage collector runs

A function-scoped yield dependency returns a file handle to a StreamingResponse iterator. What is the likely lifetime problem?

ACleanup can close the handle before streaming consumes it
BFunction scope keeps the handle open forever
CThe iterator runs during application import
DFastAPI duplicates the file for each chunk

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.