Skip to content

Commit a73021c

Browse files
Upgrade type annotations
1 parent d87d05c commit a73021c

11 files changed

Lines changed: 39 additions & 37 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
- Upgrade Python versions in workflow.
1111
- Remove support for Python <= 3.10.
1212
- Remove Codecov from README and workflow.
13+
- Upgrade type annotations.
1314

1415
## [2.0.4] - 2023-12-28 :snowman_with_snow:
1516

config/common/__init__.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from abc import ABC, abstractmethod
22
from collections import abc
3-
from typing import Any, Dict, List, Mapping, Optional, Type, TypeVar
3+
from typing import Any, Mapping, Type, TypeVar
44

55
from deepmerge import Merger
66

@@ -97,7 +97,7 @@ def merge_values(destination: Mapping[str, Any], source: Mapping[str, Any]) -> N
9797

9898
class ConfigurationSource(ABC):
9999
@abstractmethod
100-
def get_values(self) -> Dict[str, Any]:
100+
def get_values(self) -> dict[str, Any]:
101101
"""Returns the values read from this source."""
102102

103103
def __repr__(self) -> str:
@@ -112,7 +112,7 @@ def __init__(self, values: Mapping[str, Any]) -> None:
112112
super().__init__()
113113
self._values = dict(values.items())
114114

115-
def get_values(self) -> Dict[str, Any]:
115+
def get_values(self) -> dict[str, Any]:
116116
return self._values
117117

118118

@@ -136,11 +136,11 @@ def __new__(cls, arg=None):
136136
return [cls(item) for item in arg]
137137
return arg
138138

139-
def __init__(self, mapping: Optional[Mapping[str, Any]] = None):
139+
def __init__(self, mapping: Mapping[str, Any] | None = None):
140140
"""
141141
Creates a new instance of Configuration object with the given values.
142142
"""
143-
self._data: Dict[str, Any] = dict(mapping.items()) if mapping else {}
143+
self._data: dict[str, Any] = dict(mapping.items()) if mapping else {}
144144

145145
def __contains__(self, item: str) -> bool:
146146
return item in self._data
@@ -166,7 +166,7 @@ def __repr__(self) -> str:
166166
return f"<Configuration {repr(hidden_values)}>"
167167

168168
@property
169-
def values(self) -> Dict[str, Any]:
169+
def values(self) -> dict[str, Any]:
170170
"""
171171
Returns a copy of the dictionary of current settings.
172172
"""
@@ -190,13 +190,13 @@ def __init__(self, *sources: ConfigurationSource) -> None:
190190
object from different sources. Sources are applied in the given order and can
191191
override each other's settings.
192192
"""
193-
self._sources: List[ConfigurationSource] = list(sources) if sources else []
193+
self._sources: list[ConfigurationSource] = list(sources) if sources else []
194194

195195
def __repr__(self) -> str:
196196
return f"<ConfigurationBuilder {self._sources}>"
197197

198198
@property
199-
def sources(self) -> List[ConfigurationSource]:
199+
def sources(self) -> list[ConfigurationSource]:
200200
return self._sources
201201

202202
def add_source(self, source: ConfigurationSource):

config/common/files.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
from abc import abstractmethod
22
from pathlib import Path
3-
from typing import Any, Dict, Union
3+
from typing import Any
44

55
from config.common import ConfigurationSource
66
from config.errors import MissingConfigurationFileError
77

8-
PathType = Union[Path, str]
8+
PathType = Path | str
99

1010

1111
class FileConfigurationSource(ConfigurationSource):
@@ -15,13 +15,13 @@ def __init__(self, file_path: PathType, optional: bool = False) -> None:
1515
self.optional = optional
1616

1717
@abstractmethod
18-
def read_source(self) -> Dict[str, Any]:
18+
def read_source(self) -> dict[str, Any]:
1919
"""
2020
Reads values from the source file path. This method is not
2121
used if the file does not exist.
2222
"""
2323

24-
def get_values(self) -> Dict[str, Any]:
24+
def get_values(self) -> dict[str, Any]:
2525
if not self.file_path.exists():
2626
if self.optional:
2727
return {}

config/env/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import os
2-
from typing import Any, Dict, Optional
2+
from typing import Any
33

44
from dotenv import load_dotenv
55

@@ -10,16 +10,16 @@
1010
class EnvironmentVariables(ConfigurationSource):
1111
def __init__(
1212
self,
13-
prefix: Optional[str] = None,
13+
prefix: str | None = None,
1414
strip_prefix: bool = True,
15-
file: Optional[PathType] = None,
15+
file: PathType | None = None,
1616
) -> None:
1717
super().__init__()
1818
self.prefix = prefix
1919
self.strip_prefix = strip_prefix
2020
self._file = file
2121

22-
def get_values(self) -> Dict[str, Any]:
22+
def get_values(self) -> dict[str, Any]:
2323
if self._file:
2424
load_dotenv(self._file)
2525

config/ini/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import configparser
22
from collections import abc
3-
from typing import Any, Dict
3+
from typing import Any
44

55
from config.common.files import FileConfigurationSource
66

@@ -20,7 +20,7 @@ def _develop_configparser_values(parser):
2020

2121

2222
class INIFile(FileConfigurationSource):
23-
def read_source(self) -> Dict[str, Any]:
23+
def read_source(self) -> dict[str, Any]:
2424
parser = configparser.ConfigParser()
2525
parser.read(self.file_path, encoding="utf8")
2626
return _develop_configparser_values(parser)

config/json/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import json
2-
from typing import Any, Dict
2+
from typing import Any
33

44
from config.common.files import FileConfigurationSource
55

66

77
class JSONFile(FileConfigurationSource):
8-
def read_source(self) -> Dict[str, Any]:
8+
def read_source(self) -> dict[str, Any]:
99
with open(self.file_path, "rt", encoding="utf-8") as source:
1010
return json.load(source)

config/toml/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Callable, Dict
1+
from typing import Any, Callable
22

33
from config.common.files import FileConfigurationSource, PathType
44

@@ -20,6 +20,6 @@ def __init__(
2020
super().__init__(file_path, optional)
2121
self.parse_float = parse_float
2222

23-
def read_source(self) -> Dict[str, Any]:
23+
def read_source(self) -> dict[str, Any]:
2424
with open(self.file_path, "rb") as source:
2525
return tomllib.load(source, parse_float=self.parse_float)

config/user/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
This module provides support for user settings stored locally, for development, or for
33
CLIs.
44
"""
5+
56
from pathlib import Path
6-
from typing import Any, Dict, Optional
7+
from typing import Any
78
from uuid import uuid4
89

