Repository navigation
Adding directory to PymodeRope #1159
Description
Using the python project scheme suggested by the pytest documentation, pymode does not include the tests/ directory into its ropeproject when the “include method” of the src/ directory is done via an editable install (pip install -e .).
How do I add an additional source directory to PymodeRope, namely the tests/ directory?
Minimal working example
First create a venv virtual environment as well as a temporary directory. The abbreviation pmt stands for “pymode test”.
$ mkdir -p /tmp/pmt/{.venv,src/pmt,tests}
$ cd /tmp/pmt
$ python -m venv /tmp/pmt/.venv/pmt
$ source /tmp/pmt/.venv/pmt/bin/activate
(pmt) $ python -m pip install -U pip
(pmt) $ python -m pip install build pytest
(pmt) $ touch pyproject.toml src/pmt/__init__.pyCreate the three source files with an editor of your choice, namely
-
# ––– file /tmp/pmt/src/pmt/spam.py def spamfunc() -> int: return 1
-
# ––– file /tmp/pmt/src/pmt/bacon.py from .spam import spamfunc def baconfunc() -> None: return 1 + spamfunc()
-
# ––– file /tmp/pmt/tests/test_pmt.py from pmt.bacon import baconfunc from pmt.spam import spamfunc def test_baconfunc() -> None: assert 2 == baconfunc() def test_spamfunc() -> None: assert 1 == spamfunc()
The project directory will look like this:
.
├── pyproject.toml
├── src
│ └── pmt
│ ├── bacon.py
│ ├── __init__.py
│ └── spam.py
└── tests
└── test_pmt.py
As suggested by pytest, install the editable version of the package and invoke pytest:
(pmt) $ python -m pip install -e .
(pmt) $ python -m pytest
================================ test session starts =================================
platform linux -- Python 3.10.6, pytest-7.1.3, pluggy-1.0.0
rootdir: /tmp/pmt
collected 2 items
tests/test_pmt.py .. [100%]
================================= 2 passed in 0.01s ==================================
To finally reproduce the behaviour:
- open the source file with
vim /tmp/pmt/src/pmt/spam.py - move the cursor to
spamfunc - start function renaming by hitting
<C-c>rr - insert a different name, for instance
hamfuncand press<Enter> - perform the renaming by inserting
1<Enter>
After that, the renaming was successfully done in /tmp/pmt/src/pmt/spam.py and /tmp/pmt/src/pmt/bacon.py but not in tmp/pmt/tests/test_pmt.py (as I expect).
What I have tried
I tried to add manually the tests directory to the .ropeproject/config.py file
def set_prefs(prefs):
# … more code of the default config.py file …
# Custom source folders: By default rope searches the project
# for finding source folders (folders that should be searched
# for finding modules). You can add paths to that list. Note
# that rope guesses project source folders correctly most of the
# time; use this if you have any problems.
# The folders should be relative to project root and use '/' for
# separating folders regardless of the platform rope is running on.
# 'src/my_source_folder' for instance.
prefs.add('source_folders', 'tests')
# … more code of the default config.py file …with no success.
When I change the import policy by
- uninstalling the module again with
(pmt) $ python -m pip uninstall pmt - change the imports of
tmp/pmt/tests/test_pmt.pytofrom src.pmt.bacon import baconfunc from src.pmt.spam import spamfunc
then the renaming as suggested above does work, but that project structure is not as I or the pytest guys, respectively, want it to be.
Used versions
(pmt) $ python --version
Python 3.10.6
(pmt) $ python -m pip freeze | grep 'build\|pytest'
build==0.8.0
pytest==7.1.3Pymode version: commit 85d2cd1a196ac1def79d4599b5be69395ed5b603 (HEAD → develop)
Activity