From 512374588b4a86b2f311f3d0286f870fbffd00e6 Mon Sep 17 00:00:00 2001 From: Thomas Hisch Date: Wed, 1 Apr 2020 21:51:01 +0200 Subject: [PATCH] Adapt to Node API change The Node API was changed in pytest 5.4 in https://github.com/pytest-dev/pytest/pull/5975. This commit adds support for the new API but maintains backwards compatiblity. Closes: #11 --- CHANGELOG.md | 4 ++++ pytest_sphinx.py | 30 ++++++++++++++++++++++++------ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 279ab9a..814c6a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## [Unreleased] +### +- Adapt to Node API changes in pytest-5.4 (Fixes #11) + ## [0.2.2] - 2019-05-24 ### - Add hack for handling mock style objects diff --git a/pytest_sphinx.py b/pytest_sphinx.py index 491d351..51785f1 100644 --- a/pytest_sphinx.py +++ b/pytest_sphinx.py @@ -16,10 +16,15 @@ import sys import traceback +from pkg_resources import parse_version + import _pytest.doctest import pytest +PYTEST_PRE_54 = parse_version(pytest.__version__) < parse_version('5.4') + + def pairwise(iterable): """ s -> (s0,s1), (s1,s2), (s2, s3), ... @@ -41,9 +46,15 @@ def pytest_collect_file(path, parent): config = parent.config if path.ext == ".py": if config.option.doctestmodules: - return SphinxDoctestModule(path, parent) + if PYTEST_PRE_54: + return SphinxDoctestModule(path, parent) + else: + return SphinxDoctestModule.from_parent(parent, fspath=path) elif _is_doctest(config, path, parent): - return SphinxDoctestTextfile(path, parent) + if PYTEST_PRE_54: + return SphinxDoctestTextfile(path, parent) + else: + return SphinxDoctestTextfile.from_parent(parent, fspath=path) def _is_doctest(config, path, parent): @@ -343,8 +354,11 @@ def collect(self): docstring=text) if test.examples: - yield _pytest.doctest.DoctestItem( - test.name, self, runner, test) + if PYTEST_PRE_54: + yield _pytest.doctest.DoctestItem(test.name, self, runner, test) + else: + yield _pytest.doctest.DoctestItem.from_parent( + parent=self, name=test.name, runner=runner, dtest=test) class SphinxDoctestModule(pytest.Module): @@ -395,5 +409,9 @@ def _find(self, tests, obj, name, module, source_lines, for test in finder.find(module, module.__name__): if test.examples: - yield _pytest.doctest.DoctestItem( - test.name, self, runner, test) + if PYTEST_PRE_54: + yield _pytest.doctest.DoctestItem( + test.name, self, runner, test) + else: + yield _pytest.doctest.DoctestItem.from_parent( + parent=self, name=test.name, runner=runner, dtest=test)