Skip to content

Commit b4ddfbf

Browse files
committed
refactor: Update type hints to use built-in types and improve imports
1 parent e115881 commit b4ddfbf

4 files changed

Lines changed: 17 additions & 17 deletions

File tree

config2py/base.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,24 @@
44

55
from collections import ChainMap
66
from typing import (
7-
Callable,
87
Type,
98
Tuple,
109
KT,
1110
VT,
1211
Any,
13-
Iterable,
1412
Protocol,
1513
Union,
1614
runtime_checkable,
1715
Optional,
18-
MutableMapping,
1916
)
17+
from collections.abc import Callable, Iterable, MutableMapping
2018
from dataclasses import dataclass
2119
from functools import lru_cache, partial
2220

2321
from config2py.util import always_true, ask_user_for_input, no_default, not_found
2422
from config2py.errors import ConfigNotFound
2523

26-
Exceptions = Tuple[Type[Exception], ...]
24+
Exceptions = tuple[type[Exception], ...]
2725

2826

2927
@runtime_checkable
@@ -104,8 +102,8 @@ def get_config(
104102
sources: Sources = None,
105103
*,
106104
default: VT = no_default,
107-
egress: Optional[GetConfigEgress] = None,
108-
val_is_valid: Optional[Callable[[VT], bool]] = always_true,
105+
egress: GetConfigEgress | None = None,
106+
val_is_valid: Callable[[VT], bool] | None = always_true,
109107
config_not_found_exceptions: Exceptions = (Exception,),
110108
):
111109
"""Get a config value from a list of sources
@@ -374,7 +372,7 @@ def ask_user_for_key(
374372
save_to: SaveTo = None,
375373
save_condition=is_not_empty,
376374
user_asker=ask_user_for_input,
377-
egress: Optional[Callable] = None,
375+
egress: Callable | None = None,
378376
):
379377
if key is None:
380378
return partial(
@@ -399,7 +397,7 @@ def user_gettable(
399397
save_to: SaveTo = None,
400398
*,
401399
prompt_template="Enter a value for {}: ",
402-
egress: Optional[Callable] = None,
400+
egress: Callable | None = None,
403401
user_asker=ask_user_for_input,
404402
val_is_valid: Callable[[VT], bool] = is_not_empty,
405403
config_not_found_exceptions: Exceptions = (Exception,),

config2py/s_configparser.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -299,11 +299,11 @@ def __exit__(self, *exc_details):
299299

300300
@persist_after_operation
301301
def __setitem__(self, k, v):
302-
super(ConfigStore, self).__setitem__(k, v)
302+
super().__setitem__(k, v)
303303

304304
@persist_after_operation
305305
def __delitem__(self, k):
306-
super(ConfigStore, self).__delitem__(k)
306+
super().__delitem__(k)
307307

308308
# __setitem__ = super_and_persist(ConfigParser, '__setitem__')
309309
# __delitem__ = super_and_persist(ConfigParser, '__delitem__')
@@ -388,13 +388,14 @@ def __delitem__(self, k):
388388
# return super()._obj_of_data(data)
389389

390390

391-
from typing import Mapping, Iterable, Generator, Union
391+
from typing import Union
392+
from collections.abc import Mapping, Iterable, Generator
392393
import re
393394

394395

395396
# TODO: postprocess_ini_section_items and preprocess_ini_section_items: Add comma separated possibility?
396397
# TODO: Find out if configparse has an option to do this processing alreadys
397-
def postprocess_ini_section_items(items: Union[Mapping, Iterable]) -> Generator:
398+
def postprocess_ini_section_items(items: Mapping | Iterable) -> Generator:
398399
r"""Transform newline-separated string values into actual list of strings (assuming that intent)
399400
400401
>>> section_from_ini = {
@@ -417,7 +418,7 @@ def postprocess_ini_section_items(items: Union[Mapping, Iterable]) -> Generator:
417418

418419

419420
# TODO: Find out if configparse has an option to do this processing alreadys
420-
def preprocess_ini_section_items(items: Union[Mapping, Iterable]) -> Generator:
421+
def preprocess_ini_section_items(items: Mapping | Iterable) -> Generator:
421422
"""Transform list values into newline-separated strings, in view of writing the value to a ini formatted section
422423
>>> section = {
423424
... 'name': 'aspyre',

config2py/tools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Various tools"""
22

3-
from typing import Callable
3+
from collections.abc import Callable
44
from pathlib import Path
55
import re
66
from dol import Pipe, TextFiles, resolve_path

config2py/util.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
from collections import ChainMap, namedtuple
1010
from pathlib import Path
1111
from functools import partial
12-
from typing import Optional, Union, Any, Callable, Set, Literal, get_args
12+
from typing import Optional, Union, Any, Set, Literal, get_args
13+
from collections.abc import Callable
1314
from types import SimpleNamespace
1415
import getpass
1516

@@ -119,7 +120,7 @@ def ask_user_for_input(
119120

120121
# Note: Could be made more efficient, but this is good enough (for now)
121122
def extract_variable_declarations(
122-
string: str, expand: Optional[Union[dict, bool]] = None
123+
string: str, expand: dict | bool | None = None
123124
) -> dict:
124125
"""
125126
Reads the contents of a config file, extracting Unix-style environment variable
@@ -554,7 +555,7 @@ def is_repl():
554555
return False
555556

556557

557-
is_repl.repl_conditions: Set[Callable] = _repl_conditions # type: ignore
558+
is_repl.repl_conditions: set[Callable] = _repl_conditions # type: ignore
558559

559560

560561
def _value_node_is_instance_of(

0 commit comments

Comments
 (0)