Skip to content

Commit

Permalink
draft mypy types
Browse files Browse the repository at this point in the history
  • Loading branch information
NiklasRosenstein committed Jan 31, 2025
1 parent b9f1fd3 commit ff1ed4c
Show file tree
Hide file tree
Showing 17 changed files with 928 additions and 377 deletions.
33 changes: 32 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ name = "pytr"
version = "0.3.1"
description = "Use TradeRepublic in terminal"
readme = "README.md"
requires-python = ">=3.8"
requires-python = ">=3.11"
license = { text = "MIT" }
authors = [
{ name = "marzzzello", email = "853485-marzzzello@users.noreply.gitlab.com" }
Expand All @@ -32,10 +32,41 @@ dependencies = [
"shtab",
"websockets>=10.1,<14",
"babel",
"typing-extensions>=4.7.0",
]

[project.optional-dependencies]
dev = [
"types-requests",
"types-pygments",
# Don't exist:
# "types-coloredlogs",
# "types-ecdsa"
]

[tool.mypy]
python_version = "3.11"
warn_return_any = true
warn_unused_configs = true
disallow_untyped_defs = true
disallow_incomplete_defs = true
check_untyped_defs = true
disallow_untyped_decorators = true
no_implicit_optional = true
warn_redundant_casts = true
warn_unused_ignores = true
warn_no_return = true
warn_unreachable = true

[project.scripts]
pytr = "pytr.main:main"

[tool.hatch.build.hooks.babel]
locale_dir = "pytr/locale"

[dependency-groups]
dev = [
"mypy>=1.14.1",
"types-pygments>=2.19.0.20250107",
"types-requests>=2.32.0.20241016",
]
22 changes: 15 additions & 7 deletions pytr/account.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import json
import sys
from pygments import highlight, lexers, formatters
import time
from getpass import getpass
from logging import Logger
from typing import Optional

from pytr.api import TradeRepublicApi, CREDENTIALS_FILE
from pygments import formatters, highlight, lexers

from pytr.api import CREDENTIALS_FILE, TradeRepublicApi
from pytr.utils import get_logger


def get_settings(tr):
def get_settings(tr: TradeRepublicApi) -> str:
formatted_json = json.dumps(tr.settings(), indent=2)
if sys.stdout.isatty():
colorful_json = highlight(
Expand All @@ -19,7 +22,12 @@ def get_settings(tr):
return formatted_json


def login(phone_no=None, pin=None, web=True, store_credentials=False):
def login(
phone_no: Optional[str] = None,
pin: Optional[str] = None,
web: bool = True,
store_credentials: bool = False
) -> TradeRepublicApi:
"""
If web is true, use web login method, else simulate app login.
Handle credentials parameters and store to credentials file if requested.
Expand Down Expand Up @@ -79,10 +87,10 @@ def login(phone_no=None, pin=None, web=True, store_credentials=False):
)
code = input("Code: ")
if code == "":
countdown = countdown - (time.time() - request_time)
for remaining in range(int(countdown)):
remaining_time = countdown - (time.time() - request_time)
for remaining in range(int(remaining_time)):
print(
f"Need to wait {int(countdown-remaining)} seconds before requesting SMS...",
f"Need to wait {int(remaining_time-remaining)} seconds before requesting SMS...",
end="\r",
)
time.sleep(1)
Expand Down
25 changes: 12 additions & 13 deletions pytr/alarms.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import asyncio
from datetime import datetime
from logging import Logger
from typing import Any, Dict, List

from pytr.utils import preview, get_logger
from pytr.utils import get_logger, preview


class Alarms:
def __init__(self, tr):
def __init__(self, tr: Any) -> None:
self.tr = tr
self.log = get_logger(__name__)
self.alarms = []

async def alarms_loop(self):
async def alarms_loop(self) -> None:
recv = 0
await self.tr.price_alarm_overview()
while True:
Expand All @@ -26,7 +29,7 @@ async def alarms_loop(self):
if recv == 1:
return

async def ticker_loop(self):
async def ticker_loop(self) -> None:
recv = 0
await self.tr.price_alarm_overview()
while True:
Expand All @@ -43,7 +46,7 @@ async def ticker_loop(self):
if recv == 1:
return

def overview(self):
def overview(self) -> None:
print("ISIN status created target diff% createdAt triggeredAT")
self.log.debug(f"Processing {len(self.alarms)} alarms")

Expand All @@ -57,25 +60,21 @@ def overview(self):
target_price = float(a["targetPrice"])
created_price = float(a["createdPrice"])
created = datetime.fromtimestamp(ts).isoformat(sep=" ", timespec="minutes")
if a["triggeredAt"] is None:
triggered = "-"
else:
triggered = "-"
if a["triggeredAt"] is not None:
ts = int(a["triggeredAt"]) / 1000.0
triggered = datetime.fromtimestamp(ts).isoformat(
sep=" ", timespec="minutes"
)

if a["createdPrice"] == 0:
diffP = 0.0
else:
diffP = (target_price / created_price) * 100 - 100
diffP = 0.0 if a["createdPrice"] == 0 else (target_price / created_price) * 100 - 100

print(
f"{a['instrumentId']} {a['status']} {created_price:>7.2f} {target_price:>7.2f} "
+ f"{diffP:>5.1f}% {created} {triggered}"
)

def get(self):
def get(self) -> None:
asyncio.get_event_loop().run_until_complete(self.alarms_loop())

self.overview()
Loading

0 comments on commit ff1ed4c

Please sign in to comment.