Skip to content

Commit

Permalink
Added linting with flake8 (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilcardella committed Aug 1, 2020
1 parent 7085c70 commit 0b58a92
Show file tree
Hide file tree
Showing 19 changed files with 120 additions and 41 deletions.
7 changes: 7 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[flake8]
; ignore = E203, E266, E501, W503, F403
; max-line-length = 88
; max-complexity = 18
; select = B,C,E,F,W,T4,B9
select = C,E,F,W,B,B950
extend-ignore = E501
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Added
- Added Makefile to perform development and deployment actions
- Added formatting with black and isort
- Linting with flake8

## Removed
- Removed `setup.py` with full usage of `pyproject.toml`
Expand Down
9 changes: 4 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ build: clean
mypy:
> poetry run mypy tradingmate/

flake:
flake8:
> poetry run flake8 tradingmate/ test/

isort:
Expand All @@ -53,10 +53,9 @@ black:

format: isort black

lint: flake mypy
lint: flake8 #mypy

# check: format lint test
check: format test
check: format lint test

ci: install check docs build

Expand All @@ -72,4 +71,4 @@ clean:
> find . -name '.mypy_cache' -exec rm -rf {} +
> find . -name '.pytest_cache' -exec rm -rf {} +

.PHONY: test lint format install docs build install-system ci check mypy flake isort black remove update
.PHONY: test lint format install docs build install-system ci check mypy flake8 isort black remove update
59 changes: 58 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ docutils = "^0.16"
pytest = "^6.0.0"
black = {version = "^19.10b0", allow-prereleases = true}
isort = {version = "^5.2.1", allow-prereleases = true}
flake8 = "^3.8.3"

[tool.poetry.scripts]
trading_bot = 'tradingmate:main'
Expand Down
2 changes: 1 addition & 1 deletion test/test_configuration_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_config_values(cm):
assert isinstance(config, float)
assert config >= 0.0

confgi = cm.get_yfinance_polling_period()
config = cm.get_yfinance_polling_period()
assert isinstance(config, float)
assert config >= 0.0

Expand Down
32 changes: 16 additions & 16 deletions test/test_holding.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@ def test_init():
holding = Holding("mock", 10)
assert holding.get_symbol() == "mock"
assert holding.get_quantity() == 10
assert holding.get_open_price() == None
assert holding.get_last_price() == None
assert holding.get_last_price_valid() == False
assert holding.get_open_price() is None
assert holding.get_last_price() is None
assert holding.get_last_price_valid() is False

holding = Holding("mock", 10, 100)
assert holding.get_symbol() == "mock"
assert holding.get_quantity() == 10
assert holding.get_open_price() == 100
assert holding.get_last_price() == None
assert holding.get_last_price_valid() == False
assert holding.get_last_price() is None
assert holding.get_last_price_valid() is False


def test_init_fail():
with pytest.raises(ValueError) as e:
h = Holding("mock", -1)
with pytest.raises(ValueError) as e:
h = Holding("mock", 0)
with pytest.raises(ValueError) as e:
h = Holding("mock", 1, -1)
with pytest.raises(ValueError):
_ = Holding("mock", -1)
with pytest.raises(ValueError):
_ = Holding("mock", 0)
with pytest.raises(ValueError):
_ = Holding("mock", 1, -1)


def test_get_cost():
Expand Down Expand Up @@ -81,21 +81,21 @@ def test_get_profit_loss_perc():

def test_set_last_price():
h = Holding("mock", 1, 100)
with pytest.raises(ValueError) as e:
with pytest.raises(ValueError):
h.set_last_price(-1)


def test_set_open_price():
h = Holding("mock", 1, 100)
with pytest.raises(ValueError) as e:
with pytest.raises(ValueError):
h.set_open_price(-1)


def test_set_quantity():
h = Holding("mock", 1, 100)
with pytest.raises(ValueError) as e:
with pytest.raises(ValueError):
h.set_quantity(-1)
with pytest.raises(ValueError) as e:
with pytest.raises(ValueError):
h.set_quantity(0)


Expand All @@ -104,4 +104,4 @@ def test_set_last_price_invalid():
h.set_last_price(1000)
assert h.get_last_price_valid()
h.set_last_price_invalid()
assert h.get_last_price_valid() == False
assert h.get_last_price_valid() is False
10 changes: 5 additions & 5 deletions test/test_portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ def test_get_holding_quantity(portfolio):

def test_get_holding_last_price(portfolio):
wait_for_prices(portfolio)
with pytest.raises(ValueError) as e:
with pytest.raises(ValueError):
assert portfolio.get_holding_last_price("MOCK") is None
assert portfolio.get_holding_last_price("MOCK13") == PF_MOCK13_LAST_PRICE
assert portfolio.get_holding_last_price("MOCK4") == PF_MOCK4_LAST_PRICE


def test_get_holding_open_price(portfolio):
with pytest.raises(ValueError) as e:
with pytest.raises(ValueError):
assert portfolio.get_holding_open_price("MOCK") is None
assert portfolio.get_holding_open_price("MOCK13") == PF_MOCK13_OPEN_PRICE
assert portfolio.get_holding_open_price("MOCK4") == PF_MOCK4_OPEN_PRICE
Expand Down Expand Up @@ -140,7 +140,7 @@ def test_get_open_positions_pl_perc(portfolio):


def test_has_unsaved_changes(portfolio):
assert portfolio.has_unsaved_changes() == False
assert portfolio.has_unsaved_changes() is False
item = {
"id": "0",
"date": "01/01/0001 00:00",
Expand All @@ -153,9 +153,9 @@ def test_has_unsaved_changes(portfolio):
"notes": "mock",
}
portfolio.add_trade(Trade.from_dict(item))
assert portfolio.has_unsaved_changes() == True
assert portfolio.has_unsaved_changes() is True
portfolio.save_portfolio("/tmp/TradingMate_test_portfolio.json")
assert portfolio.has_unsaved_changes() == False
assert portfolio.has_unsaved_changes() is False


def test_get_trade_history(portfolio):
Expand Down
8 changes: 4 additions & 4 deletions test/test_trading_mate.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ def test_new_trade_event(trading_mate):
}
for pf in trading_mate.get_portfolios():
trade = Trade.from_dict(invalid_trade)
with pytest.raises(RuntimeError) as e:
with pytest.raises(RuntimeError):
trading_mate.new_trade_event(trade, pf.get_id())


