Update the types for diff chunks and lines.
Review Request #14399 — Created April 14, 2025 and updated
This change defines a
TypedDict
for diff chunks, and aNamedTuple
for diff lines, with definitions and documentation for each element.Before, these were just an opaque dictionary, and a heterogenous list.
The diff lines were particularly annoying because they were variable
length (if there was no metadata, that element was just left off the
end), and types were annoying (for "real" line numbers, we'd have an
int if the line was present on that side of the diff, but was set to
an empty string if it wasn't). This change technically breaks
compatibility in those two cases. We now always include the metadata
field, set toNone
if there's no metadata for the line. In the case of
real line numbers which are not present on one side of the diff (for
example, the line number of an added line for the left-hand side of the
diff), we set the data for that toNone
.The
diff_lines
template tag and other consumers now use names instead
of indexing magically into the structure, making it much clearer what's
going on.This also updates the regions field to always be a list. In some cases
we had empty lists, and in others we hadNone
.This change only touches code related to chunks and lines. Other diff
viewer cleanup is coming in later changes.
- Tested a ton of different diffs, both in the diff viewer as well as
the text and markdown Review UIs. Verified that all types of edits
(added/edited/deleted/moved) lines all worked as expected. - Ran unit tests.
Summary | ID |
---|---|
43f82cea94288ecb0ca769be4b38c080b27fd028 |
Description | From | Last Updated |
---|---|---|
closing bracket does not match indentation of opening bracket's line Column: 17 Error code: E123 |
![]() |
|
'reviewboard.diffviewer.chunk_generator.DiffLine' imported but unused Column: 5 Error code: F401 |
![]() |
|
too many blank lines (2) Column: 5 Error code: E303 |
![]() |
|
undefined name 'none' Column: 42 Error code: F821 |
![]() |
- Commits:
-
Summary ID c07cac59fee7c3f20506e9999d70f54eb99148c6 541e055e3978c23ea3bba8f9ee0b67c26fc4134f - Diff:
-
Revision 2 (+1054 -462)
Checks run (2 succeeded)
- Commits:
-
Summary ID 541e055e3978c23ea3bba8f9ee0b67c26fc4134f b65d93070af3cf1bc40238f661da52ad4eeacd2d - Diff:
-
Revision 3 (+1044 -452)
Checks run (2 succeeded)
- Commits:
-
Summary ID b65d93070af3cf1bc40238f661da52ad4eeacd2d e2861454c701dc0aa04163b7aa4a1982233c2cb2 - Diff:
-
Revision 4 (+1044 -452)
Checks run (2 succeeded)
- Commits:
-
Summary ID e2861454c701dc0aa04163b7aa4a1982233c2cb2 111341255393a8d9dc42826a2f5e7e443ddb1ed0 - Diff:
-
Revision 5 (+1044 -452)
Checks run (2 succeeded)
- Commits:
-
Summary ID 111341255393a8d9dc42826a2f5e7e443ddb1ed0 c53a6fb50e23eaecc39f7239a5998d6553c13cee - Diff:
-
Revision 6 (+1044 -452)
Checks run (2 succeeded)
- Commits:
-
Summary ID c53a6fb50e23eaecc39f7239a5998d6553c13cee 43f82cea94288ecb0ca769be4b38c080b27fd028 - Diff:
-
Revision 7 (+1454 -658)
Checks run (2 succeeded)
- Description:
-
This change defines a
TypedDict
for diff chunks, and aNamedTuple
for diff lines, with definitions and documentation for each element. Before, these were just an opaque dictionary, and a heterogenous list.
The diff lines were particularly annoying because they were variable length (if there was no metadata, that element was just left off the end), and types were annoying (for "real" line numbers, we'd have an int if the line was present on that side of the diff, but was set to an empty string if it wasn't). This change technically breaks compatibility in those two cases. We now always include the metadata field, set to None
if there's no metadata for the line. In the case ofreal line numbers which are not present on one side of the diff (for example, the line number of an added line for the left-hand side of the diff), we set the data for that to None
.The
diff_lines
template tag and other consumers now use names insteadof indexing magically into the structure, making it much clearer what's going on. + This also updates the regions field to always be a list. In some cases
+ we had empty lists, and in others we had None
.+ This change only touches code related to chunks and lines. Other diff
viewer cleanup is coming in later changes.