From 67030bf2314456f08f06997271300a9fc9240f51 Mon Sep 17 00:00:00 2001 From: David Rodriguez Date: Wed, 17 Apr 2024 15:39:57 -0400 Subject: [PATCH 1/2] Updates to support using ruff for linting/formatting --- .github/workflows/test-package.yml | 8 ++++---- .pre-commit-config.yaml | 7 ++++++- pyproject.toml | 20 ++++++++++++++++++++ setup.cfg | 1 + 4 files changed, 31 insertions(+), 5 deletions(-) diff --git a/.github/workflows/test-package.yml b/.github/workflows/test-package.yml index a01522c..cfbbaab 100644 --- a/.github/workflows/test-package.yml +++ b/.github/workflows/test-package.yml @@ -29,12 +29,12 @@ jobs: pip install flake8 pytest pytest-astropy pytest-cov sphinx-astropy codecov # install package and requirements pip install ".[all]" - - name: Lint with flake8 + - name: Lint with ruff 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 + ruff check astrodbkit2 --select=E9,F63,F7,F82 --statistics + # exit-zero treats all errors as warnings. + ruff check astrodbkit2 --config=pyproject.toml --exit-zero - name: Test with pytest run: | pytest --cov --cov-config=setup.cfg --cov-report=term --cov-report=xml:coverage.xml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 24196a5..24f3768 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -7,4 +7,9 @@ repos: - repo: https://github.com/akaihola/darker rev: 1.7.2 hooks: - - id: darker \ No newline at end of file + - id: darker + args: + - --lint + - ruff check + additional_dependencies: + - ruff==0.3.7 \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index f0492be..e2ac6eb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,6 +9,26 @@ requires = ["setuptools", build-backend = 'setuptools.build_meta' +[tool.ruff.lint] +# Enable Pyflakes (`F`) and a subset of the pycodestyle (`E`) codes by default. +# Unlike Flake8, Ruff doesn't enable pycodestyle warnings (`W`) or +# McCabe complexity (`C901`) by default. +# See https://docs.astral.sh/ruff/rules/ +select = ["E4", "E7", "E9", "F", "I", "PL", "C901", "RUF010"] +#select = ["ALL"] +ignore = ["PLR"] # pylint refactor + +# Allow fix for all enabled rules (when `--fix`) is provided. +fixable = ["ALL"] +unfixable = [] + +[tool.ruff] +line-length = 120 + +[tool.ruff.lint.mccabe] +# Flag errors (`C901`) whenever the complexity level exceeds 15. +max-complexity = 15 + [tool.darker] line-length = 120 diff --git a/setup.cfg b/setup.cfg index f4c7097..3af1bc8 100644 --- a/setup.cfg +++ b/setup.cfg @@ -39,6 +39,7 @@ test = darker==1.7.2 black==23.9.1 pre-commit==3.4.0 + ruff==0.3.7 docs = sphinx-astropy From 32cc175aa8f991b323cd138a771c152a9fea9b9c Mon Sep 17 00:00:00 2001 From: David Rodriguez Date: Wed, 17 Apr 2024 16:01:57 -0400 Subject: [PATCH 2/2] More ruff settings and fixes --- astrodbkit2/astrodb.py | 19 ++++++++++--------- astrodbkit2/schema_example.py | 4 +++- astrodbkit2/spectra.py | 7 ++++--- astrodbkit2/tests/test_astrodb.py | 25 ++++++++++++++----------- astrodbkit2/tests/test_spectra.py | 7 ++++--- astrodbkit2/tests/test_utils.py | 7 +++++-- astrodbkit2/tests/test_views.py | 4 ++-- astrodbkit2/utils.py | 3 ++- pyproject.toml | 5 ++++- 9 files changed, 48 insertions(+), 33 deletions(-) diff --git a/astrodbkit2/astrodb.py b/astrodbkit2/astrodb.py index c367485..c705958 100644 --- a/astrodbkit2/astrodb.py +++ b/astrodbkit2/astrodb.py @@ -2,24 +2,25 @@ __all__ = ["__version__", "Database", "or_", "and_", "create_database"] -import os import json +import os import sqlite3 + import numpy as np import pandas as pd +import sqlalchemy.types as sqlalchemy_types +from astropy.coordinates import SkyCoord from astropy.table import Table as AstropyTable from astropy.units.quantity import Quantity -from astropy.coordinates import SkyCoord -from sqlalchemy.orm import sessionmaker, declarative_base -from sqlalchemy.orm.query import Query +from sqlalchemy import Table, and_, create_engine, event, or_, text from sqlalchemy.engine import Engine -import sqlalchemy.types as sqlalchemy_types -from sqlalchemy import event, create_engine, Table -from sqlalchemy import or_, and_, text +from sqlalchemy.orm import declarative_base, sessionmaker +from sqlalchemy.orm.query import Query from tqdm import tqdm -from . import REFERENCE_TABLES, PRIMARY_TABLE, PRIMARY_TABLE_KEY, FOREIGN_KEY -from .utils import json_serializer, get_simbad_names, deprecated_alias, datetime_json_parser + +from . import FOREIGN_KEY, PRIMARY_TABLE, PRIMARY_TABLE_KEY, REFERENCE_TABLES from .spectra import load_spectrum +from .utils import datetime_json_parser, deprecated_alias, get_simbad_names, json_serializer try: from .version import version as __version__ diff --git a/astrodbkit2/schema_example.py b/astrodbkit2/schema_example.py index 0233397..533ad64 100644 --- a/astrodbkit2/schema_example.py +++ b/astrodbkit2/schema_example.py @@ -3,9 +3,11 @@ # pylint: disable=unused-argument, unused-import import enum + import sqlalchemy as sa -from sqlalchemy import Boolean, Column, Float, ForeignKey, Integer, String, BigInteger, Enum, Date, DateTime +from sqlalchemy import Boolean, Column, Enum, Float, ForeignKey, String from sqlalchemy.orm import validates + from astrodbkit2.astrodb import Base from astrodbkit2.views import view diff --git a/astrodbkit2/spectra.py b/astrodbkit2/spectra.py index 761df07..7174184 100644 --- a/astrodbkit2/spectra.py +++ b/astrodbkit2/spectra.py @@ -1,15 +1,16 @@ """Functions to handle loading of spectrum objects""" import os -import numpy as np + import astropy.units as u -from astropy.wcs import WCS +import numpy as np from astropy.io import fits from astropy.nddata import StdDevUncertainty from astropy.units import Unit +from astropy.wcs import WCS from specutils import Spectrum1D -from specutils.io.registers import data_loader from specutils.io.parsing_utils import read_fileobj_or_hdulist +from specutils.io.registers import data_loader # pylint: disable=no-member, unused-argument diff --git a/astrodbkit2/tests/test_astrodb.py b/astrodbkit2/tests/test_astrodb.py index 4973dcc..40cf76a 100644 --- a/astrodbkit2/tests/test_astrodb.py +++ b/astrodbkit2/tests/test_astrodb.py @@ -1,19 +1,22 @@ # Testing for astrodb -import os -import json -import pytest import io +import json +import os + import pandas as pd +import pytest import sqlalchemy as sa -from sqlalchemy.exc import IntegrityError -from astropy.table import Table from astropy.coordinates import SkyCoord -from astropy.units.quantity import Quantity from astropy.io import ascii -from astrodbkit2.astrodb import Database, create_database, Base, copy_database_schema -from astrodbkit2.views import view +from astropy.table import Table +from astropy.units.quantity import Quantity +from sqlalchemy.exc import IntegrityError + +from astrodbkit2.astrodb import Database, copy_database_schema, create_database from astrodbkit2.schema_example import * +from astrodbkit2.views import view + try: import mock except ImportError: @@ -26,7 +29,7 @@ def test_nodatabase(): connection_string = 'sqlite:///:memory:' with pytest.raises(RuntimeError, match='Create database'): - db = Database(connection_string) + _ = Database(connection_string) @pytest.fixture(scope="module") @@ -144,9 +147,9 @@ def test_orm_use(db): # Adding a source with problematic ra/dec to test validation with pytest.raises(ValueError): - s2 = Sources(source="V4046 Sgr", ra=9999, dec=-32.79, reference="Schm10") + _ = Sources(source="V4046 Sgr", ra=9999, dec=-32.79, reference="Schm10") with pytest.raises(ValueError): - s2 = Sources(source="V4046 Sgr", ra=273.54, dec=-9999, reference="Schm10") + _ = Sources(source="V4046 Sgr", ra=273.54, dec=-9999, reference="Schm10") def test_add_table_data(db): diff --git a/astrodbkit2/tests/test_spectra.py b/astrodbkit2/tests/test_spectra.py index 44a7a6e..4e6bf04 100644 --- a/astrodbkit2/tests/test_spectra.py +++ b/astrodbkit2/tests/test_spectra.py @@ -1,15 +1,16 @@ # Tests for spectra functions -import pytest import numpy as np +import pytest from astropy.io import fits from astropy.units import Unit + from astrodbkit2.spectra import ( - identify_spex_prism, _identify_spex, + identify_spex_prism, + identify_wcs1d_multispec, load_spectrum, spex_prism_loader, - identify_wcs1d_multispec, wcs1d_multispec_loader, ) diff --git a/astrodbkit2/tests/test_utils.py b/astrodbkit2/tests/test_utils.py index ed6e6ca..e5240d9 100644 --- a/astrodbkit2/tests/test_utils.py +++ b/astrodbkit2/tests/test_utils.py @@ -1,12 +1,15 @@ # Testing for utils -import pytest import json from datetime import datetime from decimal import Decimal from io import StringIO + +import pytest from astropy.table import Table -from astrodbkit2.utils import json_serializer, get_simbad_names, _name_formatter, datetime_json_parser + +from astrodbkit2.utils import _name_formatter, datetime_json_parser, get_simbad_names, json_serializer + try: import mock except ImportError: diff --git a/astrodbkit2/tests/test_views.py b/astrodbkit2/tests/test_views.py index 1538c2c..e554a22 100644 --- a/astrodbkit2/tests/test_views.py +++ b/astrodbkit2/tests/test_views.py @@ -2,8 +2,8 @@ # Adapted from https://github.com/sqlalchemy/sqlalchemy/wiki/Views import sqlalchemy as sa -from sqlalchemy.orm import declarative_base -from sqlalchemy.orm import Session +from sqlalchemy.orm import Session, declarative_base + from astrodbkit2.views import * diff --git a/astrodbkit2/utils.py b/astrodbkit2/utils.py index 7b1c16b..974117c 100644 --- a/astrodbkit2/utils.py +++ b/astrodbkit2/utils.py @@ -1,10 +1,11 @@ """Utility functions for Astrodbkit2""" -import re import functools +import re import warnings from datetime import datetime from decimal import Decimal + from astroquery.simbad import Simbad __all__ = ["json_serializer", "get_simbad_names"] diff --git a/pyproject.toml b/pyproject.toml index e2ac6eb..164f5c9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -16,7 +16,10 @@ build-backend = 'setuptools.build_meta' # See https://docs.astral.sh/ruff/rules/ select = ["E4", "E7", "E9", "F", "I", "PL", "C901", "RUF010"] #select = ["ALL"] -ignore = ["PLR"] # pylint refactor +ignore = ["PLR", # pylint refactor + "F403", # star imports used + "F405", # referring to import from star imports + ] # Allow fix for all enabled rules (when `--fix`) is provided. fixable = ["ALL"]