Skip to content

describe() reads an enum whose values contain a colon correctly - #1730

Merged
dimitri-yatsenko merged 4 commits into
masterfrom
fix/1724-enum-colon-describe
Dec 1, 2026
Merged

dimitri-yatsenko merged 4 commits into
masterfrom
fix/1724-enum-colon-describe

Conversation

@dimitri-yatsenko

@dimitri-yatsenko dimitri-yatsenko commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

Summary

DataJoint records an attribute's declared type at the start of the column comment as :type:comment. For an enum whose values contain a colon, such as enum('10:00','11:00'), the type was read only up to the first colon. So original_type became enum('10, the comment began with 00','11:00'), and describe() printed a definition that could not be declared again.

The type now ends at the first colon outside a quoted string. The enum is read whole, the comment is read correctly, and describe() round-trips on both backends. alter() reads the type marker in front of a {old_name} rename marker the same way, so it also finds a rename on such an attribute.

Fixes #1724

Merge order

  • This PR is stacked on Read declared types from column comments only when they are known types #1721, which adds split_type_comment. Merge 1721 first. Until then, this PR's diff also shows the 1721 commits; this PR's own changes are the last two commits.
  • alter() changes and renames attributes on PostgreSQL #1639 rewrites _make_attribute_alter and removes original_regexp. It reads the rename marker from the comment before the :type: marker is added, so the parser change in declare.py here is not needed once 1639 is in. Whichever of 1639 and this PR merges second must reconcile the two: keep 1639's _make_attribute_alter, keep test_rename_enum_with_colon_keeps_values, and change that test's PostgreSQL branch to expect the rename to keep the values, as 1639 does for test_rename_keeps_values.

Changes

  • heading.py: the pattern that reads the type from a column comment treats single- and double-quoted strings as part of the type, so a colon inside quotes does not end it. The type still cannot span lines, inside quotes too. A marker that is not a known core or codec type is still read as a plain comment, as in 1721.
  • declare.py: _make_attribute_alter splits the :type: marker off the declared comment with split_type_comment before it looks for the {old_name} rename marker. It used to read the marker up to the first colon, so after enum('10:00','11:00') it missed the rename marker.

Behavior change

  • describe() on a table with an enum whose values contain a colon prints a definition that declares the same table. Before, the enum was cut at the first colon.
  • alter() on such a table used to stop with Unsupported attribute type enum('10 ..., because the definition read back from the database did not parse, and made no change. It now runs. A rename of such an attribute is a CHANGE on MySQL, which keeps the values. On PostgreSQL, alter() refuses the rename and makes no change, as it does for any other attribute.
  • Without the declare.py change, the heading fix alone would have let alter() miss the rename marker on such an attribute and drop the attribute and add it again empty. Both changes ship together in this PR.

Tests

  • tests/unit/test_type_comment.py: :enum('10:00','11:00'):start of slot gives the type enum('10:00','11:00') and the comment start of slot. Other cases: a colon in a value followed by a comment with colons, a doubled quote inside a value, and an apostrophe in the comment after a plain type. A quoted value with a line break, an unclosed quote, and a quoted type name all stay plain comments.
  • tests/integration/test_type_comment.py, on MySQL and PostgreSQL: a table with two such enums reads back the right original_type and comment, a table declared from describe() matches the original declaration, and values insert and fetch unchanged.
  • tests/integration/test_alter_rename.py::test_rename_enum_with_colon_keeps_values, on MySQL and PostgreSQL, for a required and a nullable attribute: an enum('10:00','11:00') attribute holding two rows is renamed with {start}. On MySQL the values and the declared type are kept under the new name. On PostgreSQL alter() raises the rename error and the values stay in place.
  • Without the declare.py change, all four cases of the new alter() test fail: on MySQL every row ends up '10:00' (required) or NULL (nullable), and on PostgreSQL the nullable attribute is dropped and added again empty with no error. On master they fail with the Unsupported attribute type error.
  • The new tests pass with this change. The full test suite passes locally on both backends.

The declared type at the start of a column comment ended at the first
colon, so enum('10:00','11:00') was cut inside its first value. The type
now ends at the first colon outside a quoted string, and still cannot
span lines.

Fixes #1724
alter() read the :type: marker of a declared comment up to the first
colon, so with enum('10:00','11:00') it ended inside a value and the
{old_name} rename marker after it went unnoticed. The attribute was then
dropped and added again empty. The marker is now split off with
split_type_comment, the same quote-aware reader the heading uses.
MilagrosMarin
MilagrosMarin previously approved these changes Nov 28, 2026

@MilagrosMarin MilagrosMarin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @dimitri-yatsenko, this is a clean fix with tests that actually guard it.

  • Ran the new tests on MySQL and PostgreSQL after merging current master: 16 integration and 40 unit tests pass. The full suite passes too (2105 passed, 17 skipped).
  • Put back master's heading.py and declare.py: 10 of the 16 integration tests fail. With only the declare.py change removed, all 4 test_rename_enum_with_colon_keeps_values cases fail, as the description says.
  • Checked the parser against a doubled quote ('a''b:c'), a double-quoted value, an empty comment after the type, a missing closing colon, and {start} after the enum type. All are read correctly.
  • Merge order: this conflicts with #1639 in declare.py and test_alter_rename.py whichever merges second. I tried #1721, then #1639, then this PR. Keeping #1639's _make_attribute_alter and making test_rename_enum_with_colon_keeps_values expect the values to be kept on both backends was enough, and the full suite passed (2213 passed, 18 skipped).
  • Minor: a value with a backslash-escaped quote and a colon, such as 'it\'s:x', is still read as a plain comment. That predates this PR and may not be worth handling.
  • Nit: the docstring of test_rename_enum_with_colon_keeps_values tells the story of the bug. One line naming the rule under test would be enough.

With #1721 in first, this makes describe() and alter() safe for time-of-day enums like '10:00'.

@dimitri-yatsenko
dimitri-yatsenko merged commit 5d2b833 into master Dec 1, 2026
25 checks passed
@dimitri-yatsenko
dimitri-yatsenko deleted the fix/1724-enum-colon-describe branch December 1, 2026 18:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

describe() misreads an enum type whose values contain a colon

2 participants