Skip to content

Commit b8a25df

Browse files
committed
Modify annotations to accommodate NULL_TREE
This finishes the main changes being done at this time to the annotations of diff-related methods that were started in 0e1df29. As expected and noted in previous commits, having Diffable.diff permit NULL_TREE entails a violation of the Liskov substitution principle in the overridden method IndexFile.diff unless a similar change were also made there, which could not be done correctly without modifying its behavior to actually accept NULL_TREE (it does not contain code to cover it, and raises ane exception if it is passed). I am unsure if that should ultimately be done, but even if so, it seems to me to be beyond the scope of the typing changes being done here. This therefore applies a suppression there. The suppression is specific to that one parameter. The long-standing comment atop the IndexFile.diff method, which reads as vague today, is replaced with a specific FIXME comment describing the situation where the method refers to the base-class docstring for documentation of its parameters yet doesn't accept NULL_TREE (and notes the mypy error). In effect this is really restoring and fixing the suppression that was present before 0e1df29 rather than adding a "new" one, but at that time the base-class parameter type was much broader since it was a union with object as one of its alternatives, so the situation was much less clear.
1 parent c9952e1 commit b8a25df

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

git/diff.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def _process_diff_args(
189189

190190
def diff(
191191
self,
192-
other: Union[Literal[DiffConstants.INDEX], "Tree", "Commit", str, None] = INDEX,
192+
other: Union[DiffConstants, "Tree", "Commit", str, None] = INDEX,
193193
paths: Union[PathLike, List[PathLike], Tuple[PathLike, ...], None] = None,
194194
create_patch: bool = False,
195195
**kwargs: Any,

git/index/base.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1475,10 +1475,13 @@ def reset(
14751475

14761476
return self
14771477

1478-
# @ default_index, breaks typing for some reason, copied into function
1478+
# FIXME: This is documented to accept the same parameters as Diffable.diff, but this
1479+
# does not handle NULL_TREE for `other`. (The suppressed mypy error is about this.)
14791480
def diff(
14801481
self,
1481-
other: Union[Literal[git_diff.DiffConstants.INDEX], "Tree", "Commit", str, None] = git_diff.INDEX,
1482+
other: Union[ # type: ignore[override]
1483+
Literal[git_diff.DiffConstants.INDEX], "Tree", "Commit", str, None
1484+
] = git_diff.INDEX,
14821485
paths: Union[PathLike, List[PathLike], Tuple[PathLike, ...], None] = None,
14831486
create_patch: bool = False,
14841487
**kwargs: Any,

0 commit comments

Comments
 (0)