Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sage -pytest #33572

Closed
mkoeppe opened this issue Mar 26, 2022 · 47 comments
Closed

sage -pytest #33572

mkoeppe opened this issue Mar 26, 2022 · 47 comments

Comments

@mkoeppe
Copy link
Member

mkoeppe commented Mar 26, 2022

Thin wrapper around pytest for running it in the Sage environment.

as suggested in #31924 comment:94

Similar to our existing wrapper sage -tox.

CC: @tornaria @tobiasdiez @soehms

Component: doctest framework

Author: Matthias Koeppe

Branch/Commit: 8825079

Reviewer: Tobias Diez

Issue created by migration from https://trac.sagemath.org/ticket/33572

@mkoeppe mkoeppe added this to the sage-9.6 milestone Mar 26, 2022
@mkoeppe
Copy link
Member Author

mkoeppe commented Mar 26, 2022

Branch: u/mkoeppe/sage__pytest

@mkoeppe
Copy link
Member Author

mkoeppe commented Mar 26, 2022

comment:2

Here's an attempt at this command, but I think I'll need some help here to make it actually work:

$ ./sage -pytest src/sage/numerical/
============================================================================================================ test session starts ============================================================================================================
platform darwin -- Python 3.10.2, pytest-7.0.1, pluggy-1.0.0
rootdir: /Users/mkoeppe/s/sage/sage-rebasing/worktree-gcc11/src, configfile: tox.ini
collected 0 items / 5 errors                                                                                                                                                                                                                

================================================================================================================== ERRORS ===================================================================================================================
______________________________________________________________________________________ ERROR collecting sage/numerical/backends/cvxopt_backend_test.py ______________________________________________________________________________________
ImportError while importing test module '/Users/mkoeppe/s/sage/sage-rebasing/worktree-gcc11/src/sage/numerical/backends/cvxopt_backend_test.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
src/sage/numerical/backends/cvxopt_backend_test.py:5: in <module>
    from sage.numerical.mip import MixedIntegerLinearProgram
sage/numerical/mip.pyx:242: in init sage.numerical.mip
    ???
sage/rings/integer_ring.pyx:1: in init sage.rings.integer_ring
    ???
sage/rings/integer.pyx:1: in init sage.rings.integer
    ???
sage/rings/rational.pyx:79: in init sage.rings.rational
    ???
E   ImportError: cannot import name ZZ

New commits:

9385501src/bin/sage: Implement 'sage --pytest'

@mkoeppe
Copy link
Member Author

mkoeppe commented Mar 26, 2022

Changed author from Matthias Koeppe to Matthias Koeppe, ...

@mkoeppe
Copy link
Member Author

mkoeppe commented Mar 26, 2022

Commit: 9385501

@tobiasdiez
Copy link
Contributor

comment:3

That looks like a typical cyclic import that relies on sage.all to be broken. It is also not specific to pytest as the following example fails with the same error (when run through ./sage src/test_module_load.py).

import importlib.util
spec = importlib.util.spec_from_file_location("module.name", "src/sage/numerical/backends/cvxopt_backend_test.py")
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)

@tobiasdiez
Copy link
Contributor

comment:4

Moreover, there are a few dozens of such import errors:

import importlib.util
import pathlib
for file in pathlib.Path().glob("src/sage/**/*.py"):
    try: 
        spec = importlib.util.spec_from_file_location("module.name", file)
        module = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(module)
    except:
        print(file)

@mkoeppe
Copy link
Member Author

mkoeppe commented Mar 27, 2022

comment:5

Can't you just add import sage.all in conftest.py?

@tobiasdiez
Copy link
Contributor

comment:6

There is already a hook which adds sage.all, but that one is only executed after the collection phase.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Mar 27, 2022

Branch pushed to git repo; I updated commit sha1. New commits:

abbb61fsrc/conftest.py: import sage.all to avoid cyclic import errors

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Mar 27, 2022

Changed commit from 9385501 to abbb61f

@mkoeppe
Copy link
Member Author

mkoeppe commented Mar 27, 2022

Changed author from Matthias Koeppe, ... to Matthias Koeppe

@tobiasdiez
Copy link
Contributor

comment:9

Ah that's what you had in mind. Seems to work indeed. Can you please add a comment that prevents pyright to complain about the unused import and add a link to #33580. Thanks!

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Mar 27, 2022

Branch pushed to git repo; I updated commit sha1. New commits:

9bb72easrc/conftest.py: Add # type: ignore, add reference to trac ticket
2c16704src/conftest.py: Remove outdated text

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Mar 27, 2022

Changed commit from abbb61f to 2c16704

@tobiasdiez
Copy link
Contributor

comment:12
$ ./sage -pytest
========================================================================================================= test session starts ==========================================================================================================
platform linux -- Python 3.8.10, pytest-7.0.1, pluggy-1.0.0
rootdir: /workspace/sagetrac-mirror/src
collected 0 items / 1 error                                                                                                                                                                                                            

