summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2023-05-16 10:39:31 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2023-05-16 10:40:09 -0400
commit95adff69ec5b821235489e4fafdab358a18f323f (patch)
tree609796d3605cecb00e197a907a696a6d1219984e
parentf983e1d1442070bfc35ce1c19379221c1baf54cf (diff)
downloadalembic-95adff69ec5b821235489e4fafdab358a18f323f.tar.gz
restore Dict[str, str] as potential get_section() type
Restored the output type of :meth:`.Config.get_section` to include ``Dict[str, str]`` as a potential return type, which had been changed to immutable ``Mapping[str, str]``. When a section is returned and the default is not used, a mutable dictionary is returned. Change-Id: I6e2c67f00222d7a2b388f9294c5336fd8e2dec94 Fixes: #1244
-rw-r--r--alembic/config.py25
-rw-r--r--docs/build/unreleased/1244.rst8
2 files changed, 28 insertions, 5 deletions
diff --git a/alembic/config.py b/alembic/config.py
index 2968f0c..1577ce9 100644
--- a/alembic/config.py
+++ b/alembic/config.py
@@ -8,6 +8,7 @@ import os
import sys
from typing import Any
from typing import cast
+from typing import Dict
from typing import Mapping
from typing import Optional
from typing import overload
@@ -219,20 +220,34 @@ class Config:
@overload
def get_section(
- self, name: str, default: Mapping[str, str]
- ) -> Mapping[str, str]:
+ self, name: str, default: None = ...
+ ) -> Optional[Dict[str, str]]:
...
+ # "default" here could also be a TypeVar
+ # _MT = TypeVar("_MT", bound=Mapping[str, str]),
+ # however mypy wasn't handling that correctly (pyright was)
@overload
def get_section(
- self, name: str, default: Optional[Mapping[str, str]] = ...
- ) -> Optional[Mapping[str, str]]:
+ self, name: str, default: Dict[str, str]
+ ) -> Dict[str, str]:
+ ...
+
+ @overload
+ def get_section(
+ self, name: str, default: Mapping[str, str]
+ ) -> Union[Dict[str, str], Mapping[str, str]]:
...
- def get_section(self, name: str, default=None):
+ def get_section(
+ self, name: str, default: Optional[Mapping[str, str]] = None
+ ) -> Optional[Mapping[str, str]]:
"""Return all the configuration options from a given .ini file section
as a dictionary.
+ If the given section does not exist, the value of ``default``
+ is returned, which is expected to be a dictionary or other mapping.
+
"""
if not self.file_config.has_section(name):
return default
diff --git a/docs/build/unreleased/1244.rst b/docs/build/unreleased/1244.rst
new file mode 100644
index 0000000..a32ffa0
--- /dev/null
+++ b/docs/build/unreleased/1244.rst
@@ -0,0 +1,8 @@
+.. change::
+ :tags: bug, typing, regression
+ :tickets: 1244
+
+ Restored the output type of :meth:`.Config.get_section` to include
+ ``Dict[str, str]`` as a potential return type, which had been changed to
+ immutable ``Mapping[str, str]``. When a section is returned and the default
+ is not used, a mutable dictionary is returned.