AskReference
ConceptIntermediate

What are the four problems with getting dependencies directly inside your web functions, as listed in the chapter?

The four problems are testing, hidden dependencies, code duplication, and OpenAPI visibility.

Getting dependencies directly inside web functions creates four consequences. Testing is harder because you cannot test variations of the function that look up the dependency differently. Hidden dependencies arise because details of how the function gets what it needs are hidden, so external code changes could break it. Code duplication occurs when a common dependency lookup is repeated in multiple functions. OpenAPI visibility is reduced because FastAPI's automatic test page needs information from the dependency injection mechanism.

Key points

  • Testing: you cannot test variations of the function that look up the dependency differently.
  • Hidden dependencies: external code changes could break code your function needs.
  • Code duplication: common lookups may be duplicated in multiple functions.
  • OpenAPI visibility: FastAPI's automatic test page needs dependency injection information.
Source:FastAPI: Modern Python Web Development· Dependencies· p. 95–100

Related questions

Cover of FastAPI: Modern Python Web Development

FastAPI: Modern Python Web Development

Bill Lubanovic;

First Edition · O'Reilly Media, Inc.

View this ebook