Skip to content

Commit b898244

Browse files
Update project configuration and linting defaults
Why these changes are being introduced: * Simplify and harmonize project configuration and linting methods with modern and active libraries that augment one another. How this addresses that need: * Replace setup.cfg with pyproject.toml * Use updated pyproject.toml template * Use updated Makefile template * Add pre-commit hooks * Update various modules to address Ruff's lint rules * Update Pipfile to remove deprecated dev packages * Update Python version to 3.11 Side effects of this change: * Updated dependencies and deprecation of setup.cfg Relevant ticket(s): * https://mitlibraries.atlassian.net/browse/IN-894
1 parent 9c4e1d2 commit b898244

File tree

11 files changed

+646
-48
lines changed

11 files changed

+646
-48
lines changed

.pre-commit-config.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
default_language_version:
2+
python: python3.11 # set for project python version
3+
repos:
4+
- repo: local
5+
hooks:
6+
- id: black-apply
7+
name: black-apply
8+
entry: pipenv run black
9+
language: system
10+
pass_filenames: true
11+
types: ["python"]
12+
- id: mypy
13+
name: mypy
14+
entry: pipenv run mypy
15+
language: system
16+
pass_filenames: true
17+
types: ["python"]
18+
exclude: "tests/"
19+
- id: ruff-apply
20+
name: ruff-apply
21+
entry: pipenv run ruff check --fix
22+
language: system
23+
pass_filenames: true
24+
types: ["python"]
25+
- id: safety
26+
name: safety
27+
entry: pipenv check
28+
language: system
29+
pass_filenames: false

.python-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.10.3
1+
3.11.4

Makefile

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,49 @@
11
SHELL=/bin/bash
22
DATETIME:=$(shell date -u +%Y%m%dT%H%M%SZ)
33

4-
### Dependency commands ###
4+
## ---- Dependency commands ---- ##
55

6-
install: ## Install dependencies and CLI app
6+
install: # install dependencies
77
pipenv install --dev
8+
pipenv run pre-commit install
89

9-
update: install ## Update all Python dependencies
10+
update: install # update all Python dependencies
1011
pipenv clean
1112
pipenv update --dev
1213

13-
### Test commands ###
14+
## ---- Unit test commands ---- ##
1415

15-
test: ## Run tests and print a coverage report
16+
test: # run tests and print a coverage report
1617
pipenv run coverage run --source=my_app -m pytest -vv
1718
pipenv run coverage report -m
1819

1920
coveralls: test
2021
pipenv run coverage lcov -o ./coverage/lcov.info
2122

22-
### Code quality and safety commands ###
2323

24-
lint: bandit black mypy pylama safety ## Run linting, code quality, and safety checks
24+
## ---- Code quality and safety commands ---- ##
2525

26-
bandit:
27-
pipenv run bandit -r my_app
26+
# linting commands
27+
lint: black mypy ruff safety
2828

2929
black:
3030
pipenv run black --check --diff .
3131

3232
mypy:
33-
pipenv run mypy my_app
33+
pipenv run mypy .
3434

35-
pylama:
36-
pipenv run pylama --options setup.cfg
35+
ruff:
36+
pipenv run ruff check .
3737

3838
safety:
3939
pipenv check
4040
pipenv verify
41+
42+
# apply changes to resolve any linting errors
43+
lint-apply: black-apply ruff-apply
44+
45+
black-apply:
46+
pipenv run black .
47+
48+
ruff-apply:
49+
pipenv run ruff check --fix .

Pipfile

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,15 @@ click = "*"
88
sentry-sdk = "*"
99

1010
[dev-packages]
11-
bandit = "*"
1211
black = "*"
13-
coverage = "*"
1412
coveralls = "*"
1513
mypy = "*"
16-
pylama = {extras = ["all"], version = "*"}
14+
pre-commit = "*"
1715
pytest = "*"
16+
ruff = "*"
1817

1918
[requires]
20-
python_version = "3.10"
19+
python_version = "3.11"
2120

2221
[scripts]
2322
my_app = "python -c \"from my_app.cli import main; main()\""

Pipfile.lock

Lines changed: 512 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

my_app/cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
@click.option(
1414
"-v", "--verbose", is_flag=True, help="Pass to log at debug level instead of info"
1515
)
16-
def main(verbose: bool) -> None:
16+
def main(*, verbose: bool) -> None:
1717
start_time = perf_counter()
1818
root_logger = logging.getLogger()
19-
logger.info(configure_logger(root_logger, verbose))
19+
logger.info(configure_logger(root_logger, verbose=verbose))
2020
logger.info(configure_sentry())
2121
logger.info("Running process")
2222

my_app/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import sentry_sdk
55

66

7-
def configure_logger(logger: logging.Logger, verbose: bool) -> str:
7+
def configure_logger(logger: logging.Logger, *, verbose: bool) -> str:
88
if verbose:
99
logging.basicConfig(
1010
format="%(asctime)s %(levelname)s %(name)s.%(funcName)s() line %(lineno)d: "

pyproject.toml

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# We do our best to sync this template with the latest version in our internal documentation.
2+
# For MIT developers, we strongly recommend copying the pyproject.toml template from:
3+
# https://mitlibraries.atlassian.net/wiki/spaces/IN/pages/3432415247/Python+Project+Linters#Template-for-pyproject.toml
4+
5+
[tool.black]
6+
line-length = 90
7+
8+
[tool.mypy]
9+
disallow_untyped_calls = true
10+
disallow_untyped_defs = true
11+
exclude = ["tests/"]
12+
13+
[tool.pytest.ini_options]
14+
log_level = "INFO"
15+
16+
[tool.ruff]
17+
target-version = "py311"
18+
select = ["ALL", "PT"]
19+
20+
ignore = [
21+
# default
22+
"ANN101",
23+
"ANN102",
24+
"COM812",
25+
"D107",
26+
"N812",
27+
"PTH",
28+
29+
# project-specific
30+
"C90",
31+
"D100",
32+
"D101",
33+
"D102",
34+
"D103",
35+
"D104",
36+
"PLR0912",
37+
"PLR0913",
38+
"PLR0915",
39+
"S320",
40+
"S321",
41+
]
42+
43+
# allow autofix behavior for specified rules
44+
fixable = ["E", "F", "I", "Q"]
45+
46+
# set max line length
47+
line-length = 90
48+
49+
# enumerate all fixed violations
50+
show-fixes = true
51+
52+
[tool.ruff.flake8-annotations]
53+
mypy-init-return = true
54+
55+
[tool.ruff.flake8-pytest-style]
56+
fixture-parentheses = false
57+
58+
[tool.ruff.per-file-ignores]
59+
"tests/**/*" = [
60+
"ANN",
61+
"ARG001",
62+
"S101",
63+
]
64+
65+
[tool.ruff.pycodestyle]
66+
max-doc-length = 90
67+
68+
[tool.ruff.pydocstyle]
69+
convention = "google"

setup.cfg

Lines changed: 0 additions & 22 deletions
This file was deleted.

tests/conftest.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55

66

77
@pytest.fixture(autouse=True)
8-
def test_env():
9-
os.environ = {"SENTRY_DSN": None, "WORKSPACE": "test"}
10-
yield
8+
def _test_env():
9+
os.environ["SENTRY_DSN"] = "None"
10+
os.environ["WORKSPACE"] = "test"
1111

1212

13-
@pytest.fixture()
13+
@pytest.fixture
1414
def runner():
1515
return CliRunner()

0 commit comments

Comments
 (0)