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

Apply basic code style checks to test configuration files #9146

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
9 changes: 3 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ repos:
jwst/persistence/.* |
jwst/photom/.* |
jwst/refpix/.* |
jwst/regtest/.* |
jwst/resample/.* |
jwst/reset/.* |
jwst/residual_fringe/.* |
Expand All @@ -82,9 +81,7 @@ repos:
jwst/tso_photometry/.* |
jwst/wfs_combine/.* |
jwst/white_light/.* |
jwst/conftest.py |
.*/tests/.* |
setup.py |
pytest_jwst/.* |
docs/.*
docs/.* |
jwst/regtest/test_.* |
.*/tests/test_.*
)$
22 changes: 10 additions & 12 deletions .ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ exclude = [
"build",
"*.ipynb",
"**/tests/test_*.py",
"**/regtest/test_*.py",
]
line-length = 100

Expand All @@ -18,10 +19,6 @@ quote-style = "double"
indent-style = "space"
docstring-code-format = true
exclude = [
"jwst/step.py",
"jwst/__init__.py",
"jwst/conftest.py",
"pytest_jwst/**.py",
"jwst/assign_wcs/**.py",
"jwst/associations/**.py",
"jwst/background/**.py",
Expand All @@ -44,7 +41,6 @@ exclude = [
"jwst/persistence/**.py",
"jwst/photom/**.py",
"jwst/refpix/**.py",
"jwst/regtest/**.py",
"jwst/resample/**.py",
"jwst/reset/**.py",
"jwst/residual_fringe/**.py",
Expand All @@ -55,7 +51,6 @@ exclude = [
"jwst/srctype/**.py",
"jwst/straylight/**.py",
"jwst/superbias/**.py",
"jwst/tests/**.py",
"jwst/tso_photometry/**.py",
"jwst/wfs_combine/**.py",
"jwst/white_light/**.py",
Expand Down Expand Up @@ -115,13 +110,18 @@ ignore-fully-untyped = true # Turn off annotation checking for fully untyped co
"**/tests/**.py" = [
"D104", # missing docstring in public package
]
"jwst/conftest.py" = [
"SLF", "ARG", "PTH",
"B003", # Assigning to os.environ does not clear the environment
]
"**/regtest/**.py" = [ # conftest, regtestdata, and sdp_pools_source
"SLF", "ARG", "PTH",
"A002", # builtin variable shadowing
"S101", # use of assert
]
"jwst/associations/tests*" = [
"F841", # unused variable
]
"jwst/step.py" = ["D", "N", "A", "ARG", "B", "C4", "ICN", "INP", "ISC", "LOG", "NPY", "PGH", "PTH", "S", "SLF", "SLOT", "T20", "TRY", "UP", "YTT", "E501"]
"jwst/__init__.py" = ["D", "N", "A", "ARG", "B", "C4", "ICN", "INP", "ISC", "LOG", "NPY", "PGH", "PTH", "S", "SLF", "SLOT", "T20", "TRY", "UP", "YTT", "E501"]
"jwst/conftest.py" = ["D", "N", "A", "ARG", "B", "C4", "ICN", "INP", "ISC", "LOG", "NPY", "PGH", "PTH", "S", "SLF", "SLOT", "T20", "TRY", "UP", "YTT", "E501"]
"pytest_jwst/**.py" = ["D", "N", "A", "ARG", "B", "C4", "ICN", "INP", "ISC", "LOG", "NPY", "PGH", "PTH", "S", "SLF", "SLOT", "T20", "TRY", "UP", "YTT", "E501"]
"jwst/assign_wcs/**.py" = ["D", "N", "A", "ARG", "B", "C4", "ICN", "INP", "ISC", "LOG", "NPY", "PGH", "PTH", "S", "SLF", "SLOT", "T20", "TRY", "UP", "YTT", "E501"]
"jwst/associations/**.py" = ["D", "N", "A", "ARG", "B", "C4", "ICN", "INP", "ISC", "LOG", "NPY", "PGH", "PTH", "S", "SLF", "SLOT", "T20", "TRY", "UP", "YTT", "E501"]
"jwst/background/**.py" = ["D", "N", "A", "ARG", "B", "C4", "ICN", "INP", "ISC", "LOG", "NPY", "PGH", "PTH", "S", "SLF", "SLOT", "T20", "TRY", "UP", "YTT", "E501"]
Expand All @@ -144,7 +144,6 @@ ignore-fully-untyped = true # Turn off annotation checking for fully untyped co
"jwst/persistence/**.py" = ["D", "N", "A", "ARG", "B", "C4", "ICN", "INP", "ISC", "LOG", "NPY", "PGH", "PTH", "S", "SLF", "SLOT", "T20", "TRY", "UP", "YTT", "E501"]
"jwst/photom/**.py" = ["D", "N", "A", "ARG", "B", "C4", "ICN", "INP", "ISC", "LOG", "NPY", "PGH", "PTH", "S", "SLF", "SLOT", "T20", "TRY", "UP", "YTT", "E501"]
"jwst/refpix/**.py" = ["D", "N", "A", "ARG", "B", "C4", "ICN", "INP", "ISC", "LOG", "NPY", "PGH", "PTH", "S", "SLF", "SLOT", "T20", "TRY", "UP", "YTT", "E501"]
"jwst/regtest/**.py" = ["D", "N", "A", "ARG", "B", "C4", "ICN", "INP", "ISC", "LOG", "NPY", "PGH", "PTH", "S", "SLF", "SLOT", "T20", "TRY", "UP", "YTT", "E501"]
"jwst/resample/**.py" = ["D", "N", "A", "ARG", "B", "C4", "ICN", "INP", "ISC", "LOG", "NPY", "PGH", "PTH", "S", "SLF", "SLOT", "T20", "TRY", "UP", "YTT", "E501"]
"jwst/reset/**.py" = ["D", "N", "A", "ARG", "B", "C4", "ICN", "INP", "ISC", "LOG", "NPY", "PGH", "PTH", "S", "SLF", "SLOT", "T20", "TRY", "UP", "YTT", "E501"]
"jwst/residual_fringe/**.py" = ["D", "N", "A", "ARG", "B", "C4", "ICN", "INP", "ISC", "LOG", "NPY", "PGH", "PTH", "S", "SLF", "SLOT", "T20", "TRY", "UP", "YTT", "E501"]
Expand All @@ -155,7 +154,6 @@ ignore-fully-untyped = true # Turn off annotation checking for fully untyped co
"jwst/srctype/**.py" = ["D", "N", "A", "ARG", "B", "C4", "ICN", "INP", "ISC", "LOG", "NPY", "PGH", "PTH", "S", "SLF", "SLOT", "T20", "TRY", "UP", "YTT", "E501"]
"jwst/straylight/**.py" = ["D", "N", "A", "ARG", "B", "C4", "ICN", "INP", "ISC", "LOG", "NPY", "PGH", "PTH", "S", "SLF", "SLOT", "T20", "TRY", "UP", "YTT", "E501"]
"jwst/superbias/**.py" = ["D", "N", "A", "ARG", "B", "C4", "ICN", "INP", "ISC", "LOG", "NPY", "PGH", "PTH", "S", "SLF", "SLOT", "T20", "TRY", "UP", "YTT", "E501"]
"jwst/tests/**.py" = ["D", "N", "A", "ARG", "B", "C4", "ICN", "INP", "ISC", "LOG", "NPY", "PGH", "PTH", "S", "SLF", "SLOT", "T20", "TRY", "UP", "YTT", "E501"]
"jwst/tso_photometry/**.py" = ["D", "N", "A", "ARG", "B", "C4", "ICN", "INP", "ISC", "LOG", "NPY", "PGH", "PTH", "S", "SLF", "SLOT", "T20", "TRY", "UP", "YTT", "E501"]
"jwst/wfs_combine/**.py" = ["D", "N", "A", "ARG", "B", "C4", "ICN", "INP", "ISC", "LOG", "NPY", "PGH", "PTH", "S", "SLF", "SLOT", "T20", "TRY", "UP", "YTT", "E501"]
"jwst/white_light/**.py" = ["D", "N", "A", "ARG", "B", "C4", "ICN", "INP", "ISC", "LOG", "NPY", "PGH", "PTH", "S", "SLF", "SLOT", "T20", "TRY", "UP", "YTT", "E501"]
4 changes: 3 additions & 1 deletion jwst/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Process JWST data with Python."""

import re

from importlib.metadata import version
Expand All @@ -7,6 +9,6 @@
_regex_git_hash = re.compile(r".*\+g(\w+)")
__version_commit__ = ""
if "+" in __version__:
commit = _regex_git_hash.match(__version__).groups() # type: ignore
commit = _regex_git_hash.match(__version__).groups() # type: ignore[union-attr]
if commit:
__version_commit__ = commit[0]
63 changes: 45 additions & 18 deletions jwst/conftest.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,40 @@
"""Project default for pytest"""
"""Set project defaults and add fixtures for pytest."""

import os
import tempfile
import pytest
import inspect
from pathlib import Path

from jwst.associations import (AssociationRegistry, AssociationPool)
from jwst.associations import AssociationRegistry, AssociationPool
from jwst.associations.tests.helpers import t_path


@pytest.fixture
def jail_environ():
"""Lock changes to the environment"""
"""Lock changes to the environment."""
original = os.environ.copy()
try:
yield
finally:
os.environ = original


@pytest.fixture(scope='session')
@pytest.fixture(scope="session")
def full_pool_rules(request):
"""Setup to use the full example pool and registry"""
pool_fname = t_path('data/mega_pool.csv')
"""
Set up the full example pool and registry.

Returns
-------
pool: AssociationPool
The full example pool as read from data/mega_pool.csv.
rules: AssociationRegistry
The registry of available associations.
pool_fname: str
The full test path to mega_pool.csv.
"""
pool_fname = t_path("data/mega_pool.csv")
pool = AssociationPool.read(pool_fname)
rules = AssociationRegistry()

Expand All @@ -46,20 +58,32 @@ def mk_tmp_dirs():

@pytest.fixture
def slow(request):
"""Setup slow fixture for tests to identify if --slow
has been specified
"""
return request.config.getoption('--slow')
Set up slow fixture for tests to identify if --slow has been specified.

Returns
-------
bool
True if --slow has been specified, False otherwise.
"""
return request.config.getoption("--slow")


@pytest.fixture(scope="module")
def tmp_cwd_module(request, tmp_path_factory):
"""Run test in a pristine temporary working directory, scoped to module.
This allows a fixture using it to produce files in a
"""
Set up fixture to run test in a pristine temporary working directory, scoped to module.

This allows a test using this fixture to produce files in a
temporary directory, and then have the tests access them.

Yields
------
tmp_path
The temporary directory path.
"""
old_dir = os.getcwd()
path = request.module.__name__.split('.')[-1]
path = request.module.__name__.split(".")[-1]
if request._parent_request.fixturename is not None:
path = path + "_" + request._parent_request.fixturename
newpath = tmp_path_factory.mktemp(path)
Expand All @@ -81,12 +105,14 @@ def tmp_cwd(tmp_path):

@pytest.hookimpl(trylast=True)
def pytest_configure(config):
terminal_reporter = config.pluginmanager.getplugin('terminalreporter')
config.pluginmanager.register(TestDescriptionPlugin(terminal_reporter), 'testdescription')
"""Add the test description plugin to the pytest configuration."""
terminal_reporter = config.pluginmanager.getplugin("terminalreporter")
config.pluginmanager.register(TestDescriptionPlugin(terminal_reporter), "testdescription")


class TestDescriptionPlugin:
"""Pytest plugin to print the test docstring when `pytest -vv` is used.
"""
Pytest plugin to print the test docstring when `pytest -vv` is used.

This plug-in was added to support JWST instrument team testing and
reporting for the JWST calibration pipeline.
Expand All @@ -97,20 +123,21 @@ def __init__(self, terminal_reporter):
self.desc = None

def pytest_runtest_protocol(self, item):
"""Get the docstring for the test."""
try:
# Get the docstring for the test
self.desc = inspect.getdoc(item.obj)
except AttributeError:
self.desc = None

@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_logstart(self, nodeid, location):
"""Print the test docstring when `pytest -vv` is used."""
# When run as `pytest` or `pytest -v`, no change in behavior
if self.terminal_reporter.verbosity <= 1:
yield
# When run as `pytest -vv`, `pytest -vvv`, etc, print the test docstring
else:
self.terminal_reporter.write('\n')
self.terminal_reporter.write("\n")
yield
if self.desc:
self.terminal_reporter.write(f'\n{self.desc} ')
self.terminal_reporter.write(f"\n{self.desc} ")
2 changes: 1 addition & 1 deletion jwst/extract_1d/soss_extract/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def throughput():
"""

def filter_function(wl, wl_max):
"""Set free parameters to roughly mimic throughput functions on main."""
# Set free parameters to roughly mimic throughput functions on main.
maxthru = 0.4
thresh = 0.01
scaling = 0.3
Expand Down
7 changes: 3 additions & 4 deletions jwst/pipeline/tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@ def update_asn_basedir(asn_file, root=None):

Parameters
----------
asn_file: str
asn_file : str
The original association file

root: str
root : str
The root directory where the data actually resides

Returns
-------
updated_asn_path: str
updated_asn_path : str
The updated association file path
"""
if root is None:
Expand Down
54 changes: 52 additions & 2 deletions jwst/ramp_fitting/tests/check_gls_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,41 @@ def setup_inputs(
gain=1,
deltatime=1,
):
"""Set up data."""
"""
Set up the input data.

Parameters
----------
ngroups : int
Number of groups in the ramp.
readnoise : float
Read noise.
nints : int
Number of integrations.
nrows : int
Number of rows in the image.
ncols : int
Number of columns in the image.
gain : float
Gain.
deltatime : float
Time between groups.

Returns
-------
model1 : RampModel
Data model.
gdq : ndarray
Group data quality array.
rnoise : ReadnoiseModel
Read noise data model.
pixdq : ndarray
Pixel data quality array.
err : ndarray
Error array.
gain : GainModel
Gain data model.
"""
times = np.array(list(range(ngroups)), dtype=np.float64) * deltatime
gain = np.ones(shape=(nrows, ncols), dtype=np.float64) * gain
err = np.ones(shape=(nints, ngroups, nrows, ncols), dtype=np.float64)
Expand Down Expand Up @@ -63,7 +97,23 @@ def setup_inputs(


def simple_ramp(fit_method="OLS", ngroups=10, cr_group=None):
"""Create a simple ramp."""
"""
Create a simple ramp.

Parameters
----------
fit_method : str
Fitting method.
ngroups : int
Number of groups in the ramp.
cr_group : int
Group with a cosmic ray.

Returns
-------
slopes : ndarray
Fitted slopes.
"""
# Here given a 10 group ramp with an exact slope of 20/group.
# The output slope should be 20.
slope_per_group = 20.0
Expand Down
1 change: 1 addition & 0 deletions jwst/regtest/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""JWST pipeline regression tests."""
Loading