910
from config.common import ConfigurationSource
@@ -37,7 +38,7 @@ class UserSettings(ConfigurationSource):
3738
"""
3839

3940
def __init__(
40-
self, project_name: Optional[str] = None, optional: bool = True
41+
self, project_name: str | None = None, optional: bool = True
4142
) -> None:
4243
"""
4344
Configures an instance of UserSettings that obtains values from a project file
@@ -60,6 +61,6 @@ def get_base_folder(self) -> Path:
6061
def settings_file_path(self) -> Path:
6162
return self._settings_file_path
6263

63-
def get_values(self) -> Dict[str, Any]:
64+
def get_values(self) -> dict[str, Any]:
6465
"""Returns the values read from this source."""
6566
return self._source.get_values()

config/user/cli.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def debug(self, message):
2222
class UserSettingsManager(UserSettings):
2323
def __init__(
2424
self,
25-
project_name: Optional[str] = None,
25+
project_name: str | None = None,
2626
) -> None:
2727
super().__init__(project_name, True)
2828
self.logger = ClickLogger()
@@ -120,7 +120,7 @@ def settings():
120120

121121
@click.command(name="init")
122122
@click.option("--project", "-p", required=False)
123-
def init_settings(project: Optional[str]):
123+
def init_settings(project: str | None):
124124
"""
125125
Initialize user settings for the current folder.
126126
If a project name is specified, it is used, otherwise a value is obtained
@@ -133,7 +133,7 @@ def init_settings(project: Optional[str]):
133133
@click.argument("key")
134134
@click.argument("value")
135135
@click.option("--project", "-p", required=False)
136-
def set_value(key: str, value: str, project: Optional[str]):
136+
def set_value(key: str, value: str, project: str | None):
137137
"""
138138
Set a setting in a user file by key and value.
139139
If a project name is specified, it is used, otherwise a value is obtained
@@ -151,7 +151,7 @@ def set_value(key: str, value: str, project: Optional[str]):
151151
@click.command(name="get")
152152
@click.argument("key")
153153
@click.option("--project", "-p", required=False)
154-
def get_value(key: str, project: Optional[str]):
154+
def get_value(key: str, project: str | None):
155155
"""
156156
Get a setting in a user file by key.
157157
If a project name is specified, it is used, otherwise a value is obtained
@@ -167,7 +167,7 @@ def get_value(key: str, project: Optional[str]):
167167
@click.command(name="set-many")
168168
@click.option("--file", help="Input file", type=click.File("r"), default=sys.stdin)
169169
@click.option("--project", "-p", required=False)
170-
def set_many_values(file, project: Optional[str]):
170+
def set_many_values(file, project: str | None):
171171
"""
172172
Set many settings, read from a JSON file passed through stdin.
173173
If a project name is specified, it is used, otherwise a value is obtained
@@ -188,7 +188,7 @@ def set_many_values(file, project: Optional[str]):
188188
@click.command(name="del")
189189
@click.argument("key")
190190
@click.option("--project", "-p", required=False)
191-
def del_value(key: str, project: Optional[str]):
191+
def del_value(key: str, project: str | None):
192192
"""
193193
Delete a setting for a project, by key.
194194
"""
@@ -197,7 +197,7 @@ def del_value(key: str, project: Optional[str]):
197197

198198
@click.command(name="show")
199199
@click.option("--project", "-p", required=False)
200-
def show_settings(project: Optional[str]):
200+
def show_settings(project: str | None):
201201
"""
202202
Show the local settings for a project.
203203
"""
@@ -214,7 +214,7 @@ def list_groups():
214214

215215
@click.command(name="info")
216216
@click.option("--project", "-p", required=False)
217-
def show_info(project: Optional[str]):
217+
def show_info(project: str | None):
218218
"""
219219
Show information about settings for a project.
220220
"""

config/yaml/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Dict
1+
from typing import Any
22

33
import yaml
44

@@ -12,7 +12,7 @@ def __init__(
1212
super().__init__(file_path, optional)
1313
self.safe_load = safe_load
1414

15-
def read_source(self) -> Dict[str, Any]:
15+
def read_source(self) -> dict[str, Any]:
1616
with open(self.file_path, "rt", encoding="utf-8") as source:
1717
if self.safe_load:
1818
return yaml.safe_load(source)

0 commit comments

Comments
 (0)