From deebd1e371cd448de3bf58d178d6d510b5242ed9 Mon Sep 17 00:00:00 2001 From: Zixuan James Li Date: Wed, 16 Aug 2023 18:35:10 -0400 Subject: [PATCH] api: Avoid programming errors due to nested Annotated types. We want to reject ambiguous type annotations that set ApiParamConfig inside a Union. If a parameter is Optional and has a default of None, we prefer Annotated[Optional[T], ...] over Optional[Annotated[T, ...]]. This implements a check that detects Optional[Annotated[T, ...]] and raise an assertion error if ApiParamConfig is in the annotation. It also checks if the type annotation contains any ApiParamConfig objects that are ignored, which can happen if the Annotated type is nested inside another type like List, Union, etc. Note that because param: Annotated[Optional[T], ...] = None and param: Optional[Annotated[Optional[T], ...]] = None are equivalent in runtime prior to Python 3.11, there is no way for us to distinguish the two. So we cannot detect that in runtime. See also: https://github.com/python/cpython/issues/90353 --- zerver/lib/typed_endpoint.py | 66 ++++++++++++++++++++++++++--- zerver/tests/test_typed_endpoint.py | 50 +++++++++++++++++++++- 2 files changed, 108 insertions(+), 8 deletions(-) diff --git a/zerver/lib/typed_endpoint.py b/zerver/lib/typed_endpoint.py index df892a87d08886..618f3795848d4c 100644 --- a/zerver/lib/typed_endpoint.py +++ b/zerver/lib/typed_endpoint.py @@ -1,5 +1,6 @@ import inspect import json +import sys from dataclasses import dataclass from enum import Enum, auto from functools import wraps @@ -194,6 +195,40 @@ def is_annotated(type_annotation: Type[object]) -> bool: return origin is Annotated +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 + + +API_PARAM_CONFIG_USAGE_HINT = f""" + Detected incorrect usage of Annotated types for parameter {{param_name}}! + Check the placement of the {ApiParamConfig.__name__} object in the type annotation: + + {{param_name}}: {{param_type}} + + The Annotated[T, ...] type annotation containing the + {ApiParamConfig.__name__} object should not be nested inside another type. + + Correct examples: + + # Using Optional inside Annotated when the default is None + param: Annotated[Optional[int], ApiParamConfig(...)]] = None + + # Not using Optional when the default is not None + param: Annotated[int, ApiParamConfig(...)] + + Incorrect examples: + + # Nesting Annotated inside Optional regardless of the default + param: Optional[Annotated[int, ApiParamConfig(...)]] + param: Optional[Annotated[int, ApiParamConfig(...)]] = None + + # Nesting the Annotated type carrying ApiParamConfig inside other types like Union + param: Union[str, Annotated[int, ApiParamConfig(...)]] +""" + + def parse_single_parameter( param_name: str, param_type: Type[T], parameter: inspect.Parameter ) -> FuncParam[T]: @@ -208,13 +243,22 @@ def parse_single_parameter( # otherwise causes undesired behaviors that the annotated metadata gets # lost. This is fixed in Python 3.11: # https://github.com/python/cpython/issues/90353 - if param_default is None: - origin = get_origin(param_type) + if sys.version_info < (3, 11) and param_default is None: type_args = get_args(param_type) - if origin is Union and type(None) in type_args and len(type_args) == 2: - inner_type = type_args[0] if type_args[1] is type(None) else type_args[1] - if is_annotated(inner_type): - param_type = inner_type + assert is_optional(param_type) + inner_type = type_args[0] if type_args[1] is type(None) else type_args[1] + if is_annotated(inner_type): + annotated_type, *annotations = get_args(inner_type) + has_api_param_config = any( + isinstance(annotation, ApiParamConfig) for annotation in annotations + ) + # This prohibits the use of `Optional[Annotated[T, ApiParamConfig(...)]] = None` + # and encourage `Annotated[Optional[T], ApiParamConfig(...)] = None` + # to avoid confusion when the parameter metadata is unintentionally nested. + assert not has_api_param_config or is_optional( + annotated_type + ), API_PARAM_CONFIG_USAGE_HINT.format(param_name=param_name, param_type=param_type) + param_type = inner_type param_config: Optional[ApiParamConfig] = None json_wrapper = False @@ -223,7 +267,7 @@ def parse_single_parameter( # metadata attached to Annotated. Note that we do not transform # param_type to its underlying type because the Annotated metadata might # still be needed by other parties like Pydantic. - _, *annotations = get_args(param_type) + ignored_type, *annotations = get_args(param_type) for annotation in annotations: if type(annotation) is Json: json_wrapper = True @@ -231,6 +275,14 @@ def parse_single_parameter( continue assert param_config is None, "ApiParamConfig can only be defined once per parameter" param_config = annotation + else: + # When no parameter configuration is found, assert that there is none + # nested somewhere in a Union type to avoid silently ignoring it. If it + # does present in the stringified parameter type, it is very likely a + # programming error. + assert ApiParamConfig.__name__ not in str(param_type), API_PARAM_CONFIG_USAGE_HINT.format( + param_name=param_name, param_type=param_type + ) # Set param_config to a default early to avoid additional None-checks. if param_config is None: param_config = ApiParamConfig() diff --git a/zerver/tests/test_typed_endpoint.py b/zerver/tests/test_typed_endpoint.py index 185204d83216cd..8b21810d57f432 100644 --- a/zerver/tests/test_typed_endpoint.py +++ b/zerver/tests/test_typed_endpoint.py @@ -3,7 +3,7 @@ import orjson from django.core.exceptions import ValidationError as DjangoValidationError from django.http import HttpRequest, HttpResponse -from pydantic import BaseModel, ConfigDict, Json +from pydantic import BaseModel, ConfigDict, Json, StringConstraints from pydantic.dataclasses import dataclass from typing_extensions import Annotated @@ -400,6 +400,54 @@ def view3( ) self.assertFalse(result) + # Not nesting the Annotated type with the ApiParamConfig inside Optional is fine + @typed_endpoint + def view4( + request: HttpRequest, + bar: Annotated[ + Optional[str], + StringConstraints(strip_whitespace=True, max_length=3), + ApiParamConfig("test"), + ] = None, + ) -> None: + pass + + with self.assertRaisesMessage(ApiParamValidationError, "test is too long"): + call_endpoint(view4, HostRequestMock({"test": "long"})) + + # Nesting Annotated with ApiParamConfig inside Optional is not fine + def view5( + request: HttpRequest, + bar: Optional[Annotated[str, ApiParamConfig("test")]] = None, + ) -> None: + pass + + with self.assertRaisesRegex( + AssertionError, "Detected incorrect usage of Annotated types for parameter bar!" + ): + typed_endpoint(view5) + + # Not nesting Annotated inside Optional with a None default is fine + @typed_endpoint + def view6( + request: HttpRequest, + bar: Annotated[Optional[str], ApiParamConfig("test")] = None, + ) -> str: + return bar or "no input" + + self.assertEqual(call_endpoint(view6, HostRequestMock({"test": "long"})), "long") + + # Nesting Annotated inside Optional, when ApiParamConfig is not also nested is fine + @typed_endpoint + def view7( + request: HttpRequest, + bar: Optional[Annotated[str, StringConstraints(max_length=3)]] = None, + ) -> None: + pass + + with self.assertRaisesMessage(ApiParamValidationError, "bar is too long"): + call_endpoint(view7, HostRequestMock({"bar": "long"})) + def test_aliases(self) -> None: @typed_endpoint def foo(