Mercurial > public > mercurial-scm > hg
comparison mercurial/interfaces/status.py @ 52451: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 |
comparison
equal
deleted
inserted
replaced
52450:a1c0f19e7cb4 | 52451:f5d134e57f51 |
---|---|
4 # | 4 # |
5 # This software may be used and distributed according to the terms of the | 5 # This software may be used and distributed according to the terms of the |
6 # GNU General Public License version 2 or any later version. | 6 # GNU General Public License version 2 or any later version. |
7 | 7 |
8 from __future__ import annotations | 8 from __future__ import annotations |
9 | |
10 import abc | |
9 | 11 |
10 from typing import ( | 12 from typing import ( |
11 Iterator, | 13 Iterator, |
12 Protocol, | 14 Protocol, |
13 ) | 15 ) |
40 """The list of files in the working directory that are ignored.""" | 42 """The list of files in the working directory that are ignored.""" |
41 | 43 |
42 clean: list[bytes] | 44 clean: list[bytes] |
43 """The list of files that are not in any other state.""" | 45 """The list of files that are not in any other state.""" |
44 | 46 |
47 @abc.abstractmethod | |
45 def __iter__(self) -> Iterator[list[bytes]]: | 48 def __iter__(self) -> Iterator[list[bytes]]: |
46 """Iterates over each of the categories of file lists.""" | 49 """Iterates over each of the categories of file lists.""" |
47 | 50 |
51 @abc.abstractmethod | |
48 def __repr__(self) -> str: | 52 def __repr__(self) -> str: |
49 """Creates a string representation of the file lists.""" | 53 """Creates a string representation of the file lists.""" |