Skip to content

修复_load_imgs中isinstance(img_content,InputType)错误#140

Merged
SWHL merged 2 commits into
RapidAI:mainfrom
hbh112233abc:main
Sep 4, 2025
Merged

修复_load_imgs中isinstance(img_content,InputType)错误#140
SWHL merged 2 commits into
RapidAI:mainfrom
hbh112233abc:main

Conversation

@hbh112233abc

Copy link
Copy Markdown
Contributor

原来的代码,判断类型会报错 TypeError: Subscripted generics cannot be used with class and instance checks

img_contents = (
       [img_content] if isinstance(img_content, InputType) else img_content
)

修复如下:

  1. 对传入的类型先统一判断,如果类型不符合要求就提前抛出异常
  2. _load_imgs仅判断是否list,否则就组装为list

@SWHL
SWHL requested a review from Copilot September 4, 2025 01:21

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a TypeError in the _load_imgs method where isinstance(img_content, InputType) fails because subscripted generics cannot be used with class and instance checks. The fix introduces early type validation using get_args() and simplifies the list handling logic.

  • Adds early type validation in the __call__ method to check input types before processing
  • Simplifies _load_imgs to only check if input is a list rather than using isinstance with InputType
  • Imports get_args from typing module to enable proper type checking of Union types

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment thread rapid_table/main.py
Comment on lines +78 to +85
for img_content in img_contents:
if not isinstance(img_content, get_args(InputType)):
type_names = ", ".join([t.__name__ for t in get_args(InputType)])
actual_type = (
type(img_content).__name__ if img_content is not None else "None"
)
raise TypeError(
f"Type Error: Expected input of type [{type_names}], but received '{img_content}' of type {actual_type}."

Copilot AI Sep 4, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message includes the actual value '{img_content}' which could expose sensitive data or create overly verbose output for large objects. Consider removing the value from the error message and only showing the type information.

Suggested change
for img_content in img_contents:
if not isinstance(img_content, get_args(InputType)):
type_names = ", ".join([t.__name__ for t in get_args(InputType)])
actual_type = (
type(img_content).__name__ if img_content is not None else "None"
)
raise TypeError(
f"Type Error: Expected input of type [{type_names}], but received '{img_content}' of type {actual_type}."
f"Type Error: Expected input of type [{type_names}], but received type {actual_type}."

Copilot uses AI. Check for mistakes.
Comment thread rapid_table/main.py
img_contents = (
[img_content] if isinstance(img_content, InputType) else img_content
)
img_contents = img_content if isinstance(img_content, list) else [img_content]

Copilot AI Sep 4, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change breaks the original contract where _load_imgs accepted Union[Sequence[InputType], InputType] but now only checks for list. Other sequence types like tuples would incorrectly be wrapped in a list. Consider using isinstance(img_content, (list, tuple)) or checking against Sequence to maintain the original API contract.

Suggested change
img_contents = img_content if isinstance(img_content, list) else [img_content]
img_contents = (
img_content
if isinstance(img_content, Sequence) and not isinstance(img_content, (str, bytes))
else [img_content]
)

Copilot uses AI. Check for mistakes.

@SWHL SWHL left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@SWHL
SWHL merged commit 4dce0a5 into RapidAI:main Sep 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants