Skip to content

Statements incorrectly reported as missing with FastAPI and async SQLAlchemy #1240

Closed
@rfleschenberg

Description

@rfleschenberg

Describe the bug

When using coverage in a FastAPI project that uses SQLAlchemy and asyncpg, parts of the code are wrongly reported as not covered. I know that the affected code statements do run, because otherwise the tests would fail. I also checked this by setting debugger breakpoints.

My tests so far indicate that this only happens if the test goes through a FastAPI router function. If I instead call the affected function directly, the coverage report is correct.

Unfortunately the most minimal MRE I was able to come up with involves Postgres and a bunch of dependencies. Sorry about that. On the other hand, I think my stack is not that exotic, so probably sooner or later others will also run into this issue.

To Reproduce
What version of Python are you using?

I tested with Python 3.7.3 and Python 3.9.6.

What version of coverage.py are you using? The output of coverage debug sys is helpful.

-- sys -------------------------------------------------------
               coverage_version: 6.0.2
                coverage_module: /home/rene/.virtualenvs/csa/lib/python3.7/site-packages/coverage/__init__.py
                         tracer: -none-
                        CTracer: available
           plugins.file_tracers: -none-
            plugins.configurers: -none-
      plugins.context_switchers: -none-
              configs_attempted: .coveragerc
                                 setup.cfg
                                 tox.ini
                                 pyproject.toml
                   configs_read: -none-
                    config_file: None
                config_contents: -none-
                      data_file: -none-
                         python: 3.7.3 (default, Jan 22 2021, 20:04:44) [GCC 8.3.0]
                       platform: Linux-4.19.0-18-amd64-x86_64-with-debian-10.11
                 implementation: CPython
                     executable: /home/rene/.virtualenvs/csa/bin/python3
                   def_encoding: utf-8
                    fs_encoding: utf-8
                            pid: 1704
                            cwd: /home/rene/projects/coverage-sqlalchemy-async
                           path: /home/rene/.virtualenvs/csa/bin
                                 /usr/lib/python37.zip
                                 /usr/lib/python3.7
                                 /usr/lib/python3.7/lib-dynload
                                 /home/rene/.virtualenvs/csa/lib/python3.7/site-packages
                    environment: HOME = /home/rene
                                 PYENV_ROOT = /home/rene/.pyenv
                                 PYENV_SHELL = zsh
                                 PYTHONDONTWRITEBYTECODE = 1
                                 PYTHONSTARTUP = /home/rene/.pystartup
                   command_line: /home/rene/.virtualenvs/csa/bin/coverage debug sys
                sqlite3_version: 2.6.0
         sqlite3_sqlite_version: 3.27.2
             sqlite3_temp_store: 0
        sqlite3_compile_options: COMPILER=gcc-8.3.0; ENABLE_COLUMN_METADATA; ENABLE_DBSTAT_VTAB
                                 ENABLE_FTS3; ENABLE_FTS3_PARENTHESIS; ENABLE_FTS3_TOKENIZER
                                 ENABLE_FTS4; ENABLE_FTS5; ENABLE_JSON1
                                 ENABLE_LOAD_EXTENSION; ENABLE_PREUPDATE_HOOK; ENABLE_RTREE
                                 ENABLE_SESSION; ENABLE_STMTVTAB; ENABLE_UNLOCK_NOTIFY
                                 ENABLE_UPDATE_DELETE_LIMIT; HAVE_ISNAN; LIKE_DOESNT_MATCH_BLOBS
                                 MAX_SCHEMA_RETRY=25; MAX_VARIABLE_NUMBER=250000; OMIT_LOOKASIDE
                                 SECURE_DELETE; SOUNDEX; TEMP_STORE=1
                                 THREADSAFE=1; USE_URI

What versions of what packages do you have installed? The output of pip freeze is helpful.

anyio==3.3.3
asgiref==3.4.1
asyncpg==0.24.0
attrs==21.2.0
certifi==2021.10.8
charset-normalizer==2.0.7
click==8.0.3
coverage==6.0.2
fastapi==0.70.0
flake8==4.0.1
greenlet==1.1.2
h11==0.12.0
httpcore==0.13.7
httpx==0.19.0
idna==3.2
importlib-metadata==4.2.0
iniconfig==1.1.1
mccabe==0.6.1
packaging==21.0
pkg-resources==0.0.0
pluggy==1.0.0
py==1.10.0
pycodestyle==2.8.0
pydantic==1.8.2
pyflakes==2.4.0
pyparsing==2.4.7
pytest==6.2.5
pytest-asyncio==0.15.1
rfc3986==1.5.0
sniffio==1.2.0
SQLAlchemy==1.4.25
starlette==0.16.0
toml==0.10.2
typing-extensions==3.10.0.2
uvicorn==0.15.0
zipp==3.6.0

What code are you running? Give us a specific commit of a specific repo that we can check out.

https://github.com/rfleschenberg/coverage-sqlalchemy-async/tree/ea17b8f9a3fddb1c62212803b6793aa4f9c63561

  1. What commands did you run?
./runtests.sh

Expected behavior

A correct coverage report ;)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingduplicateThis issue or pull request already exists

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions