hf_argparser: fix TypeError when resolving Optional of generic type#46009
hf_argparser: fix TypeError when resolving Optional of generic type#46009Dev-X25874 wants to merge 1 commit into
Conversation
|
This PR was flagged by our automated quality checks. If you're a genuine Common reasons for flagging:
We appreciate your contribution and apologize if this is a false positive! |
|
Hi, I understand the automated flag and appreciate the bot catching low-quality submissions — the repo clearly needs it. To clarify: I found this bug manually by reading through I'm happy to answer any questions about the bug or the reasoning behind the fix to demonstrate this. The change is a single expression on line 185 — genuinely as small as it looks. |
What does this PR do?
Fixes a
TypeErrorinHfArgumentParser._parse_dataclass_fieldwhen a dataclass field is typed asOptional[X]whereXis a subscripted generic (e.g.Optional[List[str]],Optional[list[str]],Optional[Dict[str, int]]).The root cause is on line 185, where
isinstance(None, field.type.__args__[1])is used to detect whether the second type argument isNoneType. This works for plain classes likeintorstr, but raisesTypeError: Subscripted generics cannot be used with class and instance checkswhenargs[1]is a generic alias — which happens whenNoneappears first in the union, e.g.Union[None, List[str]].The fix replaces the
isinstancecall with an identity check:field.type.__args__[1] is type(None). This is the correct, idiomatic way to test forNoneTypeand mirrors the pattern already used two lines above (type(None) not in field.type.__args__). It is safe for all types including generics, and produces identical results for plain classes whereisinstancehappened to work.Fixes # (issue)
Code Agent Policy
Before submitting
Who can review?
@ArthurZucker