question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Execution order of dependencies

See original GitHub issue

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google “How to X in FastAPI” and didn’t find any information.
  • I already read and followed all the tutorial in the docs and didn’t find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

import uvicorn as uvicorn
from fastapi import FastAPI, Depends

app = FastAPI()


def dep1():
    print('1')


def dep2():
    print('2')


@app.get('/test', dependencies=[Depends(dep1), Depends(dep2)])
def test():
    return 'test'


if __name__ == '__main__':
    uvicorn.run("main:app", host="127.0.0.1", port=8008, reload=True, debug=True)

Description

By running the above code the output is

1
2

Is the execution order of dependencies always the declared order? I haven’t found anything in the docs regarding the order of the dependencies.

Operating System

Linux

Operating System Details

No response

FastAPI Version

0.73.0

Python Version

3.10.6

Additional Context

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Soerboecommented, Aug 30, 2022

@imacat The order of the elements in a list is guaranteed by python. A list is an ordered collection. Please, check your statements before posting.

However, FastAPI could choose to reorder the Depends() in the list for some reason. I want to know whether FastAPI would do this or not.

1reaction
imacatcommented, Aug 30, 2022

I suppose the order is from left to right. That means, dep1, and then dep2. However, the order is not guaranteed. And the order is dealt with by the Python compiler, not FastAPI.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Example: Execution dependencies - IBM
Each order can have multiple dependencies as well as multiple dependent orders. The application stores each edge as an individual record identifying the ......
Read more >
Dependency and execution order in asynchronous operations
A dependent asynchronous operation does not execute until the operation that it is dependent with has finished executing. For example, if ...
Read more >
Gradle Task Dependencies and Ordering
Dependency is mandatory execution of both task, in the required order, so dependency implies order. Order does not imply mandatory execution, ...
Read more >
Find the ordering of tasks from given dependencies
Explanation: There are a total of 2 tasks to pick. To pick task 1 you should have finished task 0. So the correct...
Read more >
Establish execution order from dependencies - Scala Algorithms
Given a of dependencies (eg 'Task A depends on Task B', 'Task B depends on Task C'), establish the order in which the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found