Skip to content

Commit 4c58146

Browse files
authored
Merge pull request #60 from Soju06/refector/decorator-keeping-function-information
Refector/decorator keeping function information
2 parents 94a903d + 9bd0f9a commit 4c58146

File tree

12 files changed

+70
-68
lines changed

12 files changed

+70
-68
lines changed

pykis/__env__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424

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

2828
USER_AGENT = f"PyKis/{VERSION}"
2929

pykis/api/account/balance.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from decimal import Decimal
2+
from functools import cached_property
23
from typing import TYPE_CHECKING, Iterator, Protocol, runtime_checkable
34

45
from pykis.adapter.account_product.order import (
@@ -27,7 +28,6 @@
2728
from pykis.responses.dynamic import KisDynamic, KisList, KisObject, KisTransform
2829
from pykis.responses.response import KisAPIResponse, KisPaginationAPIResponse
2930
from pykis.responses.types import KisAny, KisDecimal, KisString
30-
from pykis.utils.cache import cached
3131
from pykis.utils.repr import kis_repr
3232
from pykis.utils.typing import Checkable
3333

@@ -748,12 +748,14 @@ class KisForeignBalanceStock(KisDynamic, KisBalanceStockBase):
748748
purchase_amount: Decimal = KisDecimal["frcr_pchs_amt1"]
749749
"""매입금액"""
750750

751-
@property
752-
@cached
753-
def exchange_rate(self) -> Decimal:
751+
# Pylance bug: cached_property[Decimal] type inference error.
752+
@cached_property
753+
def exchange_rate(self) -> Decimal: # type: ignore
754754
"""환율 (캐시됨)"""
755755
return self.balance.deposits[self.currency].exchange_rate
756756

757+
exchange_rate: Decimal
758+
757759

758760
class KisForeignBalance(KisPaginationAPIResponse, KisBalanceBase):
759761
"""한국투자증권 해외종목 잔고"""

pykis/api/account/daily_order.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from datetime import date, datetime, timedelta
22
from decimal import Decimal
3+
from functools import cached_property
34
from typing import TYPE_CHECKING, Any, Iterable, Protocol, runtime_checkable
45
from zoneinfo import ZoneInfo
56

@@ -29,7 +30,6 @@
2930
from pykis.responses.dynamic import KisDynamic, KisList, KisTransform
3031
from pykis.responses.response import KisPaginationAPIResponse
3132
from pykis.responses.types import KisAny, KisDecimal, KisString
32-
from pykis.utils.cache import cached
3333
from pykis.utils.repr import kis_repr
3434
from pykis.utils.timezone import TIMEZONE
3535

@@ -490,9 +490,9 @@ class KisForeignDailyOrder(KisDynamic, KisDailyOrderBase):
490490
number: str = KisString["odno"]
491491
"""주문번호"""
492492

493-
@property
494-
@cached
495-
def order_number(self) -> KisOrder:
493+
# Pylance bug: cached_property[KisOrder] type inference error.
494+
@cached_property
495+
def order_number(self) -> KisOrder: # type: ignore
496496
"""주문번호"""
497497
return KisSimpleOrder.from_order(
498498
account_number=self.account_number,
@@ -504,6 +504,8 @@ def order_number(self) -> KisOrder:
504504
kis=self.kis,
505505
)
506506

507+
order_number: KisOrder
508+
507509
name: str = KisString["prdt_name"]
508510
"""종목명"""
509511

pykis/api/account/order_profit.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from datetime import date, datetime
22
from decimal import Decimal
3+
from functools import cached_property
34
from typing import TYPE_CHECKING, Iterable, Protocol, runtime_checkable
45
from zoneinfo import ZoneInfo
56

@@ -15,14 +16,12 @@
1516
KisMarketType,
1617
get_market_code,
1718
get_market_code_timezone,
18-
get_market_timezone,
1919
)
2020
from pykis.client.account import KisAccountNumber
2121
from pykis.client.page import KisPage
2222
from pykis.responses.dynamic import KisDynamic, KisList, KisTransform
2323
from pykis.responses.response import KisPaginationAPIResponse
24-
from pykis.responses.types import KisAny, KisDecimal, KisInt, KisString
25-
from pykis.utils.cache import cached
24+
from pykis.responses.types import KisAny, KisDecimal, KisString
2625
from pykis.utils.repr import kis_repr
2726
from pykis.utils.timezone import TIMEZONE
2827

@@ -438,9 +437,9 @@ class KisForeignOrderProfits(KisPaginationAPIResponse, KisOrderProfitsBase):
438437
_end: date
439438
_country: COUNTRY_TYPE | None = None
440439

441-
@property
442-
@cached
443-
def fees(self) -> Decimal:
440+
# Pylance bug: cached_property[Decimal] type inference error.
441+
@cached_property
442+
def fees(self) -> Decimal: # type: ignore
444443
"""
445444
수수료 조회 (모의투자 미지원)
446445
@@ -454,6 +453,8 @@ def fees(self) -> Decimal:
454453
country=self._country,
455454
)
456455

456+
fees: Decimal
457+
457458
def __init__(
458459
self,
459460
account_number: KisAccountNumber,

pykis/api/account/orderable_amount.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from decimal import Decimal
2+
from functools import cached_property
23
from typing import TYPE_CHECKING, Any, Protocol, runtime_checkable
34

45
from pykis.api.account.order import (
@@ -22,8 +23,7 @@
2223
KisResponseProtocol,
2324
raise_not_found,
2425
)
25-
from pykis.responses.types import KisDecimal, KisInt
26-
from pykis.utils.cache import cached
26+
from pykis.responses.types import KisDecimal
2727
from pykis.utils.repr import kis_repr
2828

2929
if TYPE_CHECKING:
@@ -191,8 +191,7 @@ class KisDomesticOrderableAmount(KisAPIResponse, KisOrderableAmountBase):
191191
foreign_only_amount: Decimal = KisDecimal["ord_psbl_frcr_amt_wcrc"]
192192
"""외화주문가능금액 (원화환산)"""
193193

194-
@property
195-
@cached
194+
@cached_property
196195
def _foreign(self) -> "KisDomesticOrderableAmount":
197196
"""
198197
한국투자증권 국내 주식 주문가능금액 조회

