From 78b2043df264583154da60adcc709a2a3aa70162 Mon Sep 17 00:00:00 2001 From: Ross Barnowski Date: Mon, 15 Apr 2024 11:09:38 -0700 Subject: [PATCH] Add test for unwrapped Yields validation. --- numpydoc/tests/test_validate.py | 44 ++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/numpydoc/tests/test_validate.py b/numpydoc/tests/test_validate.py index d41e4bd0..eb00f896 100644 --- a/numpydoc/tests/test_validate.py +++ b/numpydoc/tests/test_validate.py @@ -2,10 +2,12 @@ import sys import warnings from contextlib import nullcontext -from functools import cached_property, partial +from functools import cached_property, partial, wraps from inspect import getsourcelines, getsourcefile from numpydoc import validate +from numpydoc.validate import Validator +from numpydoc.docscrape import get_doc_object import numpydoc.tests @@ -1692,3 +1694,43 @@ def test_source_file_name_with_properties(self, property, file_name): ) ) assert doc.source_file_name == file_name + + +def test_is_generator_validation_with_decorator(): + """Ensure that the check for a Yields section when an object is a generator + (YD01) works with decorated generators.""" + + def tinsel(f): + @wraps(f) + def wrapper(*args, **kwargs): + return f(*args, **kwargs) + + return wrapper + + def foo(): + """A simple generator""" + yield from range(10) + + @tinsel + def bar(): + """Generator wrapped once""" + yield from range(10) + + @tinsel + @tinsel + @tinsel + def baz(): + """Generator wrapped multiple times""" + yield from range(10) + + # foo without wrapper is a generator + v = Validator(get_doc_object(foo)) + assert v.is_generator_function + + # Wrapped once + v = Validator(get_doc_object(bar)) + assert v.is_generator_function + + # Wrapped multiple times + v = Validator(get_doc_object(baz)) + assert v.is_generator_function