Skip to content

Commit

Permalink
fix: check for pytest compatibility (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
15r10nk authored Jan 12, 2025
1 parent 2a98c71 commit 0edb6ca
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 6 deletions.
5 changes: 4 additions & 1 deletion executing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
from collections import namedtuple
_VersionInfo = namedtuple('_VersionInfo', ('major', 'minor', 'micro'))
from .executing import Source, Executing, only, NotOneValueFound, cache, future_flags

from ._pytest_utils import is_pytest_compatible

try:
from .version import __version__ # type: ignore[import]
if "dev" in __version__:
Expand All @@ -22,4 +25,4 @@
__version_info__ = _VersionInfo(*map(int, __version__.split('.')))


__all__ = ["Source"]
__all__ = ["Source","is_pytest_compatible"]
16 changes: 16 additions & 0 deletions executing/_pytest_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import sys



def is_pytest_compatible() -> bool:
""" returns true if executing can be used for expressions inside assert statements which are rewritten by pytest
"""
if sys.version_info < (3, 11):
return False

try:
import pytest
except ImportError:
return False

return pytest.version_tuple >= (8, 3, 4)
25 changes: 25 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@


from typing import Optional, Sequence, Union
from executing._pytest_utils import is_pytest_compatible
import _pytest.assertion.rewrite as rewrite
import importlib.machinery
import types

if not is_pytest_compatible():
original_find_spec = rewrite.AssertionRewritingHook.find_spec


def find_spec(
self,
name: str,
path: Optional[Sequence[Union[str, bytes]]] = None,
target: Optional[types.ModuleType] = None,
) -> Optional[importlib.machinery.ModuleSpec]:

if name == "tests.test_main":
return None
return original_find_spec(self, name, path, target)


rewrite.AssertionRewritingHook.find_spec = find_spec
5 changes: 0 additions & 5 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
# -*- coding: utf-8 -*-
"""
assert rewriting will break executing
PYTEST_DONT_REWRITE
"""
from __future__ import print_function, division
import ast
import contextlib
Expand Down
5 changes: 5 additions & 0 deletions tests/test_pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from time import sleep

import asttokens
from executing._pytest_utils import is_pytest_compatible
import pytest
from littleutils import SimpleNamespace

Expand Down Expand Up @@ -124,6 +125,10 @@ def check_manual_linecache(filename):
def test_exception_catching():
frame = inspect.currentframe()

if is_pytest_compatible():
assert isinstance(Source.executing(frame).node,ast.Call)
return

executing.executing.TESTING = True # this is already the case in all other tests
# Sanity check that this operation usually raises an exception.
# This actually depends on executing not working in the presence of pytest.
Expand Down

0 comments on commit 0edb6ca

Please sign in to comment.