typed_endpoint: Make is_optional understand T | None.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2024-07-11 17:59:20 -07:00 committed by Tim Abbott
parent f23fdede67
commit a2fad74a28
1 changed files with 2 additions and 1 deletions

View File

@ -1,5 +1,6 @@
import inspect
import json
import types
from dataclasses import dataclass
from enum import Enum, auto
from functools import wraps
@ -169,7 +170,7 @@ def is_annotated(type_annotation: Type[object]) -> bool:
def is_optional(type_annotation: Type[object]) -> bool:
origin = get_origin(type_annotation)
type_args = get_args(type_annotation)
return origin is Union and type(None) in type_args and len(type_args) == 2
return origin in (Union, types.UnionType) and type(None) in type_args and len(type_args) == 2
API_PARAM_CONFIG_USAGE_HINT = f"""