Apache DataFusion SQL Query Engine
ARROW-7874: [Python][Archery] Validate docstrings with numpydoc
Krisztián Szűcs08a7a5772877
diff --git a/dev/archery/archery/cli.py b/dev/archery/archery/cli.pyindex b470629a19..0492603466 100644--- a/dev/archery/archery/cli.py+++ b/dev/archery/archery/cli.py@@ -27,7 +27,7 @@ from .benchmark.compare import RunnerComparator, DEFAULT_THRESHOLDfrom .benchmark.runner import BenchmarkRunner, CppBenchmarkRunnerfrom .lang.cpp import CppCMakeDefinition, CppConfigurationfrom .utils.codec import JsonEncoder-from .utils.lint import linter, LintValidationException+from .utils.lint import linter, python_numpydoc, LintValidationExceptionfrom .utils.logger import logger, ctx as log_ctxfrom .utils.source import ArrowSourcesfrom .utils.tmpdir import tmpdir@@ -222,6 +222,8 @@ def build(ctx, src, build_dir, force, targets, **kwargs):@click.option("--with-flake8", default=True, type=BOOL,show_default=True,help="Lint python files with flake8.")[email protected]("--with-numpydoc", default=False, type=BOOL,+ show_default=True, help="Lint python files with numpydoc.")@click.option("--with-cmake-format", default=True, type=BOOL,show_default=True,help="Lint CMakeFiles.txt files with cmake-format.py.")@@ -247,6 +249,35 @@ def lint(ctx, src, **kwargs):sys.exit(1)[email protected](short_help="Lint python docstring with NumpyDoc")[email protected]('symbols', nargs=-1)[email protected]("--src", metavar="<arrow_src>", default=ArrowSources.find(),+ callback=validate_arrow_sources,+ help="Specify Arrow source directory")[email protected]("--whitelist", "-w", help="Allow only these rules")[email protected]("--blacklist", "-b", help="Disallow these rules")+def numpydoc(src, symbols, whitelist, blacklist):+ """+ Pass list of modules or symbols as arguments to restrict the validation.++ By default all modules of pyarrow are tried to be validated.++ Examples+ --------+ archery numpydoc pyarrow.dataset+ archery numpydoc pyarrow.csv pyarrow.json pyarrow.parquet+ archery numpydoc pyarrow.array+ """+ blacklist = blacklist or {'GL01', 'SA01', 'EX01', 'ES01'}+ try:+ results = python_numpydoc(symbols, whitelist=whitelist,+ blacklist=blacklist)+ for result in results:+ result.ok()+ except LintValidationException:+ sys.exit(1)++@archery.group()@click.pass_contextdef benchmark(ctx):diff --git a/dev/archery/archery/lang/python.py b/dev/archery/archery/lang/python.pynew file mode 100644index 0000000000..521e9bb71a--- /dev/null+++ b/dev/archery/archery/lang/python.py@@ -0,0 +1,210 @@+# Licensed to the Apache Software Foundation (ASF) under one+# or more contributor license agreements. See the NOTICE file+# distributed with this work for additional information+# regarding copyright ownership. The ASF licenses this file+# to you under the Apache License, Version 2.0 (the+# "License"); you may not use this file except in compliance+# with the License. You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing,+# software distributed under the License is distributed on an+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY+# KIND, either express or implied. See the License for the+# specific language governing permissions and limitations+# under the License.++import inspect+import tokenize+from contextlib import contextmanager++try:+ from numpydoc.validate import Docstring, validate+except ImportError:+ have_numpydoc = False+else:+ have_numpydoc = True++from ..utils.command import Command, default_bin+++class Flake8(Command):+ def __init__(self, flake8_bin=None):+ self.bin = default_bin(flake8_bin, "flake8")+++def _tokenize_signature(s):+ lines = s.encode('ascii').splitlines()+ generator = iter(lines).__next__+ return tokenize.tokenize(generator)+++def _convert_typehint(tokens):+ names = []+ opening_bracket_reached = False+ for token in tokens:+ # omit the tokens before the opening bracket+ if not opening_bracket_reached:+ if token.string == '(':+ opening_bracket_reached = True+ else:+ continue++ if token.type == 1: # type 1 means NAME token+ names.append(token)+ else:+ if len(names) == 1:+ yield (names[0].type, names[0].string)+ elif len(names) == 2:+ # two "NAME" tokens follow each other which means a cython+ # typehint like `bool argument`, so remove the typehint+ # note that we could convert it to python typehints, but hints+ # are not supported by _signature_fromstr+ yield (names[1].type, names[1].string)+ elif len(names) > 2:+ print(names)+ raise ValueError('More than two NAME tokens follow each other')+ names = []+ yield (token.type, token.string)+++def inspect_signature(obj):+ """+ Custom signature inspection primarly for cython generated callables.++ Cython puts the signatures to the first line of the docstrings, which we+ can reuse to parse the python signature from, but some gymnastics are+ required, like removing the cython typehints.++ It converts the cython signature:+ array(obj, type=None, mask=None, size=None, from_pandas=None,+ bool safe=True, MemoryPool memory_pool=None)+ To:+ <Signature (obj, type=None, mask=None, size=None, from_pandas=None,+ safe=True, memory_pool=None)>+ """+ cython_signature = obj.__doc__.splitlines()[0]+ cython_tokens = _tokenize_signature(cython_signature)+ python_tokens = _convert_typehint(cython_tokens)+ python_signature = tokenize.untokenize(python_tokens)+ return inspect._signature_fromstr(inspect.Signature, obj, python_signature)+++class NumpyDoc:++ def __init__(self, symbols=None):+ if not have_numpydoc:+ raise RuntimeError(+ 'Numpydoc is not available, install the development version '+ 'with command: pip install '+ 'git+https://github.com/numpy/numpydoc'+ )+ self.symbols = set(symbols or {'pyarrow'})++ def traverse(self, fn, obj, from_package):+ """Apply a function on publicly exposed API components.++ Recursively iterates over the members of the passed object. It omits+ any '_' prefixed and thirdparty (non pyarrow) symbols.++ Parameters+ ----------+ obj : Any+ from_package : string, default 'pyarrow'+ Predicate to only consider objects from this package.+ """+ todo = [obj]+ seen = set()++ while todo:+ obj = todo.pop()+ if obj in seen:+ continue+ else:+ seen.add(obj)++ fn(obj)++ for name in dir(obj):+ if name.startswith('_'):+ continue++ member = getattr(obj, name)+ module = getattr(member, '__module__', None)+ if not (module and module.startswith(from_package)):+ continue++ todo.append(member)++ @contextmanager+ def _apply_patches(self):+ """+ Patch Docstring class to bypass loading already loaded python objects.+ """+ orig_load_obj = Docstring._load_obj+ orig_signature = inspect.signature++ @staticmethod+ def _load_obj(obj):+ # By default it expects a qualname and import the object, but we+ # have already loaded object after the API traversal.+ if isinstance(obj, str):+ return orig_load_obj(obj)+ else:+ return obj++ def signature(obj):+ # inspect.signature tries to parse __text_signature__ if other+ # properties like __signature__ doesn't exists, but cython+ # doesn't set that property despite that embedsignature cython+ # directive is set. The only way to inspect a cython compiled+ # callable's signature to parse it from __doc__ while+ # embedsignature directive is set during the build phase.+ # So path inspect.signature function to attempt to parse the first+ # line of callable.__doc__ as a signature.+ try:+ return orig_signature(obj)+ except Exception as orig_error:+ try:+ return inspect_signature(obj)+ except Exception:+ raise orig_error++ try:+ Docstring._load_obj = _load_obj+ inspect.signature = signature+ yield+ finally:+ Docstring._load_obj = orig_load_obj+ inspect.signature = orig_signature++ def validate(self, from_package='', rules_blacklist=None,+ rules_whitelist=None):+ results = []++ def callback(obj):+ result = validate(obj)++ errors = []+ for errcode, errmsg in result.get('errors', []):+ if rules_whitelist and errcode not in rules_whitelist:+ continue+ if rules_blacklist and errcode in rules_blacklist:+ continue+ errors.append((errcode, errmsg))++ if len(errors):+ result['errors'] = errors+ results.append((obj, result))++ with self._apply_patches():+ for symbol in self.symbols:+ try:+ obj = Docstring._load_obj(symbol)+ except (ImportError, AttributeError):+ print('{} is not available for import'.format(symbol))+ else:+ self.traverse(callback, obj, from_package=from_package)++ return resultsdiff --git a/dev/archery/archery/utils/lint.py b/dev/archery/archery/utils/lint.pyindex a9f9d6b59e..547f623ee0 100644--- a/dev/archery/archery/utils/lint.py+++ b/dev/archery/archery/utils/lint.py@@ -18,12 +18,15 @@import gzipimport os+import click+from .command import Bash, Command, default_binfrom .cmake import CMakefrom .git import gitfrom .logger import loggerfrom ..lang.cpp import CppCMakeDefinition, CppConfigurationfrom ..lang.rust import Cargo+from ..lang.python import Flake8, NumpyDocfrom .rat import Rat, exclusion_from_globsfrom .tmpdir import tmpdir@@ -98,13 +101,8 @@ def cmake_linter(src, fix=False):yield LintResult.from_cmd(cmake_format("--check"))-class Flake8(Command):- def __init__(self, flake8_bin=None):- self.bin = default_bin(flake8_bin, "flake8")--def python_linter(src):- """ Run flake8 linter on python/pyarrow, and dev/. """+ """Run flake8 linter on python/pyarrow, and dev/. """logger.info("Running python linters")flake8 = Flake8()@@ -120,8 +118,95 @@ def python_linter(src):check=False))+def python_numpydoc(symbols=None, whitelist=None, blacklist=None):+ """Run numpydoc linter on python.++ Pyarrow must be available for import.+ """+ logger.info("Running python docstring linters")+ # by default try to run on all pyarrow package+ symbols = symbols or {+ 'pyarrow',+ 'pyarrow.compute',+ 'pyarrow.csv',+ 'pyarrow.dataset',+ 'pyarrow.feather',+ 'pyarrow.flight',+ 'pyarrow.fs',+ 'pyarrow.gandiva',+ 'pyarrow.ipc',+ 'pyarrow.json',+ 'pyarrow.orc',+ 'pyarrow.parquet',+ 'pyarrow.plasma',+ 'pyarrow.types',+ }+ try:+ numpydoc = NumpyDoc(symbols)+ except RuntimeError:+ logger.error('Numpydoc is not available')+ return++ results = numpydoc.validate(+ # limit the validation scope to the pyarrow package+ from_package='pyarrow',+ rules_whitelist=whitelist,+ rules_blacklist=blacklist+ )++ if len(results) == 0:+ yield LintResult(success=True)+ return++ number_of_violations = 0+ for obj, result in results:+ errors = result['errors']++ # inspect doesn't play nice with cython generated source code,+ # to use a hacky way to represent a proper __qualname__+ doc = getattr(obj, '__doc__', '')+ name = getattr(obj, '__name__', '')+ qualname = getattr(obj, '__qualname__', '')+ module = getattr(obj, '__module__', '')+ instance = getattr(obj, '__self__', '')+ if instance:+ klass = instance.__class__.__name__+ else:+ klass = ''++ try:+ cython_signature = doc.splitlines()[0]+ except Exception:+ cython_signature = ''++ desc = '.'.join(filter(None, [module, klass, qualname or name]))++ click.echo()+ click.echo(click.style(desc, bold=True, fg='yellow'))+ if cython_signature:+ qualname_with_signature = '.'.join([module, cython_signature])+ click.echo(+ click.style(+ '-> {}'.format(qualname_with_signature),+ fg='yellow'+ )+ )++ for error in errors:+ number_of_violations += 1+ click.echo('{}: {}'.format(*error))++ msg = 'Total number of docstring violations: {}'.format(+ number_of_violations+ )+ click.echo()+ click.echo(click.style(msg, fg='red'))++ yield LintResult(success=False)++def rat_linter(src, root):- """ Run apache-rat license linter. """+ """Run apache-rat license linter."""logger.info("Running apache-rat linter")if src.git_dirty:@@ -145,14 +230,14 @@ def rat_linter(src, root):def r_linter(src):- """ Run R linter. """+ """Run R linter."""logger.info("Running r linter")r_lint_sh = os.path.join(src.r, "lint.sh")yield LintResult.from_cmd(Bash().run(r_lint_sh, check=False))def rust_linter(src):- """ Run Rust linter. """+ """Run Rust linter."""logger.info("Running rust linter")cargo = Cargo()@@ -181,7 +266,7 @@ def is_docker_image(path):def docker_linter(src):- """ Run Hadolint docker linter. """+ """Run Hadolint docker linter."""logger.info("Running docker linter")hadolint = Hadolint()@@ -199,11 +284,11 @@ def docker_linter(src):def linter(src, with_clang_format=True, with_cpplint=True,with_clang_tidy=False, with_iwyu=False,- with_flake8=True, with_cmake_format=True,+ with_flake8=True, with_numpydoc=False, with_cmake_format=True,with_rat=True, with_r=True, with_rust=True,with_docker=True,fix=False):- """ Run all linters. """+ """Run all linters."""with tmpdir(prefix="arrow-lint-") as root:build_dir = os.path.join(root, "cpp-build")@@ -223,6 +308,9 @@ def linter(src, with_clang_format=True, with_cpplint=True,if with_flake8:results.extend(python_linter(src))+ if with_numpydoc:+ results.extend(python_numpydoc())+if with_cmake_format:results.extend(cmake_linter(src, fix=fix))