Fix type hints in Template class (#3491)

Nothing in the standard library documentation for the string module suggests that the value associated with any key in the mapping parameter(or kwds) to Template.substitute and Template.safe_substitute should be a string. In fact any object can be used, for example

Template("$number is a number.").substitute({"number": 1})

The above code sample currently causes an error message like this:

error: Dict entry 0 has incompatible type "str": "int"; expected "str": "str"

which obviously shouldn't be emitted. Also a similar logic is already in place for methods in the Formatter class. However as I saw the notice about loose types above the Formatter class, I opted to use `object` instead of `Any` as the implementation inside the affected functions just uses the built-in str function on values inside mappings.
This commit is contained in:
Mohammed El-Afifi
2019-11-25 03:16:48 +01:00
committed by Jelle Zijlstra
parent 90004af132
commit c76a298ffa

View File

@@ -20,9 +20,9 @@ class Template:
template: str
def __init__(self, template: str) -> None: ...
def substitute(self, mapping: Mapping[str, str] = ..., **kwds: str) -> str: ...
def safe_substitute(self, mapping: Mapping[str, str] = ...,
**kwds: str) -> str: ...
def substitute(self, mapping: Mapping[str, object] = ..., **kwds: object) -> str: ...
def safe_substitute(self, mapping: Mapping[str, object] = ...,
**kwds: object) -> str: ...
# TODO(MichalPokorny): This is probably badly and/or loosely typed.
class Formatter: