mirror of
https://github.com/davidhalter/typeshed.git
synced 2025-12-07 04:34:28 +08:00
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.
40 lines
1.5 KiB
Python
40 lines
1.5 KiB
Python
# Stubs for string
|
|
|
|
# Based on http://docs.python.org/3.2/library/string.html
|
|
|
|
from typing import Mapping, Sequence, Any, Optional, Union, Tuple, Iterable
|
|
|
|
ascii_letters: str
|
|
ascii_lowercase: str
|
|
ascii_uppercase: str
|
|
digits: str
|
|
hexdigits: str
|
|
octdigits: str
|
|
punctuation: str
|
|
printable: str
|
|
whitespace: str
|
|
|
|
def capwords(s: str, sep: str = ...) -> str: ...
|
|
|
|
class Template:
|
|
template: str
|
|
|
|
def __init__(self, template: str) -> None: ...
|
|
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:
|
|
def format(self, format_string: str, *args: Any, **kwargs: Any) -> str: ...
|
|
def vformat(self, format_string: str, args: Sequence[Any],
|
|
kwargs: Mapping[str, Any]) -> str: ...
|
|
def parse(self, format_string: str) -> Iterable[Tuple[str, Optional[str], Optional[str], Optional[str]]]: ...
|
|
def get_field(self, field_name: str, args: Sequence[Any],
|
|
kwargs: Mapping[str, Any]) -> Any: ...
|
|
def get_value(self, key: Union[int, str], args: Sequence[Any], kwargs: Mapping[str, Any]) -> Any: ...
|
|
def check_unused_args(self, used_args: Sequence[Union[int, str]], args: Sequence[Any],
|
|
kwargs: Mapping[str, Any]) -> None: ...
|
|
def format_field(self, value: Any, format_spec: str) -> Any: ...
|
|
def convert_field(self, value: Any, conversion: str) -> Any: ...
|