-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: check for pytest compatibility (#94)
- Loading branch information
Showing
5 changed files
with
50 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters