Skip to content

Commit a6092ef

Browse files
authored
Merge pull request #696 from TotallyNotRobots/types
style(cloudbot/config.py): add type annotations for Config object
2 parents bf66128 + 9954abf commit a6092ef

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

cloudbot/config.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,42 @@
44
import time
55
from collections import OrderedDict
66
from pathlib import Path
7+
from typing import TYPE_CHECKING, Dict, Optional, cast
8+
9+
if TYPE_CHECKING:
10+
from cloudbot.bot import AbstractBot
711

812
logger = logging.getLogger("cloudbot")
913

1014

1115
class Config(OrderedDict):
12-
def __init__(self, bot, *, filename="config.json"):
16+
def __init__(
17+
self, bot: "AbstractBot", *, filename: str = "config.json"
18+
) -> None:
1319
super().__init__()
1420
self.filename = filename
1521
self.path = Path(self.filename).resolve()
1622
self.bot = bot
1723

18-
self._api_keys = {}
24+
self._api_keys: Dict[str, Optional[str]] = {}
1925

2026
# populate self with config data
2127
self.load_config()
2228

23-
def get_api_key(self, name, default=None):
29+
def get_api_key(
30+
self, name: str, default: Optional[str] = None
31+
) -> Optional[str]:
2432
try:
2533
return self._api_keys[name]
2634
except LookupError:
27-
self._api_keys[name] = value = self.get("api_keys", {}).get(
28-
name, default
35+
value = cast(
36+
Optional[str], self.get("api_keys", {}).get(name, default)
2937
)
38+
39+
self._api_keys[name] = value
3040
return value
3141

32-
def load_config(self):
42+
def load_config(self) -> None:
3343
"""(re)loads the bot config from the config file"""
3444
self._api_keys.clear()
3545
if not self.path.exists():
@@ -50,7 +60,7 @@ def load_config(self):
5060
self.update(data)
5161
logger.debug("Config loaded from file.")
5262

53-
def save_config(self):
63+
def save_config(self) -> None:
5464
"""saves the contents of the config dict to the config file"""
5565
with self.path.open("w", encoding="utf-8") as f:
5666
json.dump(self, f, indent=4)

pyproject.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ module = 'cloudbot.*'
114114
check_untyped_defs = true
115115
warn_return_any = true
116116

117+
[[tool.mypy.overrides]]
118+
module = 'cloudbot.config'
119+
check_untyped_defs = true
120+
warn_return_any = true
121+
disallow_untyped_defs = true
122+
117123
[tool.commitizen]
118124
name = "cz_conventional_commits"
119125
tag_format = "v$version"

0 commit comments

Comments
 (0)