From 87792db2848d98619a9a09fff977ce6ea1d67de4 Mon Sep 17 00:00:00 2001 From: Mark Williams Date: Wed, 25 May 2016 00:15:09 -0700 Subject: [PATCH] funcutils.FunctionBuilder: Replace brittle string replacement with a regex that removes the keyword-only marker from the formatted argspec in Python 3. The regex tolerates variable spacing! --- boltons/funcutils.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/boltons/funcutils.py b/boltons/funcutils.py index 5928ae6..da00ee7 100644 --- a/boltons/funcutils.py +++ b/boltons/funcutils.py @@ -7,6 +7,7 @@ correcting Python's standard metaprogramming facilities. from __future__ import print_function import sys +import re import inspect import functools import itertools @@ -319,6 +320,13 @@ class FunctionBuilder(object): {}, self.annotations) + _REMOVE_KWONLY_MARKER = re.compile(r""" + \* # a star + \ * # followed by any number of spaces + , # followed by a comma + \ * # followed by any number of spaces + """, re.VERBOSE) + def get_invocation_str(self): kwonly_pairs = None formatters = {} @@ -335,7 +343,7 @@ class FunctionBuilder(object): kwonly_pairs, {}, **formatters) - sig = sig.replace('*, ', '') + sig = self._REMOVE_KWONLY_MARKER.sub('', sig) return sig[1:-1] @classmethod