]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
restore Dict[str, str] as potential get_section() type
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 16 May 2023 14:39:31 +0000 (10:39 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 16 May 2023 14:40:09 +0000 (10:40 -0400)
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
alembic/config.py
docs/build/unreleased/1244.rst [new file with mode: 0644]

index 2968f0c0b740376523983519b2f2584b14f83f22..1577ce9facb0b0df28bc40e58d1c54e76cb4f146 100644 (file)
@@ -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 (file)
index 0000000..a32ffa0
--- /dev/null
@@ -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.