Skip to content

Commit 457747d

Browse files
mstoclaude
andcommitted
refactor: delegate _attribute_is_optional to types._is_optional
Address review feedback on #295. Removes the duplicated dual-origin check from _attribute_is_optional; the two functions now share the single implementation in types._is_optional. Minor semantic tightening: Union[A, B, None]-style fields (3+ args) now return False from _attribute_is_optional, matching _is_optional's exactly-two-args rule. Tracked separately in a follow-up issue. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 9a07c80 commit 457747d

1 file changed

Lines changed: 1 addition & 5 deletions

File tree

fgpyo/util/inspect.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
from typing import TypeAlias
2525
from typing import TypeGuard
2626
from typing import TypeVar
27-
from typing import Union
2827

2928
import fgpyo.util.types as types
3029

@@ -491,10 +490,7 @@ def attr_from(
491490

492491
def _attribute_is_optional(attribute: FieldType) -> bool:
493492
"""Returns True if the attribute is optional, False otherwise."""
494-
origin = typing.get_origin(attribute.type)
495-
return (origin is Union or origin is python_types.UnionType) and isinstance(
496-
None, typing.get_args(attribute.type)
497-
)
493+
return types._is_optional(attribute.type)
498494

499495

500496
def _attribute_has_default(attribute: FieldType) -> bool:

0 commit comments

Comments
 (0)