Skip to content

Commit c76a298

Browse files
MSK61JelleZijlstra
authored andcommitted
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.
1 parent 90004af commit c76a298

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

stdlib/3/string.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ class Template:
2020
template: str
2121

2222
def __init__(self, template: str) -> None: ...
23-
def substitute(self, mapping: Mapping[str, str] = ..., **kwds: str) -> str: ...
24-
def safe_substitute(self, mapping: Mapping[str, str] = ...,
25-
**kwds: str) -> str: ...
23+
def substitute(self, mapping: Mapping[str, object] = ..., **kwds: object) -> str: ...
24+
def safe_substitute(self, mapping: Mapping[str, object] = ...,
25+
**kwds: object) -> str: ...
2626

2727
# TODO(MichalPokorny): This is probably badly and/or loosely typed.
2828
class Formatter:

0 commit comments

Comments
 (0)