Skip to content

Commit 0e270c8

Browse files
adrianhasseCopilot
andauthored
Copilot/fix 13 (#15)
* Initial plan * Complete migration to ruff + mypy configuration Co-authored-by: adrianhasse <28532763+adrianhasse@users.noreply.github.com> * Add mypy overrides for external library imports Co-authored-by: adrianhasse <28532763+adrianhasse@users.noreply.github.com> * Make ruff and mypy configuration less strict to fix CI failures Co-authored-by: adrianhasse <28532763+adrianhasse@users.noreply.github.com> * Fix ruff issues with notebook by excluding examples from linting Co-authored-by: adrianhasse <28532763+adrianhasse@users.noreply.github.com> * Fix CI workflow and configuration issues Co-authored-by: adrianhasse <28532763+adrianhasse@users.noreply.github.com> * Fix mypy type errors in broker.py Co-authored-by: adrianhasse <28532763+adrianhasse@users.noreply.github.com> * Fix line length error in broker.py docstring Co-authored-by: adrianhasse <28532763+adrianhasse@users.noreply.github.com> * Address PR feedback: remove redundant datetime checks and clean up configuration Co-authored-by: adrianhasse <28532763+adrianhasse@users.noreply.github.com> * Remove redundant mypy configuration settings to follow defaults Co-authored-by: adrianhasse <28532763+adrianhasse@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: adrianhasse <28532763+adrianhasse@users.noreply.github.com>
1 parent 42a641d commit 0e270c8

File tree

8 files changed

+56
-44
lines changed

8 files changed

+56
-44
lines changed

.github/workflows/python-package.yaml

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,16 @@ jobs:
2424
- name: Install dependencies
2525
run: |
2626
python -m pip install --upgrade pip
27-
if [ -f pyproject.toml ]; then
28-
pip install .[dev]
29-
fi
30-
- name: Lint with flake8
27+
pip install .[dev]
28+
- name: Lint and format with ruff
3129
run: |
32-
# Run flake8 and fail the build if there are any issues
33-
flake8 . --count --max-complexity=10 --max-line-length=88 --statistics
34-
- name: Lint with black
30+
ruff --version
31+
ruff format --check .
32+
ruff check .
33+
- name: Type check with mypy
3534
run: |
36-
black --check .
37-
- name: Lint with isort
38-
run: |
39-
isort . --check-only
35+
mypy --version
36+
mypy kissbt tests
4037
- name: Test with pytest
4138
run: |
4239
pytest

kissbt/broker.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from datetime import datetime
2-
from typing import Dict, List, Optional
2+
from typing import Dict, List, Optional, cast
33

44
import pandas as pd
55

@@ -64,9 +64,9 @@ def __init__(
6464
self._open_orders: List[Order] = []
6565

6666
self._current_bar: pd.DataFrame = pd.DataFrame()
67-
self._current_datetime = None
67+
self._current_datetime: Optional[datetime] = None
6868
self._previous_bar: pd.DataFrame = pd.DataFrame()
69-
self._previous_datetime = None
69+
self._previous_datetime: Optional[datetime] = None
7070

7171
self._long_only = long_only
7272
self._short_fee_rate = short_fee_rate
@@ -384,7 +384,7 @@ def update(
384384
self._execute_order(
385385
Order(ticker, -self._open_positions[ticker].size, OrderType.CLOSE),
386386
self._previous_bar,
387-
self._previous_datetime,
387+
cast(datetime, self._previous_datetime),
388388
)
389389

390390
# buy and sell assets
@@ -405,7 +405,9 @@ def update(
405405
continue
406406
if (
407407
not self._execute_order(
408-
open_order, self._current_bar, self._current_datetime
408+
open_order,
409+
self._current_bar,
410+
cast(datetime, self._current_datetime),
409411
)
410412
and open_order.good_till_cancel
411413
):
@@ -578,13 +580,14 @@ def cash(self) -> float:
578580
return self._cash
579581

580582
@property
581-
def benchmark(self) -> str:
583+
def benchmark(self) -> Optional[str]:
582584
"""Gets the benchmark symbol used for performance comparison.
583585
584586
The benchmark tracks a reference asset (e.g., market index) to evaluate relative
585587
strategy performance. Returns None if no benchmark was specified.
586588
587589
Returns:
588-
str: Ticker symbol of the benchmark instrument.
590+
Optional[str]: Ticker symbol of the benchmark instrument, or None if not
591+
set.
589592
"""
590593
return self._benchmark

pyproject.toml

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ requires-python = ">=3.10"
3030
dev = [
3131
"pytest",
3232
"pytest-mock",
33-
"flake8",
34-
"black",
35-
"isort",
33+
"ruff",
34+
"mypy",
3635
"yfinance",
3736
"pyarrow",
3837
]
@@ -41,21 +40,39 @@ dev = [
4140
homepage = "https://github.com/FinBlobs/kissbt"
4241
bug-tracker = "https://github.com/FinBlobs/kissbt/issues"
4342

44-
[tool.black]
43+
[tool.ruff]
4544
line-length = 88
46-
target-version = ['py310']
47-
include = '\.pyi?$'
45+
target-version = "py310"
46+
src = ["kissbt", "tests"]
47+
exclude = [
48+
".git",
49+
"__pycache__",
50+
"build",
51+
"dist",
52+
"examples",
53+
]
54+
55+
[tool.ruff.lint]
56+
# Start with core rules equivalent to the original flake8 setup
57+
select = [
58+
"E", # pycodestyle errors
59+
"W", # pycodestyle warnings
60+
"F", # pyflakes
61+
"I", # isort
62+
]
63+
# TODO: Gradually add more rules like N, UP, C4, B
4864

49-
[tool.isort]
50-
profile = "black"
51-
line_length = 88
52-
multi_line_output = 3
53-
include_trailing_comma = true
54-
force_grid_wrap = 0
55-
use_parentheses = true
56-
ensure_newline_before_comments = true
65+
[tool.ruff.format]
66+
# Enable Black-compatible formatting
67+
quote-style = "double"
68+
indent-style = "space"
69+
skip-magic-trailing-comma = false
70+
line-ending = "auto"
5771

58-
[tool.flake8]
59-
max-line-length = 88
60-
extend-ignore = ["E203"]
61-
exclude = [".git", "__pycache__", "build", "dist"]
72+
[tool.mypy]
73+
python_version = "3.10"
74+
# Start with basic type checking, will enable strict mode later
75+
warn_return_any = false
76+
warn_unused_configs = true
77+
show_error_codes = true
78+
ignore_missing_imports = true

tests/conftest.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import pandas as pd
44
import pytest
55
import yfinance as yf
6-
76
from kissbt.analyzer import Analyzer
87
from kissbt.broker import Broker
98

tests/test_broker.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import pandas as pd
44
import pytest
5-
65
from kissbt.broker import Broker
76
from kissbt.entities import OpenPosition, Order, OrderType
87

tests/test_entities.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from datetime import datetime
22

33
import pandas as pd
4-
54
from kissbt.entities import OpenPosition, Order, OrderType
65

76

tests/test_integration.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import pandas as pd
22
import pytest
3-
43
from kissbt.analyzer import Analyzer
54
from kissbt.broker import Broker
65
from kissbt.engine import Engine
@@ -66,9 +65,9 @@ def test_analyzer_with_golden_cross(tech_stock_data):
6665
assert len(broker.open_positions) == 0, "All positions should be closed"
6766
assert pytest.approx(broker.cash, 0.01) == 167534.46, "Final cash manually verified"
6867
assert pytest.approx(broker.portfolio_value, 0.01) == 167534.46
69-
assert (
70-
len(broker.closed_positions) == 15
71-
), "15 trades should have been executed, manually verified"
68+
assert len(broker.closed_positions) == 15, (
69+
"15 trades should have been executed, manually verified"
70+
)
7271

7372
# Create the Analyzer
7473
analyzer = Analyzer(broker, bar_size="1D")

tests/test_strategy.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import pandas as pd
22
import pytest
3-
43
from kissbt.broker import Broker
54
from kissbt.entities import Order
65
from kissbt.strategy import Strategy

0 commit comments

Comments
 (0)