Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pykis/__env__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@


VERSION = "{{VERSION_PLACEHOLDER}}" # This is automatically set via a tag in GitHub Workflow.
VERSION = "dev" if "VERSION_PLACEHOLDER" in VERSION else VERSION
VERSION = "24+dev" if "VERSION_PLACEHOLDER" in VERSION else VERSION

USER_AGENT = f"PyKis/{VERSION}"

Expand Down
10 changes: 6 additions & 4 deletions pykis/api/account/balance.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from decimal import Decimal
from functools import cached_property
from typing import TYPE_CHECKING, Iterator, Protocol, runtime_checkable

from pykis.adapter.account_product.order import (
Expand Down Expand Up @@ -27,7 +28,6 @@
from pykis.responses.dynamic import KisDynamic, KisList, KisObject, KisTransform
from pykis.responses.response import KisAPIResponse, KisPaginationAPIResponse
from pykis.responses.types import KisAny, KisDecimal, KisString
from pykis.utils.cache import cached
from pykis.utils.repr import kis_repr
from pykis.utils.typing import Checkable

Expand Down Expand Up @@ -748,12 +748,14 @@ class KisForeignBalanceStock(KisDynamic, KisBalanceStockBase):
purchase_amount: Decimal = KisDecimal["frcr_pchs_amt1"]
"""๋งค์ž…๊ธˆ์•ก"""

@property
@cached
def exchange_rate(self) -> Decimal:
# Pylance bug: cached_property[Decimal] type inference error.
@cached_property
def exchange_rate(self) -> Decimal: # type: ignore
"""ํ™˜์œจ (์บ์‹œ๋จ)"""
return self.balance.deposits[self.currency].exchange_rate

exchange_rate: Decimal


class KisForeignBalance(KisPaginationAPIResponse, KisBalanceBase):
"""ํ•œ๊ตญํˆฌ์ž์ฆ๊ถŒ ํ•ด์™ธ์ข…๋ชฉ ์ž”๊ณ """
Expand Down
10 changes: 6 additions & 4 deletions pykis/api/account/daily_order.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import date, datetime, timedelta
from decimal import Decimal
from functools import cached_property
from typing import TYPE_CHECKING, Any, Iterable, Protocol, runtime_checkable
from zoneinfo import ZoneInfo

Expand Down Expand Up @@ -29,7 +30,6 @@
from pykis.responses.dynamic import KisDynamic, KisList, KisTransform
from pykis.responses.response import KisPaginationAPIResponse
from pykis.responses.types import KisAny, KisDecimal, KisString
from pykis.utils.cache import cached
from pykis.utils.repr import kis_repr
from pykis.utils.timezone import TIMEZONE

Expand Down Expand Up @@ -490,9 +490,9 @@ class KisForeignDailyOrder(KisDynamic, KisDailyOrderBase):
number: str = KisString["odno"]
"""์ฃผ๋ฌธ๋ฒˆํ˜ธ"""

@property
@cached
def order_number(self) -> KisOrder:
# Pylance bug: cached_property[KisOrder] type inference error.
@cached_property
def order_number(self) -> KisOrder: # type: ignore
"""์ฃผ๋ฌธ๋ฒˆํ˜ธ"""
return KisSimpleOrder.from_order(
account_number=self.account_number,
Expand All @@ -504,6 +504,8 @@ def order_number(self) -> KisOrder:
kis=self.kis,
)

order_number: KisOrder

name: str = KisString["prdt_name"]
"""์ข…๋ชฉ๋ช…"""

Expand Down
13 changes: 7 additions & 6 deletions pykis/api/account/order_profit.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import date, datetime
from decimal import Decimal
from functools import cached_property
from typing import TYPE_CHECKING, Iterable, Protocol, runtime_checkable
from zoneinfo import ZoneInfo

Expand All @@ -15,14 +16,12 @@
KisMarketType,
get_market_code,
get_market_code_timezone,
get_market_timezone,
)
from pykis.client.account import KisAccountNumber
from pykis.client.page import KisPage
from pykis.responses.dynamic import KisDynamic, KisList, KisTransform
from pykis.responses.response import KisPaginationAPIResponse
from pykis.responses.types import KisAny, KisDecimal, KisInt, KisString
from pykis.utils.cache import cached
from pykis.responses.types import KisAny, KisDecimal, KisString
from pykis.utils.repr import kis_repr
from pykis.utils.timezone import TIMEZONE

