jenkins-bot has submitted this change. (
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/610271 )
Change subject: [IMPR] replace inspect.getargspec by getfullargspec
......................................................................
[IMPR] replace inspect.getargspec by getfullargspec
- inspect.getargspec has been deprecated since Python 3.0;
getfullargspec should be used instead. Use the arg attribute
of the namedtuple in api.py instead of indexing.
- remove Python 3 reimplementation of getargspec/ArgSpec;
print a FutureWarning if it is used.
- remove TestArgSpec, TestPythonArgSpec and MetaTestArgSpec
because get(full)argspec are Python functions now
Change-Id: Idc191ec39c72b5b17ccb0ac860586b8cf43a2bb0
---
M pywikibot/data/api.py
M pywikibot/tools/__init__.py
M tests/tools_tests.py
3 files changed, 10 insertions(+), 113 deletions(-)
Approvals:
Matěj Suchánek: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/data/api.py b/pywikibot/data/api.py
index 11b8826..017fe80 100644
--- a/pywikibot/data/api.py
+++ b/pywikibot/data/api.py
@@ -15,11 +15,11 @@
import re
import traceback
-
from collections.abc import Container, MutableMapping, Sized
from email.generator import BytesGenerator
from email.mime.multipart import MIMEMultipart as MIMEMultipartOrig
from email.mime.nonmultipart import MIMENonMultipart
+from inspect import getfullargspec
from io import BytesIO
from warnings import warn
from urllib.parse import urlencode, unquote
@@ -35,8 +35,7 @@
)
from pywikibot.family import SubdomainFamily
from pywikibot.tools import (
- deprecated, itergroup, PYTHON_VERSION,
- getargspec, remove_last_args
+ deprecated, itergroup, PYTHON_VERSION, remove_last_args
)
from pywikibot.tools.formatter import color_format
@@ -1299,7 +1298,7 @@
for super_cls in inspect.getmro(cls):
if not super_cls.__name__.endswith('Request'):
break
- args |= set(getargspec(super_cls.__init__)[0])
+ args |= set(getfullargspec(super_cls.__init__).args)
else:
raise ValueError('Request was not a super class of '
'{0!r}'.format(cls))
diff --git a/pywikibot/tools/__init__.py b/pywikibot/tools/__init__.py
index 174ce85..02b6133 100644
--- a/pywikibot/tools/__init__.py
+++ b/pywikibot/tools/__init__.py
@@ -25,6 +25,7 @@
from distutils.version import LooseVersion, Version
from functools import wraps
from importlib import import_module
+from inspect import getfullargspec
from ipaddress import ip_address
from itertools import zip_longest
from warnings import catch_warnings, showwarning, warn
@@ -34,7 +35,6 @@
PYTHON_VERSION = sys.version_info[:3]
PY2 = (PYTHON_VERSION[0] == 2)
-
StringTypes = (str, bytes)
UnicodeType = str
@@ -54,37 +54,6 @@
lzma = lzma_import_error
-if PYTHON_VERSION < (3, 5):
- # although deprecated in 3 completely no message was emitted until 3.5
- ArgSpec = inspect.ArgSpec
- getargspec = inspect.getargspec
-else:
- ArgSpec = collections.namedtuple('ArgSpec', ['args', 'varargs', 'keywords',
- 'defaults'])
-
- def getargspec(func):
- """Python 3 implementation using inspect.signature."""
- sig = inspect.signature(func)
- args = []
- defaults = []
- varargs = None
- kwargs = None
- for p in sig.parameters.values():
- if p.kind == inspect.Parameter.VAR_POSITIONAL:
- varargs = p.name
- elif p.kind == inspect.Parameter.VAR_KEYWORD:
- kwargs = p.name
- else:
- args += [p.name]
- if p.default != inspect.Parameter.empty:
- defaults += [p.default]
- if defaults:
- defaults = tuple(defaults)
- else:
- defaults = None
- return ArgSpec(args, varargs, kwargs, defaults)
-
-
_logger = 'tools'
@@ -1559,7 +1528,7 @@
"""
name = obj.__full_name__
depth = get_wrapper_depth(wrapper) + 1
- args, varargs, kwargs, _ = getargspec(wrapper.__wrapped__)
+ args, varargs, kwargs, *_ = getfullargspec(wrapper.__wrapped__)
if varargs is not None and kwargs is not None:
raise ValueError('{0} may not have * or ** args.'.format(
name))
@@ -1935,3 +1904,7 @@
since='20200723', future_warning=True)
wrapper._add_deprecated_attr('IteratorNextMixin', replacement_name='',
since='20200723', future_warning=True)
+wrapper._add_deprecated_attr('getargspec', inspect.getargspec,
+ since='20200712', future_warning=True)
+wrapper._add_deprecated_attr('ArgSpec', inspect.ArgSpec,
+ since='20200712', future_warning=True)
diff --git a/tests/tools_tests.py b/tests/tools_tests.py
index fce4864..f58b765 100644
--- a/tests/tools_tests.py
+++ b/tests/tools_tests.py
@@ -6,11 +6,9 @@
#
# Distributed under the terms of the MIT license.
import decimal
-import inspect
import os.path
import subprocess
import tempfile
-import warnings
from collections.abc import Mapping
from collections import OrderedDict
@@ -22,7 +20,7 @@
from tests import join_xml_data_path, mock
from tests.aspects import (
- unittest, require_modules, DeprecationTestCase, TestCase, MetaTestCaseClass
+ unittest, require_modules, DeprecationTestCase, TestCase
)
@@ -623,79 +621,6 @@
self.assertRaises(StopIteration, next, deduper)
-class MetaTestArgSpec(MetaTestCaseClass):
-
- """Metaclass to create dynamically the tests. Set the net flag to false."""
-
- def __new__(cls, name, bases, dct):
- """Create a new test case class."""
- def create_test(method):
- def test_method(self):
- """Test getargspec."""
- # all expect at least self and param
- expected = method(1, 2)
- returned = self.getargspec(method)
- self.assertEqual(returned, expected)
- self.assertIsInstance(returned, self.expected_class)
- self.assertNoDeprecation()
- return test_method
-
- for attr, tested_method in list(dct.items()):
- if attr.startswith('_method_test_'):
- suffix = attr[len('_method_test_'):]
- cls.add_method(dct, 'test_method_' + suffix,
- create_test(tested_method),
- doc_suffix='on {0}'.format(suffix))
-
- dct['net'] = False
- return super().__new__(cls, name, bases, dct)
-
-
-class TestArgSpec(DeprecationTestCase, metaclass=MetaTestArgSpec):
-
- """Test getargspec and ArgSpec from tools."""
-
- expected_class = tools.ArgSpec
-
- def _method_test_args(self, param):
- """Test method with two positional arguments."""
- return (['self', 'param'], None, None, None)
-
- def _method_test_kwargs(self, param=42):
- """Test method with one positional and one keyword argument."""
- return (['self', 'param'], None, None, (42,))
-
- def _method_test_varargs(self, param, *var):
- """Test method with two positional arguments and var args."""
- return (['self', 'param'], 'var', None, None)
-
- def _method_test_varkwargs(self, param, **var):
- """Test method with two positional arguments and var kwargs."""
- return (['self', 'param'], None, 'var', None)
-
- def _method_test_vars(self, param, *args, **kwargs):
- """Test method with two positional arguments and both var args."""
- return (['self', 'param'], 'args', 'kwargs', None)
-
- def getargspec(self, method):
- """Call tested getargspec function."""
- return tools.getargspec(method)
-
-
-class TestPythonArgSpec(TestArgSpec):
-
- """Test the same tests using Python's implementation."""
-
- expected_class = inspect.ArgSpec
-
- def getargspec(self, method):
- """Call inspect's getargspec function."""
- with warnings.catch_warnings():
- if not tools.PY2:
- warnings.simplefilter('ignore', DeprecationWarning)
- return inspect.getargspec(method)
-
-
class TestFileModeChecker(TestCase):
"""Test parsing password files."""
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/610271
To unsubscribe, or for help writing mail filters, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Idc191ec39c72b5b17ccb0ac860586b8cf43a2bb0
Gerrit-Change-Number: 610271
Gerrit-PatchSet: 8
Gerrit-Owner: Xqt <[email protected]>
Gerrit-Reviewer: Dvorapa <[email protected]>
Gerrit-Reviewer: JJMC89 <[email protected]>
Gerrit-Reviewer: Matěj Suchánek <[email protected]>
Gerrit-Reviewer: Russell Blau <[email protected]>
Gerrit-Reviewer: Zhuyifei1999 <[email protected]>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged
_______________________________________________
Pywikibot-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/pywikibot-commits