================================================================================================================ ERRORS ================================================================================================================
____________________________________________________________________________________________________ ERROR collecting test session _____________________________________________________________________________________________________
local/var/lib/sage/venv-python3.8/lib/python3.8/site-packages/IPython/conftest.py:9: in <module>
    from .testing import tools
E   ModuleNotFoundError: No module named 'workspace'

should probably fallback to ./sage -pytest src

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Mar 27, 2022

Changed commit from 2c16704 to 30642ba

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Mar 27, 2022

Branch pushed to git repo; I updated commit sha1. New commits:

30642basrc/bin/sage: Handle 'sage -pytest' without file args

@tobiasdiez
Copy link
Contributor

comment:24

Thanks for the follow-up. However, during the merge something seems to went wrong. The collect_ignore in the test config shouldn't be readded.

@tobiasdiez
Copy link
Contributor

comment:25

Replying to @mkoeppe:

Replying to @tobiasdiez:

While playing around, I've also discovered the following error:

$ ./sage --pytest src/sage/ext_data/nbconvert/postprocess.py

This file is to be considered a data file; it's not a module of the Sage library

So you are saying it should be removed during collection? Then please do so by adding a filter in pytest_collect_file similar to #33560.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Mar 30, 2022

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

f0d5f84src/conftest.py: import sage.all to avoid cyclic import errors
d881a5asrc/conftest.py: Add # type: ignore, add reference to trac ticket
1242e1fsrc/conftest.py: Remove outdated text
0d9225dsrc/bin/sage: Handle 'sage -pytest' without file args
05bc58fsrc/tox.ini (pytest): Set --import-mode importlib here, not in src/bin/sage, src/bin/sage-runtests

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Mar 30, 2022

Changed commit from 11219e5 to 05bc58f

@mkoeppe
Copy link
Member Author

mkoeppe commented Mar 30, 2022

comment:27

Indeed. Thanks for catching the mistake in rebasing

@mkoeppe
Copy link
Member Author

mkoeppe commented Mar 30, 2022

comment:28

Replying to @tobiasdiez:

So you are saying it should be removed during collection? Then please do so by adding a filter in pytest_collect_file similar to #33560.

The directories marked by the presence of a nodoctest file are ignored by the doctester.

$ find src -name nodoctest
src/sage/ext_data/nodoctest
src/sage/doctest/tests/nodoctest

Not sure if we want to use this mechanism somehow or just hardcode it in conftest.py

@tobiasdiez
Copy link
Contributor

comment:30

Replying to @mkoeppe:

Replying to @tobiasdiez:

So you are saying it should be removed during collection? Then please do so by adding a filter in pytest_collect_file similar to #33560.

The directories marked by the presence of a nodoctest file are ignored by the doctester.

$ find src -name nodoctest
src/sage/ext_data/nodoctest
src/sage/doctest/tests/nodoctest

Not sure if we want to use this mechanism somehow or just hardcode it in conftest.py

I would say its simpler to exclude these two directories in conftest. As you already know, I'm not a big fan of such marker files in general.

@mkoeppe
Copy link
Member Author

mkoeppe commented Mar 30, 2022

comment:31

OK, sounds good.

@mkoeppe
Copy link
Member Author

mkoeppe commented Mar 30, 2022

comment:32

Given that ./sage --pytest src/sage/ext_data/nbconvert/ collects nothing, I don't think we need to do anything about ./sage --pytest src/sage/ext_data/nbconvert/postprocess.py.

@tobiasdiez
Copy link
Contributor

comment:33

Why not?
It's the same issue as with sage -t and #33560. It works if you specify a folder and let pytest do the collection, but doesn't work correctly if you overwrite the collection by specifying the complete path.

@mkoeppe
Copy link
Member Author

mkoeppe commented Mar 31, 2022

comment:34

Not the same because there is no existing practice of running pytest on that file.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Mar 31, 2022

Changed commit from 05bc58f to 8825079

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Mar 31, 2022

Branch pushed to git repo; I updated commit sha1. New commits:

8825079src/doc/en/developer/tools.rst: Update section on pytest

@tobiasdiez
Copy link
Contributor

comment:36

Replying to @mkoeppe:

Not the same because there is no existing practice of running pytest on that file.

That applies to Cython files as well, or not? We still exclude them in #33560.

@mkoeppe
Copy link
Member Author

mkoeppe commented Mar 31, 2022

comment:37

There is very much existing practice of running sage -t on Cython files.

@mkoeppe
Copy link
Member Author

mkoeppe commented Apr 1, 2022

comment:38

Let's get this in please

@tobiasdiez
Copy link
Contributor

comment:39

Okay. I still think these folders with nodoctest files should be skipped; but that can be done in a follow-up ticket. (The sagebot issues don't seem to be related to this ticket.)

@mkoeppe
Copy link
Member Author

mkoeppe commented Apr 1, 2022

comment:40

Thanks!

@vbraun
Copy link
Member

vbraun commented Apr 3, 2022

Changed branch from u/mkoeppe/sage__pytest to 8825079

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants