Skip to content

Commit

Permalink
Merge pull request #5855 from MetRonnie/pipe-signature
Browse files Browse the repository at this point in the history
Async pipe decorator: preserve wrapped function signature
  • Loading branch information
oliver-sanders authored Dec 4, 2023
2 parents debd0d2 + eff84ca commit f09b3ef
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
13 changes: 13 additions & 0 deletions cylc/flow/async_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import asyncio
from functools import partial, wraps
from inspect import signature
import os
from pathlib import Path
from typing import List, Union
Expand Down Expand Up @@ -262,10 +263,22 @@ def __str__(self):
def __repr__(self):
return _AsyncPipe(self.func).__repr__()

@property
def __name__(self):
return self.func.__name__

@property
def __doc__(self):
return self.func.__doc__

@property
def __signature__(self):
return signature(self.func)

@property
def __annotations__(self):
return self.func.__annotations__


def pipe(func=None, preproc=None):
"""An asynchronous pipe implementation in pure Python.
Expand Down
8 changes: 6 additions & 2 deletions tests/unit/test_async_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import asyncio
from inspect import signature
import logging
from pathlib import Path
from random import random
Expand Down Expand Up @@ -209,14 +210,17 @@ def test_pipe_brackets():


@pipe
async def documented(x):
async def documented(x: str, y: int = 0):
"""The docstring for the pipe function."""
pass


def test_documentation():
"""It should preserve the docstring of pipe functions."""
"""It should preserve the docstring, signature & annotations of
the wrapped function."""
assert documented.__doc__ == 'The docstring for the pipe function.'
assert documented.__annotations__ == {'x': str, 'y': int}
assert str(signature(documented)) == '(x: str, y: int = 0)'


def test_rewind():
Expand Down

0 comments on commit f09b3ef

Please sign in to comment.