def test_delete_trade_event(trading_mate):
for pf in trading_mate.get_portfolios():
# Disable auto refresh so we do not need to mock requests for MOCK1
trading_mate.set_auto_refresh(False, pf.get_id())
with pytest.raises(RuntimeError) as e:
with pytest.raises(RuntimeError):
trading_mate.delete_trade_event(pf.get_id(), "mock_initial_deposit")
trading_mate.delete_trade_event(pf.get_id(), "mock_last_trade")
# Verify the last trade has been deleted
Expand All @@ -116,7 +116,7 @@ def test_delete_trade_event(trading_mate):

def test_open_portfolio_event(trading_mate):
assert len(trading_mate.get_portfolios()) == 3
with pytest.raises(Exception) as e:
with pytest.raises(Exception):
trading_mate.open_portfolio_event("/tmp/non_existing_file.json")
trading_mate.open_portfolio_event("test/test_data/trading_log.json")
assert len(trading_mate.get_portfolios()) == 4
Expand All @@ -125,7 +125,7 @@ def test_open_portfolio_event(trading_mate):
def test_save_portfolio_event(trading_mate):
for pf in trading_mate.get_portfolios():
temp_file = f"/tmp/new_trading_log{pf.get_id()}.json"
assert os.path.exists(temp_file) == False
assert os.path.exists(temp_file) is False
trading_mate.save_portfolio_event(pf.get_id(), temp_file)
assert os.path.exists(temp_file)

Expand Down
4 changes: 2 additions & 2 deletions tradingmate/model/ConfigurationManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ def _load_credentials(self):
Load the credentials file
"""
try:
credentials_filepath = self.config["general"]["credentials_filepath"]
except:
credentials_filepath = self.get_credentials_path()
except Exception:
credentials_filepath = "{}/config/.credentials".format(
Utils.get_install_path()
)
Expand Down
2 changes: 1 addition & 1 deletion tradingmate/ui/TradingMateClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
class TradingMateClient:
"""Client interface to the TradingMate business logic"""

def __init__(self, server):
def __init__(self, server: DataInterface):
self._server = server

def stop(self):
Expand Down
6 changes: 4 additions & 2 deletions tradingmate/ui/gtk/AddTradeWindow.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# flake8: noqa: E402 # Required to allow use of gi.require_version

import datetime
import os

import gi

gi.require_version("Gtk", "3.0") # noqa
from gi.repository import Gtk as gtk # noqa
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk as gtk

from tradingmate.model import Trade
from tradingmate.ui.gtk import MessageDialog
Expand Down
2 changes: 2 additions & 0 deletions tradingmate/ui/gtk/ConfirmDialog.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# flake8: noqa: E402 # Required to allow use of gi.require_version

import gi

gi.require_version("Gtk", "3.0")
Expand Down
8 changes: 4 additions & 4 deletions tradingmate/ui/gtk/ExploreMarketsWindow.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import inspect
# flake8: noqa: E402 # Required to allow use of gi.require_version

import os
import sys
from datetime import datetime as dt

import gi
Expand Down Expand Up @@ -61,7 +61,7 @@ def _validate_value(self, value, negative_ok=False):
def _validate_date(self, date):
try:
return dt.strftime(date, "%d/%m/%Y")
except:
except Exception:
return "-"

def _clear_content(self):
Expand Down Expand Up @@ -145,7 +145,7 @@ def _update_earnings(self, earnings):
# TODO
pass

### Public API
# Public API

def show(self):
self._clear_content()
Expand Down
2 changes: 2 additions & 0 deletions tradingmate/ui/gtk/LogWindow.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# flake8: noqa: E402 # Required to allow use of gi.require_version

import os
import threading
import time
Expand Down
2 changes: 2 additions & 0 deletions tradingmate/ui/gtk/MessageDialog.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# flake8: noqa: E402 # Required to allow use of gi.require_version

import gi

gi.require_version("Gtk", "3.0")
Expand Down
2 changes: 2 additions & 0 deletions tradingmate/ui/gtk/PortfolioPage.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# flake8: noqa: E402 # Required to allow use of gi.require_version

import os

import gi
Expand Down

0 comments on commit 0b58a92

Please sign in to comment.