Ensure compatibility with Python 3.11 (#2212)

* Replace deprecated `getargspec()` with `getfullargspec()`
* Replace deprecated `formatargspec()` with custom implementation

Fixes #2209.
This commit is contained in:
Christoph Erhardt 2022-06-23 12:05:54 +02:00 committed by GitHub
parent 8af6302c81
commit 50d73bfbc8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 7 deletions

View File

@ -3,11 +3,9 @@ from __future__ import (unicode_literals, division, absolute_import, print_funct
import os import os
from inspect import formatargspec
from sphinx.ext import autodoc from sphinx.ext import autodoc
from powerline.lint.inspect import getconfigargspec from powerline.lint.inspect import formatconfigargspec, getconfigargspec
from powerline.segments import Segment from powerline.segments import Segment
from powerline.lib.unicode import unicode from powerline.lib.unicode import unicode
@ -28,7 +26,7 @@ class ThreadedDocumenter(autodoc.FunctionDocumenter):
def format_args(self): def format_args(self):
argspec = getconfigargspec(self.object) argspec = getconfigargspec(self.object)
return formatargspec(*argspec, formatvalue=formatvalue).replace('\\', '\\\\') return formatconfigargspec(*argspec, formatvalue=formatvalue).replace('\\', '\\\\')
class Repr(object): class Repr(object):

View File

@ -1,7 +1,8 @@
# vim:fileencoding=utf-8:noet # vim:fileencoding=utf-8:noet
from __future__ import (unicode_literals, division, absolute_import, print_function) from __future__ import (unicode_literals, division, absolute_import, print_function)
from inspect import ArgSpec, getargspec from inspect import FullArgSpec, getfullargspec
from itertools import zip_longest
from powerline.segments import Segment from powerline.segments import Segment
@ -33,7 +34,7 @@ def getconfigargspec(obj):
requires_filesystem_watcher = hasattr(obj, 'powerline_requires_filesystem_watcher') requires_filesystem_watcher = hasattr(obj, 'powerline_requires_filesystem_watcher')
for name, method in argspecobjs: for name, method in argspecobjs:
argspec = getargspec(method) argspec = getfullargspec(method)
omitted_args = get_omitted_args(name, method) omitted_args = get_omitted_args(name, method)
largs = len(argspec.args) largs = len(argspec.args)
for i, arg in enumerate(reversed(argspec.args)): for i, arg in enumerate(reversed(argspec.args)):
@ -60,4 +61,31 @@ def getconfigargspec(obj):
if arg not in args: if arg not in args:
args.insert(0, arg) args.insert(0, arg)
return ArgSpec(args=args, varargs=None, keywords=None, defaults=tuple(defaults)) return FullArgSpec(args=args, varargs=None, varkw=None, defaults=tuple(defaults), kwonlyargs=(), kwonlydefaults={}, annotations={})
def formatconfigargspec(args, varargs=None, varkw=None, defaults=None,
kwonlyargs=(), kwonlydefaults={}, annotations={},
formatvalue=lambda value: '=' + repr(value)):
'''Format an argument spec from the values returned by getconfigargspec.
This is a specialized replacement for inspect.formatargspec, which has been
deprecated since Python 3.5 and was removed in Python 3.11. It supports
valid values for args, defaults and formatvalue; all other parameters are
expected to be either empty or None.
'''
assert varargs is None
assert varkw is None
assert not kwonlyargs
assert not kwonlydefaults
assert not annotations
specs = []
if defaults:
firstdefault = len(args) - len(defaults)
for i, arg in enumerate(args):
spec = arg
if defaults and i >= firstdefault:
spec += formatvalue(defaults[i - firstdefault])
specs.append(spec)
return '(' + ', '.join(specs) + ')'