Expand Down Expand Up @@ -438,9 +437,9 @@ class KisForeignOrderProfits(KisPaginationAPIResponse, KisOrderProfitsBase):
_end: date
_country: COUNTRY_TYPE | None = None

@property
@cached
def fees(self) -> Decimal:
# Pylance bug: cached_property[Decimal] type inference error.
@cached_property
def fees(self) -> Decimal: # type: ignore
"""
์ˆ˜์ˆ˜๋ฃŒ ์กฐํšŒ (๋ชจ์˜ํˆฌ์ž ๋ฏธ์ง€์›)

Expand All @@ -454,6 +453,8 @@ def fees(self) -> Decimal:
country=self._country,
)

fees: Decimal

def __init__(
self,
account_number: KisAccountNumber,
Expand Down
7 changes: 3 additions & 4 deletions pykis/api/account/orderable_amount.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from decimal import Decimal
from functools import cached_property
from typing import TYPE_CHECKING, Any, Protocol, runtime_checkable

from pykis.api.account.order import (
Expand All @@ -22,8 +23,7 @@
KisResponseProtocol,
raise_not_found,
)
from pykis.responses.types import KisDecimal, KisInt
from pykis.utils.cache import cached
from pykis.responses.types import KisDecimal
from pykis.utils.repr import kis_repr

if TYPE_CHECKING:
Expand Down Expand Up @@ -191,8 +191,7 @@ class KisDomesticOrderableAmount(KisAPIResponse, KisOrderableAmountBase):
foreign_only_amount: Decimal = KisDecimal["ord_psbl_frcr_amt_wcrc"]
"""์™ธํ™”์ฃผ๋ฌธ๊ฐ€๋Šฅ๊ธˆ์•ก (์›ํ™”ํ™˜์‚ฐ)"""

@property
@cached
@cached_property
def _foreign(self) -> "KisDomesticOrderableAmount":
"""
ํ•œ๊ตญํˆฌ์ž์ฆ๊ถŒ ๊ตญ๋‚ด ์ฃผ์‹ ์ฃผ๋ฌธ๊ฐ€๋Šฅ๊ธˆ์•ก ์กฐํšŒ
Expand Down
22 changes: 17 additions & 5 deletions pykis/api/stock/chart.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import bisect
from datetime import date, datetime, time, tzinfo
from decimal import Decimal
from typing import Iterable, Literal, Protocol, TypeVar, overload, runtime_checkable
from typing import (
TYPE_CHECKING,
Iterable,
Iterator,
Literal,
Protocol,
TypeVar,
overload,
runtime_checkable,
)

from pykis.api.base.product import KisProductBase, KisProductProtocol
from pykis.api.stock.market import MARKET_TYPE
Expand All @@ -15,6 +24,9 @@
"TChart",
]

if TYPE_CHECKING:
from pandas import DataFrame


@runtime_checkable
class KisChartBar(Protocol):
Expand Down Expand Up @@ -142,9 +154,9 @@ def __iter__(self) -> Iterable[KisChartBar]: ...

def __len__(self) -> int: ...

def __reversed__(self): ...
def __reversed__(self) -> Iterator[KisChartBar]: ...

def df(self):
def df(self) -> "DataFrame":
"""
์ฐจํŠธ๋ฅผ Pandas DataFrame์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

Expand Down Expand Up @@ -274,10 +286,10 @@ def __iter__(self) -> Iterable[KisChartBar]:
def __len__(self) -> int:
return len(self.bars)

def __reversed__(self):
def __reversed__(self) -> Iterator[KisChartBar]:
return reversed(self.bars)

def df(self):
def df(self) -> "DataFrame":
"""
์ฐจํŠธ๋ฅผ Pandas DataFrame์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

