Skip to content

Commit

Permalink
Use ruff for linting and formatting (#555)
Browse files Browse the repository at this point in the history
  • Loading branch information
jarrodmillman committed May 11, 2024
2 parents eece14b + 2e6d88b commit acad4ec
Show file tree
Hide file tree
Showing 22 changed files with 208 additions and 204 deletions.
1 change: 1 addition & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
4b38c01e680cf2032acb09819bc0985e5dfc5ca8
be316333f436519e0e2ab1379cbdedde79c9ac68
19 changes: 5 additions & 14 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,19 @@ repos:
- id: mixed-line-ending
- id: trailing-whitespace

- repo: https://github.com/psf/black
rev: 3702ba224ecffbcec30af640c149f231d90aebdb # frozen: 24.4.2
hooks:
- id: black

- repo: https://github.com/pre-commit/mirrors-prettier
rev: ffb6a759a979008c0e6dff86e39f4745a2d9eac4 # frozen: v3.1.0
hooks:
- id: prettier
types_or: [yaml, toml, markdown, css, scss, javascript, json]
args: [--prose-wrap=preserve]

- repo: https://github.com/adamchainz/blacken-docs
rev: 960ead214cd1184149d366c6d27ca6c369ce46b6 # frozen: 1.16.0
hooks:
- id: blacken-docs

- repo: https://github.com/asottile/pyupgrade
rev: 12af25eb252deaaecb6b259df40d01f42e716dc3 # frozen: v3.15.2
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "f8a3f8c471fb698229face5ed7640a64900b781e" # frozen: v0.4.4
hooks:
- id: pyupgrade
args: [--py38-plus]
- id: ruff
args: ["--fix", "--show-fixes", "--exit-non-zero-on-fix"]
- id: ruff-format

- repo: local
hooks:
Expand Down
8 changes: 3 additions & 5 deletions doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@
# list see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html

from datetime import date
import numpydoc

# -- Path setup --------------------------------------------------------------

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.

import os
import sys
from datetime import date

import numpydoc

# for example.py
sys.path.insert(0, os.path.abspath("."))
Expand Down
1 change: 0 additions & 1 deletion doc/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,3 @@ def foo(var1, var2, *args, long_var_name="hi", only_seldom_used_keyword=0, **kwa
# separate following codes (according to PEP257).
# But for function, method and module, there should be no blank lines
# after closing the docstring.
pass
3 changes: 2 additions & 1 deletion numpydoc/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import argparse
import ast
from collections.abc import Sequence
from pathlib import Path
from typing import List, Sequence, Union
from typing import List, Union

from .docscrape_sphinx import get_doc_object
from .hooks import utils, validate_docstrings
Expand Down
15 changes: 6 additions & 9 deletions numpydoc/docscrape.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
"""Extract reference documentation from the NumPy source tree.
"""
"""Extract reference documentation from the NumPy source tree."""

import copy
import inspect
import textwrap
import re
import pydoc
from warnings import warn
import re
import sys
import textwrap
from collections import namedtuple
from collections.abc import Callable, Mapping
import copy
import sys

from functools import cached_property
from warnings import warn


def strip_blank_lines(l):
Expand Down
11 changes: 4 additions & 7 deletions numpydoc/docscrape_sphinx.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import re
import inspect
import textwrap
import pydoc
from collections.abc import Callable
import os
import pydoc
import re
import textwrap

from jinja2 import FileSystemLoader
from jinja2.sandbox import SandboxedEnvironment
import sphinx
from sphinx.jinja2glue import BuiltinTemplateLoader

from .docscrape import NumpyDocString, FunctionDoc, ClassDoc, ObjDoc
from .docscrape import ClassDoc, FunctionDoc, NumpyDocString, ObjDoc
from .docscrape import get_doc_object as get_doc_object_orig
from .xref import make_xref


IMPORT_MATPLOTLIB_RE = r"\b(import +matplotlib|from +matplotlib +import)\b"


Expand Down
4 changes: 2 additions & 2 deletions numpydoc/hooks/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import itertools
import os
from collections.abc import Sequence
from pathlib import Path
from typing import Sequence


def find_project_root(srcs: Sequence[str]):
Expand Down Expand Up @@ -31,7 +31,7 @@ def find_project_root(srcs: Sequence[str]):
`Black <https://github.com/psf/black/blob/main/src/black/files.py>`_.
"""
if not srcs:
return Path(".").resolve(), "current directory"
return Path().resolve(), "current directory"

common_path = Path(
os.path.commonpath([Path(src).expanduser().resolve() for src in srcs])
Expand Down
42 changes: 23 additions & 19 deletions numpydoc/numpydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,22 @@
"""

from copy import deepcopy
import re
import pydoc
import inspect
from collections.abc import Callable
import hashlib
import inspect
import itertools
import pydoc
import re
from collections.abc import Callable
from copy import deepcopy

from docutils.nodes import citation, Text, section, comment, reference, inline
import sphinx
from sphinx.addnodes import pending_xref, desc_content
from docutils.nodes import Text, citation, comment, inline, reference, section
from sphinx.addnodes import desc_content, pending_xref
from sphinx.util import logging
from sphinx.errors import ExtensionError

from . import __version__
from .docscrape_sphinx import get_doc_object
from .validate import validate, ERROR_MSGS, get_validation_checks
from .validate import get_validation_checks, validate
from .xref import DEFAULT_LINKS
from . import __version__

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -248,10 +246,10 @@ def mangle_signature(app, what, name, obj, options, sig, retann):
return "", ""

if not (isinstance(obj, Callable) or hasattr(obj, "__argspec_is_invalid_")):
return
return None

if not hasattr(obj, "__doc__"):
return
return None
doc = get_doc_object(obj, config={"show_class_members": False})
sig = doc["Signature"] or _clean_text_signature(
getattr(obj, "__text_signature__", None)
Expand All @@ -275,7 +273,7 @@ def _clean_text_signature(sig):

def setup(app, get_doc_object_=get_doc_object):
if not hasattr(app, "add_config_value"):
return # probably called by nose, better bail out
return None # probably called by nose, better bail out

global get_doc_object
get_doc_object = get_doc_object_
Expand Down Expand Up @@ -417,12 +415,18 @@ def match_items(lines, content_old):
Examples
--------
>>> lines = ['', 'A', '', 'B', ' ', '', 'C', 'D']
>>> lines_old = ['a', '', '', 'b', '', 'c']
>>> items_old = [('file1.py', 0), ('file1.py', 1), ('file1.py', 2),
... ('file2.py', 0), ('file2.py', 1), ('file2.py', 2)]
>>> lines = ["", "A", "", "B", " ", "", "C", "D"]
>>> lines_old = ["a", "", "", "b", "", "c"]
>>> items_old = [
... ("file1.py", 0),
... ("file1.py", 1),
... ("file1.py", 2),
... ("file2.py", 0),
... ("file2.py", 1),
... ("file2.py", 2),
... ]
>>> content_old = ViewList(lines_old, items=items_old)
>>> match_items(lines, content_old) # doctest: +NORMALIZE_WHITESPACE
>>> match_items(lines, content_old) # doctest: +NORMALIZE_WHITESPACE
[('file1.py', 0), ('file1.py', 0), ('file2.py', 0), ('file2.py', 0),
('file2.py', 2), ('file2.py', 2), ('file2.py', 2), ('file2.py', 2)]
>>> # first 2 ``lines`` are matched to 'a', second 2 to 'b', rest to 'c'
Expand Down
3 changes: 0 additions & 3 deletions numpydoc/tests/hooks/example_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

def some_function(name):
"""Welcome to some function."""
pass


class MyClass:
Expand All @@ -23,11 +22,9 @@ def do_something(self, *args, **kwargs):
----------
*args
"""
pass

def process(self):
"""Process stuff."""
pass


class NewClass:
Expand Down
Loading

0 comments on commit acad4ec

Please sign in to comment.