Repository navigation
fix: log a warning on KRX empty responses instead of printing (#294) - #298
Closed
kvandre12-commits wants to merge 1 commit into
Closed
kvandre12-commits wants to merge 1 commit into
kvandre12-commits wants to merge 1 commit into
Conversation
…ook-kr#294) `dataframe_empty_handler` already caught the empty-response error (commonly `ValueError: Length mismatch` raised when column names are assigned to an empty frame), but it reported the failure via `print()` to stdout. Users hitting KRX rate limits under request load therefore saw output that looked like a crash (sharebook-kr#294). The handler also called `logging.info(args, kwargs)`, which misuses the logging API (the kwargs dict is passed as the %-format args). Replace the `print()` / buggy `logging.info()` with a single, suppressible `logger.warning(...)`, while keeping the existing behaviour of returning an empty DataFrame. This aligns with the graceful empty-response handling referenced in sharebook-kr#150. Also add offline unit tests for the decorator: empty response -> empty DataFrame + a warning record, passthrough of non-empty results, and KeyError / JSONDecodeError handling.
Collaborator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #294.
When KRX intermittently returns an empty response (e.g. under request load / rate limiting),
dataframe_empty_handleralready caught the resulting exception — most commonlyValueError: Length mismatch: Expected axis has 0 elements, new values have N elements, raised when the expected column names are assigned to an empty frame — and returned an emptyDataFrame.The problem is how it reported that: it used
print()to stdout, so callers hitting KRX rate limits saw output that looks like a crash even though the call actually returned an (empty) result. It also calledlogging.info(args, kwargs), which misuses the logging API — thekwargsdict is passed as the%-format args, which would raise insideloggingif INFO logging were enabled.This aligns with the intent of #150, where empty responses were made to degrade gracefully instead of surfacing as errors.
Change
print()+ buggylogging.info(args, kwargs)with a single, suppressiblelogger.warning(...)on a module-level logger.DataFrameis still returned, and the same exception types are still handled.logging.getLogger("pykrx").setLevel(logging.ERROR).Before (simulated empty KRX response)
Printed to stdout — indistinguishable from a real crash.
After
A single
WARNINGlog record (suppressible via the standardloggingconfig), and the same emptyDataFramereturn value.Tests
tests/test_dataframe_empty_handler.py:DataFrame+ a warning record (no exception, no stdout print),KeyError/json.JSONDecodeErrorhandling.pytest tests/integration/test_market_api.py -k "net_purchase or equities or ohlcv_by_date"→ 5 passed.ruff checkandruff format --checkpass.Notes
No public API or return-value changes — this only changes how the empty-response condition is reported (stdout
print→warninglog).요약 (KR)
KRX가 요청 부하/레이트리밋 등으로 일시적으로 빈 응답을 줄 때(#294),
dataframe_empty_handler는 이미 예외(대개 빈 프레임에 컬럼명을 지정하며 발생하는ValueError: Length mismatch)를 잡아 빈DataFrame을 반환하고 있었습니다. 다만 그 사실을print()로 stdout에 출력해 마치 크래시처럼 보였고,logging.info(args, kwargs)호출은 logging API를 잘못 사용합니다. 이를 억제 가능한logger.warning(...)한 줄로 교체했습니다(반환값·동작 동일, 빈DataFrame반환). 이는 #150의 처리 방향과 일치합니다. 데코레이터에 대한 오프라인 단위 테스트를 추가했으며 기존 통합 테스트도 통과합니다.