Expand Down
22 changes: 10 additions & 12 deletions pykis/api/stock/quote.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import date
from decimal import Decimal
from functools import cached_property
from typing import TYPE_CHECKING, Literal, Protocol, runtime_checkable

from pykis.api.base.product import KisProductBase, KisProductProtocol
Expand All @@ -22,7 +23,6 @@
KisInt,
KisString,
)
from pykis.utils.cache import cached, set_cache
from pykis.utils.repr import kis_repr
from pykis.utils.timezone import TIMEZONE

Expand Down Expand Up @@ -552,9 +552,9 @@ def change(self) -> Decimal:
"""์ „์ผ๋Œ€๋น„"""
return self.price - self.prev_price

@property
@cached
def indicator(self) -> KisForeignIndicator:
# Pylance bug: cached_property[KisForeignIndicator] type inference error.
@cached_property
def indicator(self) -> KisForeignIndicator: # type: ignore
"""์ข…๋ชฉ ์ง€ํ‘œ"""
return foreign_quote(
self.kis,
Expand All @@ -563,6 +563,8 @@ def indicator(self) -> KisForeignIndicator:
extended=False,
).indicator

indicator: KisForeignIndicator

open: Decimal = KisDecimal["open"]
"""๋‹น์ผ์‹œ๊ฐ€"""
high: Decimal = KisDecimal["high"]
Expand Down Expand Up @@ -605,14 +607,10 @@ def __pre_init__(self, data: dict):
super().__pre_init__(data)

if not self.extended:
set_cache(
self,
"indicator",
KisObject.transform_(
data["output"],
KisForeignIndicator,
ignore_missing=True,
),
self.indicator = KisObject.transform_(
data["output"],
KisForeignIndicator,
ignore_missing=True,
)


Expand Down
26 changes: 0 additions & 26 deletions pykis/utils/cache.py

This file was deleted.

4 changes: 3 additions & 1 deletion pykis/utils/repr.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from functools import wraps
from io import StringIO
from typing import Any, Iterable, Literal, Protocol, TypeVar

Expand Down Expand Up @@ -41,6 +42,7 @@ def kis_repr(
max_depth: int = 7,
):
def decorator(cls: type[TObject]) -> type[TObject]:
@wraps(cls.__repr__)
def __repr__(self, _depth: int = 0) -> str:
return object_repr(
self,
Expand All @@ -57,7 +59,7 @@ def __repr__(self, _depth: int = 0) -> str:
__repr__.__module__ = cls.__module__
__repr__.__qualname__ = f"{cls.__qualname__}.__repr__"
__repr__.__name__ = "__repr__"
__repr__.__is_kis_repr__ = True
__repr__.__is_kis_repr__ = True # type: ignore

cls.__repr__ = __repr__
return cls
Expand Down
4 changes: 3 additions & 1 deletion pykis/utils/thread_safe.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from functools import wraps
from multiprocessing import Lock
from typing import Any, Callable

Expand All @@ -9,7 +10,8 @@


def thread_safe(name: str | None = None):
def decorator(fn: Callable[..., Any]):
def decorator(fn: Callable[..., Any]) -> Callable[..., Any]:
@wraps(fn)
def wrapper(self, *args, **kwargs):
with global_lock:
key = f"__thread_safe_{name or fn.__name__}_lock"
Expand Down
9 changes: 6 additions & 3 deletions tests/env.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import os
from typing import Literal

import dotenv

import pykis.logging
from pykis import PyKis

dotenv.load_dotenv()
try:
import dotenv

dotenv.load_dotenv()
except ImportError:
pass


def load_pykis(
Expand Down
9 changes: 8 additions & 1 deletion tests/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import sys
import unittest
from pathlib import Path


def test_main():
def test_main() -> None:
sys.path.append(str(Path(__file__).parent.parent))

loader = unittest.TestLoader()
suite = loader.discover("tests/unit")

Expand All @@ -10,4 +14,7 @@ def test_main():


if __name__ == "__main__":
if sys.version_info < (3, 10):
raise RuntimeError("Python 3.10 ์ด์ƒ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.")

test_main()