Skip to content

Commit e3036da

Browse files
authored
[MNT] moving linting to ruff, adding editorconfig (#653)
* moves linting to `ruff` * adds `editorconfig` * makes tests dependent on passing linting Also fixes linting in some files (but not all).
1 parent 1a63132 commit e3036da

File tree

11 files changed

+490
-400
lines changed

11 files changed

+490
-400
lines changed

.editorconfig

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# This file is part of the tschm/.config-templates repository
2+
# (https://github.com/tschm/.config-templates).
3+
#
4+
root = true
5+
6+
# Default settings for all files
7+
[*]
8+
end_of_line = lf
9+
trim_trailing_whitespace = true
10+
insert_final_newline = true
11+
charset = utf-8
12+
13+
# Python, reStructuredText, and text files
14+
[*.{py,rst,txt}]
15+
indent_style = space
16+
indent_size = 4
17+
18+
# YAML, JSON, and other config files
19+
[*.{yml,yaml,json}]
20+
indent_style = space
21+
indent_size = 2
22+
23+
# Markdown files
24+
# [*.{md,markdown}]
25+
# trim_trailing_whitespace = false
26+
27+
# Don't apply editorconfig rules to vendor/ resources
28+
# This is a "defensive" rule for the day we may have
29+
# the vendor folder
30+
[vendor/**]
31+
charset = unset
32+
end_of_line = unset
33+
indent_size = unset
34+
indent_style = unset
35+
insert_final_newline = unset
36+
trim_trailing_whitespace = unset

.flake8

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

.github/workflows/codecov.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ jobs:
4949
- name: Upload coverage to Codecov
5050
# if false in order to skip for now
5151
if: false
52-
uses: codecov/codecov-action@v3
52+
uses: codecov/codecov-action@v5
5353
with:
5454
files: ./coverage.xml
5555
fail_ci_if_error: true

.github/workflows/main.yml

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,44 @@ concurrency:
1414
cancel-in-progress: true
1515

1616
jobs:
17+
code-quality:
18+
runs-on: ubuntu-latest
19+
20+
steps:
21+
- uses: actions/checkout@v5
22+
23+
- uses: actions/setup-python@v6
24+
with:
25+
python-version: '3.13'
26+
27+
- name: install pre-commit
28+
run: python3 -m pip install pre-commit
29+
30+
- name: Checkout code
31+
uses: actions/checkout@v5
32+
with:
33+
fetch-depth: 0
34+
35+
- name: Get changed files
36+
id: changed-files
37+
run: |
38+
CHANGED_FILES=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.sha }} | tr '\n' ' ')
39+
echo "CHANGED_FILES=${CHANGED_FILES}" >> $GITHUB_ENV
40+
41+
- name: Print changed files
42+
run: |
43+
echo "Changed files: $CHANGED_FILES"
44+
45+
- name: Run pre-commit on changed files
46+
run: |
47+
if [ -n "$CHANGED_FILES" ]; then
48+
pre-commit run --color always --files $CHANGED_FILES --show-diff-on-failure
49+
else
50+
echo "No changed files to check."
51+
fi
52+
1753
pytest:
54+
needs: code-quality
1855
name: py${{ matrix.python-version }} on ${{ matrix.os }}
1956
runs-on: ${{ matrix.os }}
2057
env:
@@ -53,15 +90,3 @@ jobs:
5390
- name: Test with pytest
5491
run: |
5592
pytest ./tests
56-
57-
- name: Check with isort
58-
run: |
59-
isort --check --diff .
60-
61-
- name: Check with black
62-
run: |
63-
black --check --diff .
64-
65-
- name: Check with flake8
66-
run: |
67-
flake8 --show-source --statistics .

.pre-commit-config.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v6.0.0
4+
hooks:
5+
- id: check-toml
6+
- id: check-yaml
7+
8+
- repo: https://github.com/astral-sh/ruff-pre-commit
9+
rev: 'v0.14.5'
10+
hooks:
11+
- id: ruff
12+
args: [ --fix, --exit-non-zero-on-fix, --unsafe-fixes ]
13+
14+
# Run the formatter
15+
- id: ruff-format

cookbook/1-RiskReturnModels.ipynb

Lines changed: 360 additions & 357 deletions
Large diffs are not rendered by default.

pypfopt/black_litterman.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def __init__(
135135
view_confidences=None,
136136
tau=0.05,
137137
risk_aversion=1,
138-
**kwargs
138+
**kwargs,
139139
):
140140
"""
141141
:param cov_matrix: NxN covariance matrix of returns
@@ -296,8 +296,7 @@ def _set_omega(self, omega, view_confidences):
296296
elif omega == "idzorek":
297297
if view_confidences is None:
298298
raise ValueError(
299-
"To use Idzorek's method, please supply a vector of percentage "
300-
"confidence levels for each view."
299+
"To use Idzorek's method, please supply a vector of percentage confidence levels for each view."
301300
)
302301
if not isinstance(view_confidences, np.ndarray):
303302
try:

pypfopt/cla.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -314,11 +314,11 @@ def _solve(self):
314314
covarF_inv = np.linalg.inv(covarF)
315315
j = 0
316316
for i in f:
317-
l, bi = self._compute_lambda(
317+
lam, bi = self._compute_lambda(
318318
covarF_inv, covarFB, meanF, wB, j, [self.lB[i], self.uB[i]]
319319
)
320-
if CLA._infnone(l) > CLA._infnone(l_in):
321-
l_in, i_in, bi_in = l, i, bi
320+
if CLA._infnone(lam) > CLA._infnone(l_in):
321+
l_in, i_in, bi_in = lam, i, bi
322322
j += 1
323323
# 2) case b): Free one bounded weight
324324
l_out = None
@@ -327,18 +327,18 @@ def _solve(self):
327327
for i in b:
328328
covarF, covarFB, meanF, wB = self._get_matrices(f + [i])
329329
covarF_inv = np.linalg.inv(covarF)
330-
l, bi = self._compute_lambda(
330+
lam, bi = self._compute_lambda(
331331
covarF_inv,
332332
covarFB,
333333
meanF,
334334
wB,
335335
meanF.shape[0] - 1,
336336
self.w[-1][i],
337337
)
338-
if (self.ls[-1] is None or l < self.ls[-1]) and l > CLA._infnone(
339-
l_out
340-
):
341-
l_out, i_out = l, i
338+
if (
339+
self.ls[-1] is None or lam < self.ls[-1]
340+
) and lam > CLA._infnone(l_out):
341+
l_out, i_out = lam, i
342342
if (l_in is None or l_in < 0) and (l_out is None or l_out < 0):
343343
# 3) compute minimum variance solution
344344
self.ls.append(0)

pypfopt/risk_models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def semicovariance(
181181
benchmark=0.000079,
182182
frequency=252,
183183
log_returns=False,
184-
**kwargs
184+
**kwargs,
185185
):
186186
"""
187187
Estimate the semicovariance matrix, i.e the covariance given that
@@ -290,7 +290,7 @@ def min_cov_determinant(
290290
frequency=252,
291291
random_state=None,
292292
log_returns=False,
293-
**kwargs
293+
**kwargs,
294294
): # pragma: no cover
295295
warnings.warn("min_cov_determinant is deprecated and will be removed in v1.5")
296296

pyproject.toml

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,9 @@ all_extras = [
6161

6262
# dev - the developer dependency set, for contributors and CI
6363
dev = [
64-
"pytest>=7.1.2",
65-
"flake8>=4.0.1",
66-
"black>=22.3.0",
67-
"pytest-cov>=3.0.0",
68-
"yfinance>=0.1.70",
69-
"isort",
64+
"pytest>=9.0.0",
65+
"pytest-cov>=7.0.0",
66+
"yfinance>=0.2.66",
7067
]
7168

7269
# notebook tests
@@ -91,8 +88,28 @@ requires = [
9188
[tool.setuptools.packages.find]
9289
exclude = ["example", "example.*", "tests", "tests.*"]
9390

94-
[tool.black]
91+
[tool.ruff]
9592
line-length = 88
93+
# Keep Ruff aligned with project target version
94+
target-version = "py311"
95+
# Exclude individual files/patterns from Ruff linting/formatting
96+
exclude = [
97+
"tests/test_imports.py",
98+
"cookbook/*.ipynb",
99+
]
100+
101+
[tool.ruff.lint]
102+
# Keep the same selected rule sets as in ruff.toml
103+
select = ["F", "I"]
104+
105+
[tool.ruff.format]
106+
# Formatting configuration
107+
quote-style = "double"
108+
indent-style = "space"
109+
line-ending = "auto"
110+
skip-magic-trailing-comma = false
96111

97-
[tool.isort]
98-
profile = "black"
112+
[tool.ruff.lint.isort]
113+
known-first-party = ["pypfopt"]
114+
combine-as-imports = true
115+
force-sort-within-sections = true

0 commit comments

Comments
 (0)