From 24c239d9912380bc9dab9cf8c0f0b99aa75b1bce Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Fri, 2 Jul 2021 22:37:59 -0700 Subject: [PATCH] openapi: Add missing __init__.py; fix type errors hidden by its absence. The absence of __init__.py was preventing mypy from following any of the zerver.openapi imports. These errors were being silenced by ignore_missing_imports. Signed-off-by: Anders Kaseorg --- zerver/lib/markdown/api_return_values_table_generator.py | 2 -- zerver/openapi/__init__.py | 0 zerver/openapi/markdown_extension.py | 2 +- zerver/openapi/openapi.py | 8 ++++---- zerver/views/documentation.py | 2 ++ 5 files changed, 7 insertions(+), 7 deletions(-) create mode 100644 zerver/openapi/__init__.py diff --git a/zerver/lib/markdown/api_return_values_table_generator.py b/zerver/lib/markdown/api_return_values_table_generator.py index da9dab2145..8582bd4cb7 100644 --- a/zerver/lib/markdown/api_return_values_table_generator.py +++ b/zerver/lib/markdown/api_return_values_table_generator.py @@ -40,9 +40,7 @@ class APIReturnValuesTablePreprocessor(Preprocessor): doc_name = match.group(2) endpoint, method = doc_name.rsplit(":", 1) - return_values: Dict[str, Any] = {} return_values = get_openapi_return_values(endpoint, method) - text: List[str] = [] if doc_name == "/events:get": return_values = copy.deepcopy(return_values) events = return_values["events"].pop("items", None) diff --git a/zerver/openapi/__init__.py b/zerver/openapi/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/zerver/openapi/markdown_extension.py b/zerver/openapi/markdown_extension.py index 3774a2f1d5..0268d1cb6d 100644 --- a/zerver/openapi/markdown_extension.py +++ b/zerver/openapi/markdown_extension.py @@ -474,7 +474,7 @@ class APICodeExamplesPreprocessor(Preprocessor): done = True return lines - def render_fixture(self, function: str, name: Optional[str] = None) -> List[str]: + def render_fixture(self, function: str, name: str) -> List[str]: path, method = function.rsplit(":", 1) return generate_openapi_fixture(path, method, name) diff --git a/zerver/openapi/openapi.py b/zerver/openapi/openapi.py index b97f3a4dd8..f6623e8d8f 100644 --- a/zerver/openapi/openapi.py +++ b/zerver/openapi/openapi.py @@ -8,7 +8,7 @@ import json import os import re -from typing import Any, Dict, List, Optional, Set, Tuple +from typing import Any, Dict, List, Mapping, Optional, Set, Tuple, Union from jsonschema.exceptions import ValidationError as JsonSchemaValidationError from openapi_core import create_spec @@ -320,7 +320,7 @@ def get_openapi_parameters( return parameters -def get_openapi_return_values(endpoint: str, method: str) -> List[Dict[str, Any]]: +def get_openapi_return_values(endpoint: str, method: str) -> Dict[str, Any]: operation = openapi_spec.openapi()["paths"][endpoint][method.lower()] schema = operation["responses"]["200"]["content"]["application/json"]["schema"] # In cases where we have used oneOf, the schemas only differ in examples @@ -491,7 +491,7 @@ def likely_deprecated_parameter(parameter_description: str) -> bool: return "**Deprecated**" in parameter_description -def check_deprecated_consistency(argument: Dict[str, Any], description: str) -> None: +def check_deprecated_consistency(argument: Mapping[str, Any], description: str) -> None: # Test to make sure deprecated parameters are marked so. if likely_deprecated_parameter(description): assert argument["deprecated"] @@ -510,7 +510,7 @@ SKIP_JSON = { def validate_request( url: str, method: str, - data: Dict[str, Any], + data: Union[str, bytes, Dict[str, Any]], http_headers: Dict[str, Any], json_url: bool, status_code: str, diff --git a/zerver/views/documentation.py b/zerver/views/documentation.py index 4b4a36b28d..0f25f63307 100644 --- a/zerver/views/documentation.py +++ b/zerver/views/documentation.py @@ -153,6 +153,8 @@ class MarkdownDirectoryView(ApiURLView): documentation_article.endpoint_path, documentation_article.endpoint_method, ) + assert endpoint_name is not None + assert endpoint_method is not None article_title = get_openapi_summary(endpoint_name, endpoint_method) elif self.path_template == "/zerver/api/%s.md" and "{generate_api_title(" in first_line: api_operation = context["OPEN_GRAPH_URL"].split("/api/")[1].replace("-", "_")