Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add linting with ruff and add workflow for black + ruff #188

Merged
merged 6 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 0 additions & 19 deletions .github/workflows/flake8.yml

This file was deleted.

11 changes: 11 additions & 0 deletions .github/workflows/python-linting.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: Check Python formatting using Black and Ruff

on: [push]

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: psf/black@stable
- uses: chartboost/ruff-action@v1
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ repos:
hooks:
- id: black
language_version: python3.10

- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.270
hooks:
- id: ruff
args: [--exit-non-zero-on-fix]
119 changes: 119 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,122 @@ exclude = '''
| docs
)/
'''

# Copy ruff settings from pandas
[tool.ruff]
line-length = 100
target-version = "py310"
# fix = true
unfixable = []

select = [
# isort
"I",
# pyflakes
"F",
# pycodestyle
"E", "W",
# flake8-2020
"YTT",
# flake8-bugbear
"B",
# flake8-quotes
"Q",
# flake8-debugger
"T10",
# flake8-gettext
"INT",
# pylint
"PLC", "PLE", "PLR", "PLW",
# misc lints
"PIE",
# flake8-pyi
"PYI",
# tidy imports
"TID",
# implicit string concatenation
"ISC",
# type-checking imports
"TCH",
# comprehensions
"C4",
# pygrep-hooks
"PGH"
]

ignore = [
# space before : (needed for how black formats slicing)
# "E203", # not yet implemented
# module level import not at top of file
"E402",
# do not assign a lambda expression, use a def
"E731",
# line break before binary operator
# "W503", # not yet implemented
# line break after binary operator
# "W504", # not yet implemented
# controversial
"B006",
# controversial?: Loop control variable not used within loop body
# "B007",
# controversial
"B008",
# setattr is used to side-step mypy
"B009",
# getattr is used to side-step mypy
"B010",
# tests use assert False
"B011",
# tests use comparisons but not their returned value
"B015",
# false positives
"B019",
# Loop control variable overrides iterable it iterates
"B020",
# Function definition does not bind loop variable
"B023",
# No explicit `stacklevel` keyword argument found
"B028",
# Functions defined inside a loop must not use variables redefined in the loop
# "B301", # not yet implemented
# Only works with python >=3.10
"B905",
# Too many arguments to function call
"PLR0913",
# Too many returns
"PLR0911",
# Too many branches
"PLR0912",
# Too many statements
"PLR0915",
# Redefined loop name
"PLW2901",
# Global statements are discouraged
"PLW0603",
# Docstrings should not be included in stubs
"PYI021",
# No builtin `eval()` allowed
"PGH001",
# compare-to-empty-string
"PLC1901",
# Use typing_extensions.TypeAlias for type aliases
# "PYI026", # not yet implemented
# Use "collections.abc.*" instead of "typing.*" (PEP 585 syntax)
# "PYI027", # not yet implemented
# while int | float can be shortened to float, the former is more explicit
# "PYI041", # not yet implemented

# Additional checks that don't pass yet
# Useless statement
"B018",
# Within an except clause, raise exceptions with ...
"B904",
# Magic number
"PLR2004",
# Consider `elif` instead of `else` then `if` to remove indentation level
"PLR5501",
]

exclude = [
"docs/",
]
33 changes: 18 additions & 15 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@
except ImportError:
cmdclass = {}

entry_points = {'console_scripts': ['sparkles=sparkles.core:main']}
entry_points = {"console_scripts": ["sparkles=sparkles.core:main"]}

setup(name='sparkles',
author='Tom Aldcroft',
description='Sparkles ACA review package',
author_email='taldcroft@cfa.harvard.edu',
use_scm_version=True,
setup_requires=['setuptools_scm', 'setuptools_scm_git_archive'],
zip_safe=False,
entry_points=entry_points,
packages=['sparkles', 'sparkles.tests'],
package_data={'sparkles': ['index_template*.html'],
'sparkles.tests': ['data/*pkl.gz']},
tests_require=['pytest'],
cmdclass=cmdclass,
)
setup(
name="sparkles",
author="Tom Aldcroft",
description="Sparkles ACA review package",
author_email="taldcroft@cfa.harvard.edu",
use_scm_version=True,
setup_requires=["setuptools_scm", "setuptools_scm_git_archive"],
zip_safe=False,
entry_points=entry_points,
packages=["sparkles", "sparkles.tests"],
package_data={
"sparkles": ["index_template*.html"],
"sparkles.tests": ["data/*pkl.gz"],
},
tests_require=["pytest"],
cmdclass=cmdclass,
)
2 changes: 1 addition & 1 deletion sparkles/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

__version__ = ska_helpers.get_version(__package__)

from .core import run_aca_review, ACAReviewTable # noqa
from .core import ACAReviewTable, run_aca_review # noqa: F401


