Skip to content

Commit 1e0180e

Browse files
committed
add boolean interpretation to config
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
1 parent 1b65c29 commit 1e0180e

File tree

5 files changed

+71
-5
lines changed

5 files changed

+71
-5
lines changed

alembic/config.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,19 @@ def get_alembic_option(
455455
else:
456456
return self._get_toml_config_value(name, default=default)
457457

458+
def get_alembic_boolean_option(self, name: str) -> bool:
459+
if self.file_config.has_option(self.config_ini_section, name):
460+
return (
461+
self.file_config.get(self.config_ini_section, name) == "true"
462+
)
463+
else:
464+
value = self.toml_alembic_config.get(name, False)
465+
if not isinstance(value, bool):
466+
raise util.CommandError(
467+
f"boolean value expected for TOML parameter {name!r}"
468+
)
469+
return value
470+
458471
def _get_toml_config_value(
459472
self, name: str, default: Optional[Any] = None
460473
) -> Union[None, str, list[str], dict[str, str], list[dict[str, str]]]:
@@ -483,7 +496,9 @@ def _get_toml_config_value(
483496
{k: v % (self.toml_args) for k, v in value.items()},
484497
)
485498
else:
486-
raise util.CommandError("unsupported TOML value type")
499+
raise util.CommandError(
500+
f"unsupported TOML value type for key: {name!r}"
501+
)
487502
return value
488503

489504
@util.memoized_property

alembic/script/base.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,16 +186,14 @@ def from_config(cls, config: Config) -> ScriptDirectory:
186186
if prepend_sys_path:
187187
sys.path[:0] = prepend_sys_path
188188

189-
rvl = (
190-
config.get_alembic_option("recursive_version_locations") == "true"
191-
)
189+
rvl = config.get_alembic_boolean_option("recursive_version_locations")
192190
return ScriptDirectory(
193191
util.coerce_resource_to_filename(script_location),
194192
file_template=config.get_alembic_option(
195193
"file_template", _default_file_template
196194
),
197195
truncate_slug_length=truncate_slug_length,
198-
sourceless=config.get_alembic_option("sourceless") == "true",
196+
sourceless=config.get_alembic_boolean_option("sourceless"),
199197
output_encoding=config.get_alembic_option(
200198
"output_encoding", "utf-8"
201199
),

alembic/testing/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from sqlalchemy.testing.config import combinations
1010
from sqlalchemy.testing.config import fixture
1111
from sqlalchemy.testing.config import requirements as requires
12+
from sqlalchemy.testing.config import Variation
1213
from sqlalchemy.testing.config import variation
1314

1415
from .assertions import assert_raises

docs/build/unreleased/1694.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.. change::
2+
:tags: bug, config
3+
:tickets: 1694
4+
5+
Fixed issue in new ``pyproject.toml`` support where boolean values, such as
6+
those used for the ``recursive_version_locations`` and ``sourceless``
7+
configuration parameters, would not be accepted.
8+

tests/test_config.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,50 @@ def test_string_list(self, pyproject_only_env):
542542
],
543543
)
544544

545+
@testing.combinations(
546+
"sourceless", "recursive_version_locations", argnames="paramname"
547+
)
548+
@testing.variation("argtype", ["true", "false", "omit", "wrongvalue"])
549+
def test_bool(
550+
self, pyproject_only_env, argtype: testing.Variation, paramname
551+
):
552+
553+
cfg = pyproject_only_env
554+
with cfg._toml_file_path.open("w") as file_:
555+
556+
if argtype.true:
557+
config_option = f"{paramname} = true"
558+
elif argtype.false:
559+
config_option = f"{paramname} = false"
560+
elif argtype.omit:
561+
config_option = ""
562+
elif argtype.wrongvalue:
563+
config_option = f"{paramname} = 'false'"
564+
else:
565+
argtype.fail()
566+
567+
file_.write(
568+
rf"""
569+
570+
[tool.alembic]
571+
script_location = "%(here)s/scripts"
572+
573+
{config_option}
574+
"""
575+
)
576+
if "toml_alembic_config" in cfg.__dict__:
577+
cfg.__dict__.pop("toml_alembic_config")
578+
579+
if argtype.wrongvalue:
580+
with expect_raises_message(
581+
util.CommandError,
582+
f"boolean value expected for TOML parameter '{paramname}'",
583+
):
584+
sd = ScriptDirectory.from_config(cfg)
585+
else:
586+
sd = ScriptDirectory.from_config(cfg)
587+
eq_(getattr(sd, paramname), bool(argtype.true))
588+
545589

546590
class StdoutOutputEncodingTest(TestBase):
547591
def test_plain(self):

0 commit comments

Comments
 (0)