Config: Modernize types to PEP 604 union syntax
Replace Optional[X] with X | None and typing.Dict with builtin dict throughout the module. Add "from __future__ import annotations" to enable deferred evaluation, removing all quoted forward references. Add missing return type annotations to __setitem__(), parent, root, set(), get(), value(), and name. Remove Optional and Dict from the typing import since cast() is the only runtime typing helper needed. Assisted-by: unsloth/Qwen3.6-35B-A3B-GGUF:IQ4_NL with pi.dev v Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
9ed8c1cb18
commit
ffbb070076
1 changed files with 18 additions and 16 deletions
|
|
@ -1,10 +1,12 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import glob
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
|
||||
from pathlib import Path
|
||||
from typing import Dict, Optional, cast
|
||||
from typing import cast
|
||||
|
||||
from .stree import serdes
|
||||
from .log import DEBUG, ERR, slog, get_caller_pos
|
||||
|
|
@ -76,13 +78,13 @@ class Config(): # export
|
|||
|
||||
def __init__(
|
||||
self,
|
||||
search_dirs: Optional[list[str]] = None,
|
||||
glob_paths: Optional[list[str]] = None,
|
||||
glob_paths_env_key: Optional[str] = None,
|
||||
defaults: Optional[Dict[str, str]] = None,
|
||||
tree: Optional[StringTree] = None,
|
||||
parent = None,
|
||||
root_section = None,
|
||||
search_dirs: list[str] | None = None,
|
||||
glob_paths: list[str] | None = None,
|
||||
glob_paths_env_key: str | None = None,
|
||||
defaults: dict[str, str] | None = None,
|
||||
tree: StringTree | None = None,
|
||||
parent: Config | None = None,
|
||||
root_section: str | None = None,
|
||||
refuse_mode_mask: int = _REFUSE_MODE_MASK
|
||||
) -> None:
|
||||
|
||||
|
|
@ -127,23 +129,23 @@ class Config(): # export
|
|||
raise KeyError(key)
|
||||
return ret
|
||||
|
||||
def __setitem__(self, key: str, value: str):
|
||||
return self.set(key, value)
|
||||
def __setitem__(self, key: str, value: str) -> None:
|
||||
self.set(key, value)
|
||||
|
||||
@property
|
||||
def parent(self):
|
||||
def parent(self) -> Config | None:
|
||||
return self.__parent
|
||||
|
||||
@property
|
||||
def root(self):
|
||||
def root(self) -> Config:
|
||||
if self.__parent is None:
|
||||
return self
|
||||
return self.__parent.root
|
||||
|
||||
def set(self, key: str, val):
|
||||
def set(self, key: str, val: str) -> None:
|
||||
self.__conf[key] = val
|
||||
|
||||
def get(self, key: str, default = None) -> Optional[str]:
|
||||
def get(self, key: str, default: str | None = None) -> str | None:
|
||||
item = self.__conf.get(key)
|
||||
if item:
|
||||
return item.value()
|
||||
|
|
@ -156,7 +158,7 @@ class Config(): # export
|
|||
return [name for name, child in item.children.items()]
|
||||
|
||||
# This is an alias for get()
|
||||
def value(self, key: str, default = None) -> Optional[str]:
|
||||
def value(self, key: str, default: str | None = None) -> str | None:
|
||||
return self.get(key, default)
|
||||
|
||||
def branch(self, path: str, throw: bool = True) -> Config | None:
|
||||
|
|
@ -177,7 +179,7 @@ class Config(): # export
|
|||
self.__conf.dump(prio, caller = caller, *args, **kwargs)
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
def name(self) -> str | None:
|
||||
return self.__conf.content
|
||||
|
||||
def find(
|
||||
|
|
|
|||
Loading…
Reference in a new issue