From 26c74673b1117f1bd6ba088391669d27858e3a91 Mon Sep 17 00:00:00 2001 From: Ben Mares Date: Sat, 20 Jul 2024 11:19:16 +0200 Subject: [PATCH 1/5] Add an __init__.py in repo root to prevent import confusion --- __init__.py | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 __init__.py diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000000..a053bba704 --- /dev/null +++ b/__init__.py @@ -0,0 +1,11 @@ +from pathlib import Path + + +repo_root = Path(__file__).parent + +raise RuntimeError( + f"Python is looking for PyTensor in {repo_root}, but it's " + f"actually located in {repo_root / 'pytensor'}. Probably " + f"you need to change your working directory from {Path.cwd()} " + f"to {repo_root}." +) From 5fc05170af0c97719ba5d188ffdc4664d8c439bc Mon Sep 17 00:00:00 2001 From: Ben Mares Date: Sat, 20 Jul 2024 16:39:34 +0200 Subject: [PATCH 2/5] Set pytest's import mode to importlib --- conftest.py | 6 ++++++ pyproject.toml | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/conftest.py b/conftest.py index 873da8446a..c99b54815f 100644 --- a/conftest.py +++ b/conftest.py @@ -1,8 +1,14 @@ import os +import sys +from pathlib import Path import pytest +repo_root = Path(__file__).parent +sys.path.insert(0, str(repo_root)) + + def pytest_sessionstart(session): os.environ["PYTENSOR_FLAGS"] = ",".join( [ diff --git a/pyproject.toml b/pyproject.toml index 81a1285da8..ee3810abec 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -117,7 +117,7 @@ versionfile_build = "pytensor/_version.py" tag_prefix = "rel-" [tool.pytest] -addopts = "--durations=50 --doctest-modules pytensor --ignore=pytensor/misc/check_duplicate_key.py --ignore=pytensor/link" +addopts = "--durations=50 --doctest-modules pytensor --import-mode=importlib --ignore=pytensor/misc/check_duplicate_key.py --ignore=pytensor/link" testpaths = "tests/" [tool.ruff] From ca94efe65df2cae932cbc41fbe765948d7435c9d Mon Sep 17 00:00:00 2001 From: Ben Mares Date: Sat, 20 Jul 2024 16:47:13 +0200 Subject: [PATCH 3/5] Set mypy explicit_package_bases to true --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index ee3810abec..119cca7a99 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -163,6 +163,7 @@ warn_return_any = true warn_unreachable = true files = ["pytensor", "tests"] plugins = ["numpy.typing.mypy_plugin"] +explicit_package_bases = true [tool.cibuildwheel] build = "*" From 9ae41df1abcc72b86f2bff331c5368323ab07273 Mon Sep 17 00:00:00 2001 From: Ben Mares Date: Sat, 20 Jul 2024 16:53:13 +0200 Subject: [PATCH 4/5] Use pytest CLI directly instead of via Python module This prevents the current directory from being added to the search path. --- .github/workflows/test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 674bc52c7b..a1d88b8f81 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -176,7 +176,7 @@ jobs: if [[ $FAST_COMPILE == "1" ]]; then export PYTENSOR_FLAGS=$PYTENSOR_FLAGS,mode=FAST_COMPILE; fi if [[ $FLOAT32 == "1" ]]; then export PYTENSOR_FLAGS=$PYTENSOR_FLAGS,floatX=float32; fi export PYTENSOR_FLAGS=$PYTENSOR_FLAGS,warn__ignore_bug_before=all,on_opt_error=raise,on_shape_error=raise,gcc__cxxflags=-pipe - python -m pytest -r A --verbose --runslow --durations=50 --cov=pytensor/ --cov-report=xml:coverage/coverage-${MATRIX_ID}.xml --no-cov-on-fail $PART --benchmark-skip + pytest -r A --verbose --runslow --durations=50 --cov=pytensor/ --cov-report=xml:coverage/coverage-${MATRIX_ID}.xml --no-cov-on-fail $PART --benchmark-skip env: MATRIX_ID: ${{ steps.matrix-id.outputs.id }} MKL_THREADING_LAYER: GNU @@ -231,7 +231,7 @@ jobs: shell: micromamba-shell {0} run: | export PYTENSOR_FLAGS=mode=FAST_COMPILE,warn__ignore_bug_before=all,on_opt_error=raise,on_shape_error=raise,gcc__cxxflags=-pipe - python -m pytest --runslow --benchmark-only --benchmark-json output.json + pytest --runslow --benchmark-only --benchmark-json output.json - name: Store benchmark result uses: benchmark-action/github-action-benchmark@v1 with: From 362b1b3fd6942420ba3e412875604ab2e7be96bc Mon Sep 17 00:00:00 2001 From: Ben Mares Date: Sat, 20 Jul 2024 17:47:13 +0200 Subject: [PATCH 5/5] Move --import-mode=importlib out of addopts --- .github/workflows/test.yml | 4 ++-- pyproject.toml | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a1d88b8f81..7be59df4af 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -176,7 +176,7 @@ jobs: if [[ $FAST_COMPILE == "1" ]]; then export PYTENSOR_FLAGS=$PYTENSOR_FLAGS,mode=FAST_COMPILE; fi if [[ $FLOAT32 == "1" ]]; then export PYTENSOR_FLAGS=$PYTENSOR_FLAGS,floatX=float32; fi export PYTENSOR_FLAGS=$PYTENSOR_FLAGS,warn__ignore_bug_before=all,on_opt_error=raise,on_shape_error=raise,gcc__cxxflags=-pipe - pytest -r A --verbose --runslow --durations=50 --cov=pytensor/ --cov-report=xml:coverage/coverage-${MATRIX_ID}.xml --no-cov-on-fail $PART --benchmark-skip + pytest --import-mode=importlib -r A --verbose --runslow --durations=50 --cov=pytensor/ --cov-report=xml:coverage/coverage-${MATRIX_ID}.xml --no-cov-on-fail $PART --benchmark-skip env: MATRIX_ID: ${{ steps.matrix-id.outputs.id }} MKL_THREADING_LAYER: GNU @@ -231,7 +231,7 @@ jobs: shell: micromamba-shell {0} run: | export PYTENSOR_FLAGS=mode=FAST_COMPILE,warn__ignore_bug_before=all,on_opt_error=raise,on_shape_error=raise,gcc__cxxflags=-pipe - pytest --runslow --benchmark-only --benchmark-json output.json + pytest --import-mode=importlib --runslow --benchmark-only --benchmark-json output.json - name: Store benchmark result uses: benchmark-action/github-action-benchmark@v1 with: diff --git a/pyproject.toml b/pyproject.toml index 119cca7a99..63cccda6d6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -117,7 +117,12 @@ versionfile_build = "pytensor/_version.py" tag_prefix = "rel-" [tool.pytest] -addopts = "--durations=50 --doctest-modules pytensor --import-mode=importlib --ignore=pytensor/misc/check_duplicate_key.py --ignore=pytensor/link" +addopts = [ + "--durations=50", + "--doctest-modules=pytensor", + "--ignore=pytensor/misc/check_duplicate_key.py", + "--ignore=pytensor/link" +] testpaths = "tests/" [tool.ruff]