pytest-dev / pytest-dev/pytest

The old co_filename is used when test files/folders are moved

Open
#14,552 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

Problem:

I use co_filename of code objects to find the sourcefile of the calling function in inline-snapshot and have some issues where the file (reported by co_filename) does not exist.

It appears that it has been moved for differend reasons (CI artefacts, docker).

The problem is that the old file location is still stored inside the .pyc file which is generated by pytest.

Repro:

mkdir -p test1

cat > test1/test_a.py << EOF
from inspect import currentframe, getfile

def test_a():
    assert getfile(test_a)==__file__
    assert currentframe().f_code.co_filename == __file__
EOF


echo first run
uvx pytest@9.0.3 -s test1/test_a.py

mv test1 test2

echo
echo second run
uvx pytest@9.0.3 -s test2/test_a.py

rm -r test2

output:

first run
========================================== test session starts ===========================================
platform linux -- Python 3.14.3, pytest-9.0.3, pluggy-1.6.0
rootdir: /home/frank/projects/tmp/repro_bytecode_move
collected 1 item                                                                                         

test1/test_a.py .

=========================================== 1 passed in 0.01s ============================================

second run
========================================== test session starts ===========================================
platform linux -- Python 3.14.3, pytest-9.0.3, pluggy-1.6.0
rootdir: /home/frank/projects/tmp/repro_bytecode_move
collected 1 item                                                                                         

test2/test_a.py F

================================================ FAILURES ================================================
_________________________________________________ test_a _________________________________________________

>   ???
E   AssertionError: assert '/home/frank/...st1/test_a.py' == '/home/frank/...st2/test_a.py'
E     
E     Skipping 39 identical leading characters in diff, use -v to show
E     - _move/test2/test_a.py
E     ?           ^
E     + _move/test1/test_a.py
E     ?           ^

/home/frank/projects/tmp/repro_bytecode_move/test1/test_a.py:4: AssertionError
======================================== short test summary info =========================================
FAILED test2/test_a.py::test_a - AssertionError: assert '/home/frank/...st1/test_a.py' == '/home/frank/...
st2/test_a.py'
=========================================== 1 failed in 0.04s ============================================

I have already created an PR #14551

pytest: 9.0.3
os: linux

  • a detailed description of the bug or problem you are having
  • output of pip list from the virtual environment you are using
  • pytest and operating system versions
  • minimal example if possible

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No repository file or test path is named. Start by running the provided move-and-rerun reproduction with pytest 9.0.3, then trace the pytest path that generates or loads the .pyc file. Done means the second run reports the moved test path in co_filename and the reproduction passes.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.