본문으로 건너뛰기

[Ray Data] 중복 batch_format 유효성 검사 제거

PR 링크: ray-project/ray#59900 상태: Merged | 변경: +3 / -9

들어가며

코드가 성장하면서 같은 유효성 검사가 여러 곳에 중복되는 경우가 있습니다. Ray Data의 map_batches에서 batch_format 검증이 바로 그런 사례입니다. _apply_batch_format 함수 내부에서 이미 VALID_BATCH_FORMATS를 검사하는데, 호출부에서도 동일한 검사를 수행하고 있었습니다. 이 PR은 도달할 수 없는 중복 검사를 제거하고, 타입 어노테이션도 함께 수정합니다.

핵심 코드 분석

중복 검증 제거

Before:

def _map_batches_without_batch_size_validation(self, ...):
    batch_format = _apply_batch_format(batch_format)
    # _apply_batch_format 내부에서 이미 같은 검사를 수행하므로 도달 불가능
    if batch_format not in VALID_BATCH_FORMATS:
        raise ValueError(
            f"The batch format must be one of {VALID_BATCH_FORMATS}, got: "
            f"{batch_format}"
        )

After:

def _map_batches_without_batch_size_validation(self, ...):
    batch_format = _apply_batch_format(batch_format)
    # 중복 검증 제거 - _apply_batch_format 내부에서 처리됨

타입 어노테이션 수정

# Before
def _apply_batch_format(given_batch_format: Optional[str]) -> str:

# After - None도 유효한 batch_format임을 반영
def _apply_batch_format(given_batch_format: Optional[str]) -> Optional[str]:

왜 이게 좋은가

  • 데드 코드 제거: 실행될 수 없는 코드를 제거하여 코드베이스를 깔끔하게 유지합니다.
  • 유지보수성 향상: 유효성 검사 로직이 _apply_batch_format 한 곳에만 존재하므로, 향후 수정 시 한 곳만 변경하면 됩니다.
  • 타입 정확성: None이 유효한 batch_format값임을 반환 타입에 정확히 반영합니다.
  • 불필요한 import 제거: VALID_BATCH_FORMATS를 더 이상 사용하지 않으므로 import에서도 제거됩니다.

참고 자료

댓글

관련 포스트

PR Analysis 의 다른글