From 1215f9e0bf4487bc2679815130193ae5f2f50c3e Mon Sep 17 00:00:00 2001 From: James Stevenson Date: Wed, 5 Jun 2024 10:54:48 -0400 Subject: [PATCH] style: format with Ruff --- .github/workflows/python-package.yml | 52 ++++++++++--------------- .pre-commit-config.yaml | 6 +++ Makefile | 4 +- docs/conf.py | 58 +++++++++++++++------------- docs/contributing.rst | 16 ++++---- docs/rst_epilog | 2 +- pyproject.toml | 11 +++--- sbin/generate_parser.py | 14 ++++--- setup.cfg | 4 +- setup.py | 1 + src/hgvs/alignmentmapper.py | 1 - src/hgvs/config.py | 1 + src/hgvs/dataproviders/interface.py | 4 +- src/hgvs/dataproviders/seqfetcher.py | 4 +- src/hgvs/edit.py | 3 +- src/hgvs/exceptions.py | 4 +- src/hgvs/extras/babelfish.py | 1 + src/hgvs/hgvsposition.py | 4 +- src/hgvs/normalizer.py | 4 +- src/hgvs/posedit.py | 4 +- src/hgvs/sequencevariant.py | 2 +- src/hgvs/utils/cigarmapper.py | 1 - src/hgvs/validator.py | 4 +- src/hgvs/variantmapper.py | 4 +- tests/issues/test_525.py | 1 + tests/support/mock_input_source.py | 1 + tests/test_hgvs_dataproviders_uta.py | 2 +- tests/test_hgvs_extras_babelfish.py | 10 +++-- tests/test_hgvs_grammar_full.py | 1 + 29 files changed, 111 insertions(+), 113 deletions(-) create mode 100644 .pre-commit-config.yaml diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 011055c0..628a8ecf 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -5,37 +5,27 @@ on: pull_request: jobs: - # codechecks: - # runs-on: ubuntu-latest - - # steps: - # - uses: actions/checkout@v3 - - # - name: Set up Python - # uses: actions/setup-python@v4 - # with: - # python-version: "3.10" - # cache: "pip" - # cache-dependency-path: "**/setup.cfg" - - # - name: Install dependencies - # run: | - # pip install -e .[dev] - - # - name: Lint with flake8 - # run: | - # # stop the build if there are Python syntax errors or undefined names - # flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics - # # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide - # flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics - - # - name: Format check with isort - # run: | - # isort --check src - - # - name: Format check with black - # run: | - # black --check src + cqa: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: "3.10" + cache: pip + cache-dependency-path: '**/pyproject.yaml' + + - name: Install test dependencies + run: | + python -m pip install --upgrade pip + pip install --use-deprecated=legacy-resolver -e .[dev] + + - name: Format check with Ruff + run: | + ruff format --check . test: runs-on: ubuntu-latest diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 00000000..f1a8b656 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,6 @@ +repos: +- repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.4.4 + hooks: + - id: ruff-format + args: [ --check ] diff --git a/Makefile b/Makefile index 1d439f76..765f7e76 100644 --- a/Makefile +++ b/Makefile @@ -107,9 +107,9 @@ tox: .PHONY: reformat reformat: @if ! git diff --cached --exit-code >/dev/null; then echo "Repository not clean" 1>&2; exit 1; fi - black src tests + ruff format src tests isort src tests - git commit -a -m "reformatted with black and isort" + git commit -a -m "reformatted with ruff and isort" #=> docs -- make sphinx docs .PHONY: docs diff --git a/docs/conf.py b/docs/conf.py index 773884a7..e4d41b37 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -6,55 +6,61 @@ try: # get release from hgvs if available import hgvs + release = str(hgvs.__version__) except ModuleNotFoundError: # else get release from first tag (raise exception if not available) import subprocess + release = subprocess.check_output(["git", "describe", "--tags"]).decode().strip().split()[0] version = re.sub(r"\.post\d+$", "", release) -project = u'HGVS' -authors = project + ' Contributors' -copyright = u'2021, ' + authors +project = "HGVS" +authors = project + " Contributors" +copyright = "2021, " + authors -extlinks = {'issue': ('https://github.com/biocommons/hgvs/issues/%s', 'issue '), } +extlinks = { + "issue": ("https://github.com/biocommons/hgvs/issues/%s", "issue "), +} -man_pages = [('index', project, project + u' Documentation', [project + u' Contributors'], 1)] +man_pages = [("index", project, project + " Documentation", [project + " Contributors"], 1)] # even for local builds -html_theme = 'sphinx_rtd_theme' +html_theme = "sphinx_rtd_theme" # # Boilerplate -autodoc_default_flags = ['members', 'undoc-members', 'show-inheritance'] #, 'inherited-members'] -autoclass_content = 'both' -exclude_patterns = ['build', 'static', 'templates', 'themes'] +autodoc_default_flags = ["members", "undoc-members", "show-inheritance"] # , 'inherited-members'] +autoclass_content = "both" +exclude_patterns = ["build", "static", "templates", "themes"] extensions = [ - 'sphinx.ext.autodoc', - 'sphinx.ext.autosummary', - 'sphinx.ext.coverage', - 'sphinx.ext.extlinks', - 'sphinx.ext.intersphinx', - 'sphinx.ext.todo', - 'sphinx.ext.viewcode', + "sphinx.ext.autodoc", + "sphinx.ext.autosummary", + "sphinx.ext.coverage", + "sphinx.ext.extlinks", + "sphinx.ext.intersphinx", + "sphinx.ext.todo", + "sphinx.ext.viewcode", ] -html_favicon = 'static/favicon.ico' -html_logo = 'static/hgvs-logo.png' -html_static_path = ['static'] -html_title = '{project} {release}'.format(project=project, release=release) -intersphinx_mapping = {'http://docs.python.org/': None, } -master_doc = 'index' -pygments_style = 'sphinx' -source_suffix = '.rst' -templates_path = ['templates'] +html_favicon = "static/favicon.ico" +html_logo = "static/hgvs-logo.png" +html_static_path = ["static"] +html_title = "{project} {release}".format(project=project, release=release) +intersphinx_mapping = { + "http://docs.python.org/": None, +} +master_doc = "index" +pygments_style = "sphinx" +source_suffix = ".rst" +templates_path = ["templates"] # rst_epilog is appended to all rst files # it's a good place to define global aliases # If ends in .rst, sphinx will append it to itself :-( -rst_epilog_fn = os.path.join(os.path.dirname(__file__), 'rst_epilog') +rst_epilog_fn = os.path.join(os.path.dirname(__file__), "rst_epilog") rst_epilog = open(rst_epilog_fn).read() # diff --git a/docs/contributing.rst b/docs/contributing.rst index f35a01fc..ab832857 100644 --- a/docs/contributing.rst +++ b/docs/contributing.rst @@ -35,7 +35,7 @@ Highlights close to it). * Abide by current `code style`_. Use ``make reformat`` to reformat all - code with `yapf`_ prior to submitting a PR. + code with `ruff`_ prior to submitting a PR. * Email the `hgvs-discuss`_ mailing list if you have questions. @@ -158,11 +158,11 @@ setting UTA_DB_URL. The format is ``postgresql://:@//``. For example: ``postgresql://anonymous:anonymous@uta.biocommons.org/uta/uta_20140210`` - -explicitly selects the public database, and + +explicitly selects the public database, and ``postgresql://localhost/uta/uta_20140210`` - + selects a local instance. Developers can test connectivity like this: ``$ UTA_DB_URL=postgresql://localhost/uta/uta_20140210 make test-quick`` @@ -215,7 +215,7 @@ changes:: spaces_before_comment = 4 split_before_named_assigns = True -These code conventions are uniformly enforce by yapf_. The entire code +These code conventions are uniformly enforce by ruff_. The entire code base is periodically automatically reformatted for consistency. @@ -232,13 +232,13 @@ understand the code. variable names. |eg| ``var_c`` in a function argument list signifies that a SequenceVariant object with type='c' is expected. -:hgvs*: a string representing an HGVS variant name. +:hgvs*: a string representing an HGVS variant name. :var*: a :class:`hgvs.variant.SequenceVariant` object -:pos: +:pos: -:posedit: +:posedit: :hgvs_position: diff --git a/docs/rst_epilog b/docs/rst_epilog index 7e25de92..5b01c510 100644 --- a/docs/rst_epilog +++ b/docs/rst_epilog @@ -17,7 +17,7 @@ .. _peg: http://en.wikipedia.org/wiki/Parsing_expression_grammar .. _pep8: https://www.python.org/dev/peps/pep-0008/ .. _uta: https://github.com/biocommons/uta/ -.. _yapf: https://github.com/google/yapf +.. _ruff: https://docs.astral.sh/ruff/ .. |eg| replace:: *e.g.,* diff --git a/pyproject.toml b/pyproject.toml index 8d96944e..1049b25d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -103,10 +103,6 @@ exclude_lines = [ "if __name__ == .__main__.:", ] -[tool.black] -line-length = 100 - - # [tool.flake8] # flake8 does not support configuration in pyproject.toml # https://github.com/PyCQA/flake8/issues/234#issuecomment-812800832 @@ -120,4 +116,9 @@ disable = "R0913" [tool.pylint.format] -max-line-length = "120" +max-line-length = "100" + + +[tool.ruff] +src = ["src", "test"] +line-length = 100 diff --git a/sbin/generate_parser.py b/sbin/generate_parser.py index 0ea5465f..0c7254d2 100755 --- a/sbin/generate_parser.py +++ b/sbin/generate_parser.py @@ -18,7 +18,7 @@ grammar_file = os.path.join(hgvs_base_dir, "src/hgvs/_data/hgvs.pymeta") generated_code_dir = os.path.join(hgvs_base_dir, "src/hgvs/generated") - grammar_hash = hashlib.md5(open(grammar_file, 'rb').read()).hexdigest() + grammar_hash = hashlib.md5(open(grammar_file, "rb").read()).hexdigest() prefix_length = len(hgvs_base_dir) + 1 # extra to also remove slash header_template = """# -------------------------------------------------- @@ -31,11 +31,13 @@ # Python version: {python_version} # -------------------------------------------------- """ - header = header_template.format(generate_script=script_path[prefix_length:], - grammar_file=grammar_file[prefix_length:], - grammar_hash=grammar_hash, - parsley_version=parsley.__version__, - python_version=sys.version) + header = header_template.format( + generate_script=script_path[prefix_length:], + grammar_file=grammar_file[prefix_length:], + grammar_hash=grammar_hash, + parsley_version=parsley.__version__, + python_version=sys.version, + ) g = OMeta(open(grammar_file).read(), name="Grammar") tree = g.parseGrammar("Grammar") diff --git a/setup.cfg b/setup.cfg index 88a99313..044b73f3 100644 --- a/setup.cfg +++ b/setup.cfg @@ -28,22 +28,22 @@ hgvs = [options.extras_require] dev = - black flake8 ipython isort jupyter + pre-commit ~= 3.4 pytest >= 5.3 pytest-cov >= 2.8 pytest-recording restview + ruff == 0.4.4 setuptools sphinx sphinx_rtd_theme sphinxcontrib-fulltoc >= 1.1 tox vcrpy - yapf [aliases] diff --git a/setup.py b/setup.py index 460aabeb..d5d43d7c 100644 --- a/setup.py +++ b/setup.py @@ -1,2 +1,3 @@ from setuptools import setup + setup(use_scm_version=True) diff --git a/src/hgvs/alignmentmapper.py b/src/hgvs/alignmentmapper.py index 72396bd5..1b1a2100 100644 --- a/src/hgvs/alignmentmapper.py +++ b/src/hgvs/alignmentmapper.py @@ -26,7 +26,6 @@ # g. ... 123 124 125 126 127 128 129 130 131 132 133 ... # - from bioutils.coordinates import strand_int_to_pm from six.moves import range diff --git a/src/hgvs/config.py b/src/hgvs/config.py index ff05ac16..9e2df36b 100644 --- a/src/hgvs/config.py +++ b/src/hgvs/config.py @@ -31,6 +31,7 @@ import re from configparser import ConfigParser, ExtendedInterpolation from copy import copy + try: from importlib.resources import files as resources_files except ImportError: diff --git a/src/hgvs/dataproviders/interface.py b/src/hgvs/dataproviders/interface.py index 838ba080..9cb50bdb 100644 --- a/src/hgvs/dataproviders/interface.py +++ b/src/hgvs/dataproviders/interface.py @@ -1,7 +1,5 @@ # -*- coding: utf-8 -*- -"""Defines the abstract data provider interface - -""" +"""Defines the abstract data provider interface""" import abc import os diff --git a/src/hgvs/dataproviders/seqfetcher.py b/src/hgvs/dataproviders/seqfetcher.py index 5564205c..4f22d3c9 100644 --- a/src/hgvs/dataproviders/seqfetcher.py +++ b/src/hgvs/dataproviders/seqfetcher.py @@ -1,7 +1,5 @@ # -*- coding: utf-8 -*- -"""provides sequencing fetching from NCBI and Ensembl - -""" +"""provides sequencing fetching from NCBI and Ensembl""" import logging import os diff --git a/src/hgvs/edit.py b/src/hgvs/edit.py index 0b107c5a..3de25f96 100644 --- a/src/hgvs/edit.py +++ b/src/hgvs/edit.py @@ -8,6 +8,7 @@ location). """ + import abc import attr @@ -50,7 +51,7 @@ def _del_ins_lengths(self, ilen): @property @abc.abstractmethod def type(self): - """ return the type of this Edit """ + """return the type of this Edit""" pass diff --git a/src/hgvs/exceptions.py b/src/hgvs/exceptions.py index c5d21040..e60ea12e 100644 --- a/src/hgvs/exceptions.py +++ b/src/hgvs/exceptions.py @@ -1,7 +1,5 @@ # -*- coding: utf-8 -*- -"""defined exceptions used by the hgvs package - -""" +"""defined exceptions used by the hgvs package""" class HGVSError(Exception): diff --git a/src/hgvs/extras/babelfish.py b/src/hgvs/extras/babelfish.py index 9304f206..c8a140c3 100644 --- a/src/hgvs/extras/babelfish.py +++ b/src/hgvs/extras/babelfish.py @@ -1,4 +1,5 @@ """translate between HGVS and other formats""" + import os from bioutils.assemblies import make_ac_name_map, make_name_ac_map diff --git a/src/hgvs/hgvsposition.py b/src/hgvs/hgvsposition.py index 78742213..324f324e 100644 --- a/src/hgvs/hgvsposition.py +++ b/src/hgvs/hgvsposition.py @@ -1,7 +1,5 @@ # -*- coding: utf-8 -*- -"""Represent partial HGVS tags that refer to a position without alleles - -""" +"""Represent partial HGVS tags that refer to a position without alleles""" import attr diff --git a/src/hgvs/normalizer.py b/src/hgvs/normalizer.py index 557de771..11f75b3e 100644 --- a/src/hgvs/normalizer.py +++ b/src/hgvs/normalizer.py @@ -1,6 +1,5 @@ # -*- coding: utf-8 -*- -"""hgvs.normalizer -""" +"""hgvs.normalizer""" import copy import logging @@ -444,6 +443,7 @@ def _normalize_alleles(self, var, boundary): return base + start, base + stop, (ref, alt) + # # Copyright 2018 HGVS Contributors (https://github.com/biocommons/hgvs) # diff --git a/src/hgvs/posedit.py b/src/hgvs/posedit.py index a7618230..3fc7ba2c 100644 --- a/src/hgvs/posedit.py +++ b/src/hgvs/posedit.py @@ -1,7 +1,5 @@ # -*- coding: utf-8 -*- -"""implements a (position,edit) tuple that represents a localized sequence change - -""" +"""implements a (position,edit) tuple that represents a localized sequence change""" import attr diff --git a/src/hgvs/sequencevariant.py b/src/hgvs/sequencevariant.py index 86e1aad7..3bde9093 100644 --- a/src/hgvs/sequencevariant.py +++ b/src/hgvs/sequencevariant.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -""" represents simple sequence-based variants """ +"""represents simple sequence-based variants""" import attr diff --git a/src/hgvs/utils/cigarmapper.py b/src/hgvs/utils/cigarmapper.py index 4708a377..e782563b 100644 --- a/src/hgvs/utils/cigarmapper.py +++ b/src/hgvs/utils/cigarmapper.py @@ -15,7 +15,6 @@ """ - import re from hgvs.exceptions import HGVSInvalidIntervalError diff --git a/src/hgvs/validator.py b/src/hgvs/validator.py index bfaefece..dd31eff8 100644 --- a/src/hgvs/validator.py +++ b/src/hgvs/validator.py @@ -1,7 +1,5 @@ # -*- coding: utf-8 -*- -"""implements validation of hgvs variants - -""" +"""implements validation of hgvs variants""" import logging diff --git a/src/hgvs/variantmapper.py b/src/hgvs/variantmapper.py index d73b98b7..39aa6fbe 100644 --- a/src/hgvs/variantmapper.py +++ b/src/hgvs/variantmapper.py @@ -1,7 +1,5 @@ # -*- coding: utf-8 -*- -"""Projects variants between sequences using AlignmentMapper. - -""" +"""Projects variants between sequences using AlignmentMapper.""" import copy import logging diff --git a/tests/issues/test_525.py b/tests/issues/test_525.py index 98ecdc6f..93d91c32 100644 --- a/tests/issues/test_525.py +++ b/tests/issues/test_525.py @@ -1,4 +1,5 @@ """https://github.com/biocommons/hgvs/issues/525""" + import pytest diff --git a/tests/support/mock_input_source.py b/tests/support/mock_input_source.py index 90a2dd6a..ee75fc13 100644 --- a/tests/support/mock_input_source.py +++ b/tests/support/mock_input_source.py @@ -76,6 +76,7 @@ def _read_input(self, in_file): return result + # # Copyright 2018 HGVS Contributors (https://github.com/biocommons/hgvs) # diff --git a/tests/test_hgvs_dataproviders_uta.py b/tests/test_hgvs_dataproviders_uta.py index e0b5fc68..854c777e 100644 --- a/tests/test_hgvs_dataproviders_uta.py +++ b/tests/test_hgvs_dataproviders_uta.py @@ -66,7 +66,7 @@ def test_get_tx_exons_invalid_alt_aln_method(self): def test_get_tx_for_gene(self): tig = self.hdp.get_tx_for_gene("VHL") - + expected_data = [ ("NM_001354723.1", "NC_000003.11"), ("NM_198156.2", "NC_018914.2"), diff --git a/tests/test_hgvs_extras_babelfish.py b/tests/test_hgvs_extras_babelfish.py index c0d2d1c5..0a16b000 100644 --- a/tests/test_hgvs_extras_babelfish.py +++ b/tests/test_hgvs_extras_babelfish.py @@ -8,10 +8,12 @@ "NC_000006.12:g.49949407=", [], ("6", 49949407, "A", ".", "identity"), - [("6", 49949407, "A", "A", "identity"), - # Test case insensitivity - ("6", 49949407, "A", "a", "identity"), - ("6", 49949407, "a", "A", "identity"),] + [ + ("6", 49949407, "A", "A", "identity"), + # Test case insensitivity + ("6", 49949407, "A", "a", "identity"), + ("6", 49949407, "a", "A", "identity"), + ], ), # Test multi-base identity ( diff --git a/tests/test_hgvs_grammar_full.py b/tests/test_hgvs_grammar_full.py index 5c55c4c6..3c97d08e 100644 --- a/tests/test_hgvs_grammar_full.py +++ b/tests/test_hgvs_grammar_full.py @@ -4,6 +4,7 @@ import re import unittest from sys import version_info + try: from importlib.resources import files as resources_files except ImportError: