mirror of
https://github.com/davidhalter/typeshed.git
synced 2025-12-08 13:04:46 +08:00
I did a a cursory investigation using GitHub search and also looked at
a big internal codebase, and a significant fraction of callsites used
a dict type instead of a TypedDict or a dict literal.
It seems that it's a common use case to store the config within an
attribute. For example, something like this:
```
CONFIG = {
...
}
...
logging.config.dictConfig(CONFIG)
```
Another use case that was not properly supported is reading the
config from a file, and the config is given `dict[str, Any]` as
the type.
Mypy can still do some type checking of the argument if called
with a dict literal, so I feel this is a reasonable compromise
between type checking strictness and usability.
69 lines
2.2 KiB
Python
69 lines
2.2 KiB
Python
import sys
|
|
from _typeshed import StrOrBytesPath, StrPath
|
|
from collections.abc import Callable
|
|
from configparser import RawConfigParser
|
|
from threading import Thread
|
|
from typing import IO, Any, Pattern, Sequence
|
|
|
|
from . import _Level
|
|
|
|
if sys.version_info >= (3, 8):
|
|
from typing import Literal, TypedDict
|
|
else:
|
|
from typing_extensions import Literal, TypedDict
|
|
|
|
if sys.version_info >= (3, 7):
|
|
_Path = StrOrBytesPath
|
|
else:
|
|
_Path = StrPath
|
|
|
|
DEFAULT_LOGGING_CONFIG_PORT: int
|
|
RESET_ERROR: int # undocumented
|
|
IDENTIFIER: Pattern[str] # undocumented
|
|
|
|
class _RootLoggerConfiguration(TypedDict, total=False):
|
|
level: _Level
|
|
filters: Sequence[str]
|
|
handlers: Sequence[str]
|
|
|
|
class _LoggerConfiguration(_RootLoggerConfiguration, TypedDict, total=False):
|
|
propagate: bool
|
|
|
|
class _OptionalDictConfigArgs(TypedDict, total=False):
|
|
# these two can have custom factories (key: `()`) which can have extra keys
|
|
formatters: dict[str, dict[str, Any]]
|
|
filters: dict[str, dict[str, Any]]
|
|
# type checkers would warn about extra keys if this was a TypedDict
|
|
handlers: dict[str, dict[str, Any]]
|
|
loggers: dict[str, _LoggerConfiguration]
|
|
root: _RootLoggerConfiguration | None
|
|
incremental: bool
|
|
disable_existing_loggers: bool
|
|
|
|
class _DictConfigArgs(_OptionalDictConfigArgs, TypedDict):
|
|
version: Literal[1]
|
|
|
|
# Accept dict[str, Any] to avoid false positives if called with a dict
|
|
# type, since dict types are not compatible with TypedDicts.
|
|
#
|
|
# Also accept a TypedDict type, to allow callers to use TypedDict
|
|
# types, and for somewhat stricter type checking of dict literals.
|
|
def dictConfig(config: _DictConfigArgs | dict[str, Any]) -> None: ...
|
|
|
|
if sys.version_info >= (3, 10):
|
|
def fileConfig(
|
|
fname: _Path | IO[str] | RawConfigParser,
|
|
defaults: dict[str, str] | None = ...,
|
|
disable_existing_loggers: bool = ...,
|
|
encoding: str | None = ...,
|
|
) -> None: ...
|
|
|
|
else:
|
|
def fileConfig(
|
|
fname: _Path | IO[str] | RawConfigParser, defaults: dict[str, str] | None = ..., disable_existing_loggers: bool = ...
|
|
) -> None: ...
|
|
|
|
def valid_ident(s: str) -> Literal[True]: ... # undocumented
|
|
def listen(port: int = ..., verify: Callable[[bytes], bytes | None] | None = ...) -> Thread: ...
|
|
def stopListening() -> None: ...
|