def test(*args, **kwargs):
Expand Down
36 changes: 17 additions & 19 deletions sparkles/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,31 @@
"""
import gzip
import io
import pickle
import pprint
import re
import traceback
from itertools import chain, combinations
from pathlib import Path
import pickle
from itertools import combinations, chain
import pprint

import matplotlib

matplotlib.use("Agg") # noqa
matplotlib.use("Agg")
import chandra_aca
import matplotlib.pyplot as plt
from matplotlib.patches import Circle

import numpy as np
from jinja2 import Template
from chandra_aca.star_probs import guide_count
from chandra_aca.transform import yagzag_to_pixels, mag_to_count_rate, snr_mag_for_t_ccd
import chandra_aca
from astropy.table import Column, Table

import proseco
from proseco.catalog import ACATable
import proseco.characteristics as ACA
from astropy.table import Column, Table
from chandra_aca.star_probs import guide_count
from chandra_aca.transform import mag_to_count_rate, snr_mag_for_t_ccd, yagzag_to_pixels
from jinja2 import Template
from matplotlib.patches import Circle
from proseco.catalog import ACATable
from proseco.core import MetaAttribute

from .roll_optimize import RollOptimizeMixin


CACHE = {}
FILEDIR = Path(__file__).parent

Expand All @@ -51,10 +48,10 @@

def main(sys_args=None):
"""Command line interface to preview_load()"""
from . import __version__

import argparse

from . import __version__

parser = argparse.ArgumentParser(
description=f"Sparkles ACA review tool {__version__}"
)
Expand Down Expand Up @@ -514,7 +511,7 @@ def get_acas_from_pickle(load_name, loud=False):
:param load_name: load name
:param loud: print processing information
"""
if load_name.startswith(r"\\noodle") or load_name.startswith("https://occweb"):
if load_name.startswith((r"\\noodle", "https://occweb")):
acas_dict, path_name = get_acas_dict_from_occweb(load_name)
else:
acas_dict, path_name = get_acas_dict_from_local_file(load_name, loud)
Expand Down Expand Up @@ -1171,7 +1168,7 @@ def check_guide_geometry(self):
n_guide = len(guide_idxs)

if n_guide < 2:
msg = f"Cannot check geometry with fewer than 2 guide stars"
msg = "Cannot check geometry with fewer than 2 guide stars"
self.add_message("critical", msg)
return

Expand Down Expand Up @@ -1574,7 +1571,8 @@ def from_ocat(cls, obsid, t_ccd=-5, man_angle=5, date=None, roll=None, **kwargs)
:returns: AcaReviewTable object
"""
from proseco import get_aca_catalog
from .yoshi import get_yoshi_params_from_ocat, convert_yoshi_to_proseco_params

from .yoshi import convert_yoshi_to_proseco_params, get_yoshi_params_from_ocat

params_yoshi = get_yoshi_params_from_ocat(obsid, obs_date=date)
if roll is not None:
Expand Down
14 changes: 6 additions & 8 deletions sparkles/find_er_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,15 @@
from the star fields that appear to be the best based on available stars.
3. Prioritize by the order of attitudes passed in to the function.
"""
import agasc
import numpy as np
from astropy.table import Table, MaskedColumn

from chandra_aca.transform import radec_to_yagzag, snr_mag_for_t_ccd, yagzag_to_pixels
import proseco.characteristics_guide as GUIDE
import ska_sun
from astropy.table import MaskedColumn, Table
from chandra_aca.star_probs import guide_count
from chandra_aca.transform import radec_to_yagzag, snr_mag_for_t_ccd, yagzag_to_pixels
from proseco import get_aca_catalog
import agasc
from Quaternion import Quat
from chandra_aca.star_probs import guide_count
import ska_sun

from ska_sun import get_sun_pitch_yaw


Expand Down Expand Up @@ -317,7 +315,7 @@ def get_att_opts_table(acar, atts):
)
# Creating a numpy object array of empty lists requires this workaround
for ii in range(n_atts):
t["status"][ii] = list()
t["status"][ii] = []

for name in ["dpitch", "dyaw", "droll", "count_9th", "count_10th", "count_all"]:
t[name].info.format = ".2f"
Expand Down
17 changes: 8 additions & 9 deletions sparkles/roll_optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,22 @@
"""
Roll optimization during preliminary review of ACA catalogs.
"""
from copy import deepcopy
import numpy as np
import warnings
from copy import deepcopy

import numpy as np
import ska_sun
from astropy.table import Table, vstack
from chandra_aca.star_probs import acq_success_prob, guide_count
from chandra_aca.transform import (
radec_to_yagzag,
yagzag_to_pixels,
calc_aca_from_targ,
calc_targ_from_aca,
radec_to_yagzag,
yagzag_to_pixels,
)
from Quaternion import Quat
import ska_sun

from proseco.characteristics import CCD
from proseco import get_aca_catalog
from proseco.characteristics import CCD
from Quaternion import Quat


def logical_intervals(vals, x=None):
Expand Down Expand Up @@ -169,7 +168,7 @@ def get_roll_intervals(
def get_ids_list(roll_offsets):
ids_list = []

for ii, roll_offset in enumerate(roll_offsets):
for roll_offset in roll_offsets:
# Roll about the target attitude, which is offset from ACA attitude by a bit
att_targ_rolled = Quat(
[att_targ.ra, att_targ.dec, att_targ.roll + roll_offset]
Expand Down
2 changes: 1 addition & 1 deletion sparkles/tests/test_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from proseco.tests.test_common import DARK40, STD_INFO, mod_std_info
from Quaternion import Quat

from .. import ACAReviewTable
from sparkles import ACAReviewTable


def test_check_slice_index():
Expand Down
Loading