changeset 52480:f5d134e57f51

scmutil: explicitly subclass the `Status` protocol We shouldn't have to explicitly subclass, but PyCharm has a nifty feature that puts a jump point in the gutter to navigate back and forth between the base class and subclasses (and override functions and base class functions) when there's an explicit subclassing. Additionally, PyCharm will immediately flag signature mismatches without a 40m pytype run. It was also hoped that with explicit subclassing, we would get interface checking for free. Unfortunately when I tried adding methods and fields to the Protocol class to test this theory, pytype happily accepted an assignment of the concrete class without the new field and methods, to a variable annotated with the Protocol class with them. It appears that this is what happens when explicit subclassing is used, since dropping that caused pytype to complain. By making the methods abstract here like the `mercurial.wireprototypes` classes in fd200f5bcaea, pytype will complain in that case outlined that a subclass with abstract methods (not replaced by the subclass itself) cannot be instantiated. That doesn't help with the fields. Making an `abstractproperty` likely isn't appropriate in general, because that effectively becomes a read-only property. This seems like a pretty gaping hole, but I think the benefits of explicit subclassing are worth the risk. (Though I guess it shouldn't be surprising, because a class can be both a Protocol and an implementation, so subclassing something with an empty body method doesn't really signal that it is a requirement for the subclass to implement.)
author Matt Harbison <matt_harbison@yahoo.com>
date Mon, 09 Dec 2024 00:01:03 -0500
parents a1c0f19e7cb4
children 9d79ffeed7c0
files mercurial/interfaces/status.py mercurial/scmutil.py
diffstat 2 files changed, 7 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/interfaces/status.py	Sun Dec 08 23:52:12 2024 -0500
+++ b/mercurial/interfaces/status.py	Mon Dec 09 00:01:03 2024 -0500
@@ -7,6 +7,8 @@
 
 from __future__ import annotations
 
+import abc
+
 from typing import (
     Iterator,
     Protocol,
@@ -42,8 +44,10 @@
     clean: list[bytes]
     """The list of files that are not in any other state."""
 
+    @abc.abstractmethod
     def __iter__(self) -> Iterator[list[bytes]]:
         """Iterates over each of the categories of file lists."""
 
+    @abc.abstractmethod
     def __repr__(self) -> str:
         """Creates a string representation of the file lists."""
--- a/mercurial/scmutil.py	Sun Dec 08 23:52:12 2024 -0500
+++ b/mercurial/scmutil.py	Mon Dec 09 00:01:03 2024 -0500
@@ -64,6 +64,8 @@
     vfs,
 )
 
+from .interfaces import status as istatus
+
 from .utils import (
     hashutil,
     procutil,
@@ -87,7 +89,7 @@
 
 
 @attr.s(slots=True, repr=False)
-class status:
+class status(istatus.Status):
     """Struct with a list of files per status.
 
     The 'deleted', 'unknown' and 'ignored' properties are only