fix typing that wasn't available in Python 3.6.0

This commit is contained in:
David Lord 2021-05-13 17:24:19 -07:00
parent db5fb7971f
commit 9f34d2ac87
No known key found for this signature in database
GPG Key ID: 7A1C87E3F5BC42A8
9 changed files with 24 additions and 13 deletions

View File

@ -8,6 +8,7 @@ Unreleased
- Update MarkupSafe dependency to >= 2.0. :pr:`1418`
- Mark top-level names as exported so type checking understands
imports in user projects. :issue:`1426`
- Fix some types that weren't available in Python 3.6.0. :issue:`1433`
Version 3.0.0

View File

@ -210,7 +210,7 @@ class FileSystemBytecodeCache(BytecodeCache):
self.pattern = pattern
def _get_default_cache_dir(self) -> str:
def _unsafe_dir() -> t.NoReturn:
def _unsafe_dir() -> "te.NoReturn":
raise RuntimeError(
"Cannot determine safe temp directory. You "
"need to explicitly provide one."

View File

@ -23,6 +23,7 @@ from .utils import concat
from .visitor import NodeVisitor
if t.TYPE_CHECKING:
import typing_extensions as te
from .environment import Environment
F = t.TypeVar("F", bound=t.Callable[..., t.Any])
@ -376,7 +377,7 @@ class CodeGenerator(NodeVisitor):
# -- Various compilation helpers
def fail(self, msg: str, lineno: int) -> t.NoReturn:
def fail(self, msg: str, lineno: int) -> "te.NoReturn":
"""Fail with a :exc:`TemplateAssertionError`."""
raise TemplateAssertionError(msg, lineno, self.name, self.filename)

View File

@ -915,7 +915,7 @@ class Environment:
return names
def handle_exception(self, source: t.Optional[str] = None) -> t.NoReturn:
def handle_exception(self, source: t.Optional[str] = None) -> "te.NoReturn":
"""Exception handling helper. This is used internally to either raise
rewritten exceptions or return a rendered traceback for the template.
"""

View File

@ -14,6 +14,7 @@ from .exceptions import TemplateSyntaxError
from .utils import LRUCache
if t.TYPE_CHECKING:
import typing_extensions as te
from .environment import Environment
# cache for the lexers. Exists in order to be able to have multiple
@ -259,7 +260,7 @@ class Failure:
self.message = message
self.error_class = cls
def __call__(self, lineno: int, filename: str) -> t.NoReturn:
def __call__(self, lineno: int, filename: str) -> "te.NoReturn":
raise self.error_class(self.message, lineno, filename)
@ -326,7 +327,7 @@ class TokenStream:
filename: t.Optional[str],
):
self._iter = iter(generator)
self._pushed: t.Deque[Token] = deque()
self._pushed: "te.Deque[Token]" = deque()
self.name = name
self.filename = filename
self.closed = False

View File

@ -12,6 +12,7 @@ from markupsafe import Markup
from .utils import _PassArg
if t.TYPE_CHECKING:
import typing_extensions as te
from .environment import Environment
_NodeBound = t.TypeVar("_NodeBound", bound="Node")
@ -1196,7 +1197,7 @@ class ScopedEvalContextModifier(EvalContextModifier):
# make sure nobody creates custom nodes
def _failing_new(*args: t.Any, **kwargs: t.Any) -> t.NoReturn:
def _failing_new(*args: t.Any, **kwargs: t.Any) -> "te.NoReturn":
raise TypeError("can't create custom node types")

View File

@ -76,7 +76,7 @@ class Parser:
msg: str,
lineno: t.Optional[int] = None,
exc: t.Type[TemplateSyntaxError] = TemplateSyntaxError,
) -> t.NoReturn:
) -> "te.NoReturn":
"""Convenience method that raises `exc` with the message, passed
line number or last line number as well as the current name and
filename.
@ -90,7 +90,7 @@ class Parser:
name: t.Optional[str],
end_token_stack: t.List[t.Tuple[str, ...]],
lineno: t.Optional[int],
) -> t.NoReturn:
) -> "te.NoReturn":
expected: t.Set[str] = set()
for exprs in end_token_stack:
expected.update(map(describe_token_expr, exprs))
@ -125,7 +125,9 @@ class Parser:
self.fail(" ".join(message), lineno)
def fail_unknown_tag(self, name: str, lineno: t.Optional[int] = None) -> t.NoReturn:
def fail_unknown_tag(
self, name: str, lineno: t.Optional[int] = None
) -> "te.NoReturn":
"""Called if the parser encounters an unknown tag. Tries to fail
with a human readable error message that could help to identify
the problem.
@ -136,7 +138,7 @@ class Parser:
self,
end_tokens: t.Optional[t.Tuple[str, ...]] = None,
lineno: t.Optional[int] = None,
) -> t.NoReturn:
) -> "te.NoReturn":
"""Like fail_unknown_tag but for end of template situations."""
stack = list(self._end_token_stack)
if end_tokens is not None:

View File

@ -894,7 +894,9 @@ class Undefined:
)
@internalcode
def _fail_with_undefined_error(self, *args: t.Any, **kwargs: t.Any) -> t.NoReturn:
def _fail_with_undefined_error(
self, *args: t.Any, **kwargs: t.Any
) -> "te.NoReturn":
"""Raise an :exc:`UndefinedError` when operations are performed
on the undefined value.
"""
@ -985,7 +987,7 @@ def make_logging_undefined(
def _fail_with_undefined_error( # type: ignore
self, *args: t.Any, **kwargs: t.Any
) -> t.NoReturn:
) -> "te.NoReturn":
try:
super()._fail_with_undefined_error(*args, **kwargs)
except self._undefined_exception as e:

View File

@ -14,6 +14,9 @@ from urllib.parse import quote_from_bytes
import markupsafe
if t.TYPE_CHECKING:
import typing_extensions as te
F = t.TypeVar("F", bound=t.Callable[..., t.Any])
# special singleton representing missing values for the runtime
@ -503,7 +506,7 @@ class LRUCache:
def __init__(self, capacity: int) -> None:
self.capacity = capacity
self._mapping: t.Dict[t.Any, t.Any] = {}
self._queue: t.Deque[t.Any] = deque()
self._queue: "te.Deque[t.Any]" = deque()
self._postinit()
def _postinit(self) -> None: