]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
add boolean interpretation to config
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 9 Jul 2025 18:57:11 +0000 (14:57 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 9 Jul 2025 18:57:58 +0000 (14:57 -0400)
Fixed issue in new ``pyproject.toml`` support where boolean values, such as
those used for the ``recursive_version_locations`` and ``sourceless``
configuration parameters, would not be accepted.

Fixes: #1694
Change-Id: Ifeed46c599ab20db1713f306b7be9c3b5fe23485

alembic/config.py
alembic/script/base.py
alembic/testing/__init__.py
docs/build/unreleased/1694.rst [new file with mode: 0644]
tests/test_config.py

index c1196657c10942e15c8066377276ccf26fad99ca..dcc64a4cfd9f0543d55a13b985ca16e1b1b132ba 100644 (file)
@@ -455,6 +455,19 @@ class Config:
         else:
             return self._get_toml_config_value(name, default=default)
 
+    def get_alembic_boolean_option(self, name: str) -> bool:
+        if self.file_config.has_option(self.config_ini_section, name):
+            return (
+                self.file_config.get(self.config_ini_section, name) == "true"
+            )
+        else:
+            value = self.toml_alembic_config.get(name, False)
+            if not isinstance(value, bool):
+                raise util.CommandError(
+                    f"boolean value expected for TOML parameter {name!r}"
+                )
+            return value
+
     def _get_toml_config_value(
         self, name: str, default: Optional[Any] = None
     ) -> Union[None, str, list[str], dict[str, str], list[dict[str, str]]]:
@@ -483,7 +496,9 @@ class Config:
                     {k: v % (self.toml_args) for k, v in value.items()},
                 )
             else:
-                raise util.CommandError("unsupported TOML value type")
+                raise util.CommandError(
+                    f"unsupported TOML value type for key: {name!r}"
+                )
         return value
 
     @util.memoized_property
index c3b1c3115443da5941557b11d66336922b2c63b3..94292316bc8e9fabc9160487239dcb93d84dbe98 100644 (file)
@@ -186,16 +186,14 @@ class ScriptDirectory:
         if prepend_sys_path:
             sys.path[:0] = prepend_sys_path
 
-        rvl = (
-            config.get_alembic_option("recursive_version_locations") == "true"
-        )
+        rvl = config.get_alembic_boolean_option("recursive_version_locations")
         return ScriptDirectory(
             util.coerce_resource_to_filename(script_location),
             file_template=config.get_alembic_option(
                 "file_template", _default_file_template
             ),
             truncate_slug_length=truncate_slug_length,
-            sourceless=config.get_alembic_option("sourceless") == "true",
+            sourceless=config.get_alembic_boolean_option("sourceless"),
             output_encoding=config.get_alembic_option(
                 "output_encoding", "utf-8"
             ),
index 316ea91b98a8545a504e2a41ca93aec52736645a..32915081d9530a6c1239778a84f34479d21026b2 100644 (file)
@@ -9,6 +9,7 @@ from sqlalchemy.testing import uses_deprecated
 from sqlalchemy.testing.config import combinations
 from sqlalchemy.testing.config import fixture
 from sqlalchemy.testing.config import requirements as requires
+from sqlalchemy.testing.config import Variation
 from sqlalchemy.testing.config import variation
 
 from .assertions import assert_raises
diff --git a/docs/build/unreleased/1694.rst b/docs/build/unreleased/1694.rst
new file mode 100644 (file)
index 0000000..b7f5a3c
--- /dev/null
@@ -0,0 +1,8 @@
+.. change::
+    :tags: bug, config
+    :tickets: 1694
+
+    Fixed issue in new ``pyproject.toml`` support where boolean values, such as
+    those used for the ``recursive_version_locations`` and ``sourceless``
+    configuration parameters, would not be accepted.
+
index 4827b6354291efd16d972bea6a0192c06772e7ec..40a22f7179334c904c5f6e992b1f9f717f52b967 100644 (file)
@@ -542,6 +542,50 @@ my_list = [
             ],
         )
 
+    @testing.combinations(
+        "sourceless", "recursive_version_locations", argnames="paramname"
+    )
+    @testing.variation("argtype", ["true", "false", "omit", "wrongvalue"])
+    def test_bool(
+        self, pyproject_only_env, argtype: testing.Variation, paramname
+    ):
+
+        cfg = pyproject_only_env
+        with cfg._toml_file_path.open("w") as file_:
+
+            if argtype.true:
+                config_option = f"{paramname} = true"
+            elif argtype.false:
+                config_option = f"{paramname} = false"
+            elif argtype.omit:
+                config_option = ""
+            elif argtype.wrongvalue:
+                config_option = f"{paramname} = 'false'"
+            else:
+                argtype.fail()
+
+            file_.write(
+                rf"""
+
+[tool.alembic]
+script_location = "%(here)s/scripts"
+
+{config_option}
+"""
+            )
+        if "toml_alembic_config" in cfg.__dict__:
+            cfg.__dict__.pop("toml_alembic_config")
+
+        if argtype.wrongvalue:
+            with expect_raises_message(
+                util.CommandError,
+                f"boolean value expected for TOML parameter '{paramname}'",
+            ):
+                sd = ScriptDirectory.from_config(cfg)
+        else:
+            sd = ScriptDirectory.from_config(cfg)
+            eq_(getattr(sd, paramname), bool(argtype.true))
+
 
 class StdoutOutputEncodingTest(TestBase):
     def test_plain(self):