pykis/api/stock/chart.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
import bisect
22
from datetime import date, datetime, time, tzinfo
33
from decimal import Decimal
4-
from typing import Iterable, Literal, Protocol, TypeVar, overload, runtime_checkable
4+
from typing import (
5+
TYPE_CHECKING,
6+
Iterable,
7+
Iterator,
8+
Literal,
9+
Protocol,
10+
TypeVar,
11+
overload,
12+
runtime_checkable,
13+
)
514

615
from pykis.api.base.product import KisProductBase, KisProductProtocol
716
from pykis.api.stock.market import MARKET_TYPE
@@ -15,6 +24,9 @@
1524
"TChart",
1625
]
1726

27+
if TYPE_CHECKING:
28+
from pandas import DataFrame
29+
1830

1931
@runtime_checkable
2032
class KisChartBar(Protocol):
@@ -142,9 +154,9 @@ def __iter__(self) -> Iterable[KisChartBar]: ...
142154

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

145-
def __reversed__(self): ...
157+
def __reversed__(self) -> Iterator[KisChartBar]: ...
146158

147-
def df(self):
159+
def df(self) -> "DataFrame":
148160
"""
149161
차트를 Pandas DataFrame으로 변환합니다.
150162
@@ -274,10 +286,10 @@ def __iter__(self) -> Iterable[KisChartBar]:
274286
def __len__(self) -> int:
275287
return len(self.bars)
276288

277-
def __reversed__(self):
289+
def __reversed__(self) -> Iterator[KisChartBar]:
278290
return reversed(self.bars)
279291

280-
def df(self):
292+
def df(self) -> "DataFrame":
281293
"""
282294
차트를 Pandas DataFrame으로 변환합니다.
283295

pykis/api/stock/quote.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from datetime import date
22
from decimal import Decimal
3+
from functools import cached_property
34
from typing import TYPE_CHECKING, Literal, Protocol, runtime_checkable
45

56
from pykis.api.base.product import KisProductBase, KisProductProtocol
@@ -22,7 +23,6 @@
2223
KisInt,
2324
KisString,
2425
)
25-
from pykis.utils.cache import cached, set_cache
2626
from pykis.utils.repr import kis_repr
2727
from pykis.utils.timezone import TIMEZONE
2828

@@ -552,9 +552,9 @@ def change(self) -> Decimal:
552552
"""전일대비"""
553553
return self.price - self.prev_price
554554

555-
@property
556-
@cached
557-
def indicator(self) -> KisForeignIndicator:
555+
# Pylance bug: cached_property[KisForeignIndicator] type inference error.
556+
@cached_property
557+
def indicator(self) -> KisForeignIndicator: # type: ignore
558558
"""종목 지표"""
559559
return foreign_quote(
560560
self.kis,
@@ -563,6 +563,8 @@ def indicator(self) -> KisForeignIndicator:
563563
extended=False,
564564
).indicator
565565

566+
indicator: KisForeignIndicator
567+
566568
open: Decimal = KisDecimal["open"]
567569
"""당일시가"""
568570
high: Decimal = KisDecimal["high"]
@@ -605,14 +607,10 @@ def __pre_init__(self, data: dict):
605607
super().__pre_init__(data)
606608

607609
if not self.extended:
608-
set_cache(
609-
self,
610-
"indicator",
611-
KisObject.transform_(
612-
data["output"],
613-
KisForeignIndicator,
614-
ignore_missing=True,
615-
),
610+
self.indicator = KisObject.transform_(
611+
data["output"],
612+
KisForeignIndicator,
613+
ignore_missing=True,
616614
)
617615

618616

pykis/utils/cache.py

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

pykis/utils/repr.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from functools import wraps
12
from io import StringIO
23
from typing import Any, Iterable, Literal, Protocol, TypeVar
34

@@ -41,6 +42,7 @@ def kis_repr(
4142
max_depth: int = 7,
4243
):
4344
def decorator(cls: type[TObject]) -> type[TObject]:
45+
@wraps(cls.__repr__)
4446
def __repr__(self, _depth: int = 0) -> str:
4547
return object_repr(
4648
self,
@@ -57,7 +59,7 @@ def __repr__(self, _depth: int = 0) -> str:
5759
__repr__.__module__ = cls.__module__
5860
__repr__.__qualname__ = f"{cls.__qualname__}.__repr__"
5961
__repr__.__name__ = "__repr__"
60-
__repr__.__is_kis_repr__ = True
62+
__repr__.__is_kis_repr__ = True # type: ignore
6163

6264
cls.__repr__ = __repr__
6365
return cls

pykis/utils/thread_safe.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from functools import wraps
12
from multiprocessing import Lock
23
from typing import Any, Callable
34

@@ -9,7 +10,8 @@
910

1011

1112
def thread_safe(name: str | None = None):
12-
def decorator(fn: Callable[..., Any]):
13+
def decorator(fn: Callable[..., Any]) -> Callable[..., Any]:
14+
@wraps(fn)
1315
def wrapper(self, *args, **kwargs):
1416
with global_lock:
1517
key = f"__thread_safe_{name or fn.__name__}_lock"

0 commit comments

Comments
 (0)