Open asmodehn opened 6 years ago
GitMate.io thinks possibly related issues are https://github.com/pytest-dev/pytest/issues/1871 (No assertion rewriting in files imported from conftest), https://github.com/pytest-dev/pytest/issues/1840 (register_assert_rewrite warns for already imported and rewritten modules), https://github.com/pytest-dev/pytest/issues/2435 (f), https://github.com/pytest-dev/pytest/issues/295 (lazy import of doctest in pdb breaks debugging if the environment makes doctest unimportable), and https://github.com/pytest-dev/pytest/issues/296 (lazy import of doctest in pdb breaks debugging if the environment makes doctest unimportable).
@asmodehn , I'm no core developer but probably you want to look at the find_module
and load_module
methods in the AssertionRewritingHook
class and see what it does:
https://github.com/pytest-dev/pytest/blob/master/_pytest/assertion/rewrite.py
The import hook is installed in the install_importhook
function in the init module:
https://github.com/pytest-dev/pytest/blob/master/_pytest/assertion/__init__.py
@Sup3rGeo's tips are on point! 👍
I don't have time to check right now, but it might the order of the hooks because we install the assertion rewriter hook with:
Your my_custom_importer
context manager might be installing your hook using sys.meta_path.append
, which would explain why pytest's hook runs first and fails to parse the file.
Oh this is interesting...
I used to assume the correct practice was to put custom hooks at the end of the list (sys.path_hooks or sys.meta_path), to minimize changes in early import sequence in case other tools did the same...
Then I eventually found out that the python3 FileFinder always expect to be the last one (hook always return something which prevent Finder to look into other hooks further in the list).
So now I append in sys.meta_path, and in sys.path_hooks, I look for filefinder and I put my hooks just before. I dont want to change the usual import mechanism, just add the ability to import something else...
Was I mistaken and hooks should be always prepended to the list ? Is there some general advice about it somewhere that I missed ?
On Fri, May 25, 2018, 01:57 Bruno Oliveira notifications@github.com wrote:
I don't have time to check right now, but it might the order of the hooks, because we install the assertion rewriter hook with:
You might want to do the same within your my_custom_importer context manager, this will ensure your hook runs first.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/pytest-dev/pytest/issues/3477#issuecomment-391901087, or mute the thread https://github.com/notifications/unsubscribe-auth/AANgSBKQE-abSZz2uFDTsThMo7649ksAks5t10kCgaJpZM4T_QWX .
I am currently writing a custom importer for a Domain Specific Language.
Lets call
calc
the module written in a simple DSL. Meaning that I do:The import works fine, the DSL is parsed with the Loader, which override the
get_source
method, transforming the DSL into python source and importing it.The problem arises when I use pytest in some test modules:
and when pytest attempts to rewrite the asserts in there :
It breaks on the DSL syntax in that module. It seems it doesn't use the custom loader for that module...
Any idea how to investigate this and get it right ? Any example of pytest running with custom importers (and non-python syntax in module) ? Or is there any way to disable the rewrite, in cases like this where it might get us into trouble ?
my pytest setup:
I ll try to get a minimal reproducible example, but that might be tricky, given the machinery involved...