From 6f91543ec66a864c4d492bd495e1587e5634443b Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 25 Nov 2024 03:23:44 +0100 Subject: [PATCH] Format Python code with ruff format instead of black (#498) --- README.rst | 22 +++++--------- docs/changes/498.misc.rst | 1 + docs/conf.py | 60 +++++++++++++++++++++---------------- tests/test_astroid_utils.py | 16 +++------- tox.ini | 4 +-- 5 files changed, 49 insertions(+), 54 deletions(-) create mode 100644 docs/changes/498.misc.rst diff --git a/README.rst b/README.rst index 96049cec..044469fa 100644 --- a/README.rst +++ b/README.rst @@ -17,9 +17,9 @@ Sphinx AutoAPI :target: https://pypi.org/project/sphinx-autoapi/ :alt: Supported Python Versions -.. image:: https://img.shields.io/badge/code%20style-black-000000.svg - :target: https://github.com/python/black - :alt: Formatted with Black +.. image:: https://img.shields.io/badge/code%20style-ruff-000000.svg + :target: https://docs.astral.sh/ruff/ + :alt: Formatted with Ruff Sphinx AutoAPI is a Sphinx extension for generating complete API documentation without needing to load, run, or import the project being documented. @@ -77,27 +77,19 @@ Tests are executed through `tox `_. Code Style ~~~~~~~~~~ -Code is formatted using `black `_. +Code is formatted using `ruff `_. -You can check your formatting using black's check mode: +You can check your formatting using ruff format's check mode: .. code-block:: bash tox -e format -You can also get black to format your changes for you: +You can also get ruff to format your changes for you: .. code-block:: bash - black autoapi/ tests/ - -You can even get black to format changes automatically when you commit using `pre-commit `_: - - -.. code-block:: bash - - pip install pre-commit - pre-commit install + ruff format Release Notes ~~~~~~~~~~~~~ diff --git a/docs/changes/498.misc.rst b/docs/changes/498.misc.rst new file mode 100644 index 00000000..b7be414a --- /dev/null +++ b/docs/changes/498.misc.rst @@ -0,0 +1 @@ +Format Python code with ruff format instead of black. diff --git a/docs/conf.py b/docs/conf.py index 294c7ddc..e7ba3efc 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -13,9 +13,9 @@ # -- Project information ----------------------------------------------------- # https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information -project = 'Sphinx AutoAPI' -copyright = '2023, Read the Docs' -author = 'Read the Docs' +project = "Sphinx AutoAPI" +copyright = "2023, Read the Docs" +author = "Read the Docs" version = ".".join(str(x) for x in autoapi.__version_info__[:2]) release = autoapi.__version__ @@ -23,38 +23,39 @@ # https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration extensions = [ - 'autoapi.extension', - 'sphinx.ext.intersphinx', - 'sphinx.ext.napoleon', - 'sphinx_design', + "autoapi.extension", + "sphinx.ext.intersphinx", + "sphinx.ext.napoleon", + "sphinx_design", ] -templates_path = ['_templates'] -exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store', 'changes/*.rst'] +templates_path = ["_templates"] +exclude_patterns = ["_build", "Thumbs.db", ".DS_Store", "changes/*.rst"] # -- Options for HTML output ------------------------------------------------- # https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output -html_theme = 'furo' -html_static_path = ['_static'] -html_css_files = ['overrides.css'] +html_theme = "furo" +html_static_path = ["_static"] +html_css_files = ["overrides.css"] # -- Options for AutoAPI extension ------------------------------------------- -autoapi_dirs = ['../autoapi'] +autoapi_dirs = ["../autoapi"] autoapi_generate_api_docs = False # -- Options for intersphinx extension --------------------------------------- intersphinx_mapping = { - 'jinja': ('https://jinja.palletsprojects.com/en/3.0.x/', None), - 'sphinx': ('https://www.sphinx-doc.org/en/master/', None), - 'python': ('https://docs.python.org/3/', None), + "jinja": ("https://jinja.palletsprojects.com/en/3.0.x/", None), + "sphinx": ("https://www.sphinx-doc.org/en/master/", None), + "python": ("https://docs.python.org/3/", None), } # -- Enable confval and event roles ------------------------------------------ -event_sig_re = re.compile(r'([a-zA-Z-]+)\s*\((.*)\)') +event_sig_re = re.compile(r"([a-zA-Z-]+)\s*\((.*)\)") + def parse_event(env, sig, signode): m = event_sig_re.match(sig) @@ -64,7 +65,7 @@ def parse_event(env, sig, signode): name, args = m.groups() signode += addnodes.desc_name(name, name) plist = addnodes.desc_parameterlist() - for arg in args.split(','): + for arg in args.split(","): arg = arg.strip() plist += addnodes.desc_parameter(arg, arg) signode += plist @@ -72,10 +73,19 @@ def parse_event(env, sig, signode): def setup(app): - app.add_object_type('confval', 'confval', - objname='configuration value', - indextemplate='pair: %s; configuration value') - fdesc = TypedField('parameter', label='Parameters', - names=('param',), typenames=('type',), can_collapse=True) - app.add_object_type('event', 'event', 'pair: %s; event', parse_event, - doc_field_types=[fdesc]) + app.add_object_type( + "confval", + "confval", + objname="configuration value", + indextemplate="pair: %s; configuration value", + ) + fdesc = TypedField( + "parameter", + label="Parameters", + names=("param",), + typenames=("type",), + can_collapse=True, + ) + app.add_object_type( + "event", "event", "pair: %s; event", parse_event, doc_field_types=[fdesc] + ) diff --git a/tests/test_astroid_utils.py b/tests/test_astroid_utils.py index 0d2b15bf..ace4f68a 100644 --- a/tests/test_astroid_utils.py +++ b/tests/test_astroid_utils.py @@ -67,9 +67,7 @@ def test_can_get_full_imported_basename(self, import_, basename, expected): {} class ThisClass({}): #@ pass - """.format( - import_, basename - ) + """.format(import_, basename) node = astroid.extract_node(source) basenames = _astroid_utils.resolve_qualname(node.bases[0], node.basenames[0]) assert basenames == expected @@ -82,9 +80,7 @@ def test_can_get_full_function_basename(self, import_, basename, expected): {} class ThisClass({}): #@ pass - """.format( - import_, basename - ) + """.format(import_, basename) node = astroid.extract_node(source) basenames = _astroid_utils.resolve_qualname(node.bases[0], node.basenames[0]) assert basenames == expected @@ -163,9 +159,7 @@ def test_parse_annotations(self, signature, expected): """ def func({}) -> str: #@ pass - """.format( - signature - ) + """.format(signature) ) annotations = _astroid_utils.get_args_info(node.args) @@ -227,9 +221,7 @@ def test_format_args(self, signature, expected): """ def func({}) -> str: #@ pass - """.format( - signature - ) + """.format(signature) ) args_info = _astroid_utils.get_args_info(node.args) diff --git a/tox.ini b/tox.ini index e7372568..22517bb7 100644 --- a/tox.ini +++ b/tox.ini @@ -29,9 +29,9 @@ commands = [testenv:format] skip_install = true deps = - black + ruff commands = - black --check --diff autoapi tests + ruff format --check --diff [testenv:lint] skip_install = true