mypy: Fix annotations for json_encoder_for_html.

This commit is contained in:
Tim Abbott 2017-11-22 01:32:18 -08:00
parent 3ec90f8b33
commit 8f6b39a1c0
1 changed files with 3 additions and 4 deletions

View File

@ -1,4 +1,5 @@
import json import json
from typing import Any, Dict, Iterator, Optional
# Taken from # Taken from
# https://github.com/simplejson/simplejson/blob/8edc82afcf6f7512b05fba32baa536fe756bd273/simplejson/encoder.py#L378-L402 # https://github.com/simplejson/simplejson/blob/8edc82afcf6f7512b05fba32baa536fe756bd273/simplejson/encoder.py#L378-L402
@ -11,15 +12,13 @@ class JSONEncoderForHTML(json.JSONEncoder):
within <script> tags. within <script> tags.
""" """
def encode(self, o): def encode(self, o: Any) -> str:
# type: (Dict[str, Any]) -> str
# Override JSONEncoder.encode because it has hacks for # Override JSONEncoder.encode because it has hacks for
# performance that make things more complicated. # performance that make things more complicated.
chunks = self.iterencode(o, True) chunks = self.iterencode(o, True)
return ''.join(chunks) return ''.join(chunks)
def iterencode(self, o, _one_shot=False): def iterencode(self, o: Any, _one_shot: bool=False) -> Iterator[str]:
# type: (Dict[str, Any], Optional[bool]) -> Iterator[str]
chunks = super().iterencode(o, _one_shot) chunks = super().iterencode(o, _one_shot)
for chunk in chunks: for chunk in chunks:
chunk = chunk.replace('&', '\\u0026') chunk = chunk.replace('&', '\\u0026')