Skip to content

Commit d45b7d1

Browse files
LuisHenrizzzeek
authored andcommitted
fix(config): Accept int on toml config
Fixed issue where new pyproject.toml config would fail to parse the integer value used for the ``truncate_slug_length`` parameter. Pull request courtesy Luís Henrique Allebrandt Schunemann. Fixes: #1709 Closes: #1710 Pull-request: #1710 Pull-request-sha: 87652ee Change-Id: Ide0f00437740c94250815a5340b2d742e960d1ca
1 parent 6c12707 commit d45b7d1

4 files changed

Lines changed: 64 additions & 4 deletions

File tree

alembic/config.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,9 @@ def get_alembic_option(
426426

427427
def get_alembic_option(
428428
self, name: str, default: Optional[str] = None
429-
) -> Union[None, str, list[str], dict[str, str], list[dict[str, str]]]:
429+
) -> Union[
430+
None, str, list[str], dict[str, str], list[dict[str, str]], int
431+
]:
430432
"""Return an option from the "[alembic]" or "[tool.alembic]" section
431433
of the configparser-parsed .ini file (e.g. ``alembic.ini``) or
432434
toml-parsed ``pyproject.toml`` file.
@@ -470,9 +472,11 @@ def get_alembic_boolean_option(self, name: str) -> bool:
470472

471473
def _get_toml_config_value(
472474
self, name: str, default: Optional[Any] = None
473-
) -> Union[None, str, list[str], dict[str, str], list[dict[str, str]]]:
475+
) -> Union[
476+
None, str, list[str], dict[str, str], list[dict[str, str]], int
477+
]:
474478
USE_DEFAULT = object()
475-
value: Union[None, str, list[str], dict[str, str]] = (
479+
value: Union[None, str, list[str], dict[str, str], int] = (
476480
self.toml_alembic_config.get(name, USE_DEFAULT)
477481
)
478482
if value is USE_DEFAULT:
@@ -495,6 +499,8 @@ def _get_toml_config_value(
495499
"dict[str, str]",
496500
{k: v % (self.toml_args) for k, v in value.items()},
497501
)
502+
elif isinstance(value, int):
503+
return value
498504
else:
499505
raise util.CommandError(
500506
f"unsupported TOML value type for key: {name!r}"

alembic/ddl/mysql.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ def _mysql_drop_constraint(
507507
# note that SQLAlchemy as of 1.2 does not yet support
508508
# DROP CONSTRAINT for MySQL/MariaDB, so we implement fully
509509
# here.
510-
if compiler.dialect.is_mariadb: # type: ignore[attr-defined]
510+
if compiler.dialect.is_mariadb:
511511
return "ALTER TABLE %s DROP CONSTRAINT %s" % (
512512
compiler.preparer.format_table(constraint.table),
513513
compiler.preparer.format_constraint(constraint),

docs/build/unreleased/1709.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.. change::
2+
:tags: bug, config
3+
:tickets: 1709
4+
5+
Fixed issue where new pyproject.toml config would fail to parse the integer
6+
value used for the ``truncate_slug_length`` parameter. Pull request
7+
courtesy Luís Henrique Allebrandt Schunemann.

tests/test_config.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,53 @@ def test_bool(
586586
sd = ScriptDirectory.from_config(cfg)
587587
eq_(getattr(sd, paramname), bool(argtype.true))
588588

589+
@testing.variation(
590+
"arg_type", ["int", "string_int", "omit", "wrong_value"]
591+
)
592+
def test_truncate_slug_length_types(
593+
self, pyproject_only_env, arg_type: testing.Variation
594+
):
595+
param_name = "truncate_slug_length"
596+
cfg = pyproject_only_env
597+
with cfg._toml_file_path.open("w") as file_:
598+
if arg_type.int:
599+
config_option = f"{param_name} = 42"
600+
elif arg_type.string_int:
601+
config_option = f"{param_name} = '42'"
602+
elif arg_type.omit:
603+
config_option = ""
604+
elif arg_type.wrong_value:
605+
config_option = f"{param_name} = 'wrong_value'"
606+
else:
607+
arg_type.fail()
608+
609+
file_.write(
610+
rf"""
611+
[tool.alembic]
612+
script_location = "%(here)s/scripts"
613+
614+
{config_option}
615+
"""
616+
)
617+
if "toml_alembic_config" in cfg.__dict__:
618+
cfg.__dict__.pop("toml_alembic_config")
619+
620+
if arg_type.wrong_value:
621+
with expect_raises_message(
622+
ValueError,
623+
"invalid literal for int() with base 10: 'wrong_value'",
624+
text_exact=True,
625+
):
626+
sd = ScriptDirectory.from_config(cfg)
627+
elif arg_type.omit:
628+
sd = ScriptDirectory.from_config(cfg)
629+
630+
DEFAULT_TRUNCATE_SLUG_LENGTH = 40
631+
eq_(getattr(sd, param_name), DEFAULT_TRUNCATE_SLUG_LENGTH)
632+
else:
633+
sd = ScriptDirectory.from_config(cfg)
634+
eq_(getattr(sd, param_name), 42)
635+
589636

590637
class StdoutOutputEncodingTest(TestBase):
591638
def test_plain(self):

0 commit comments

Comments
 (0)