##// END OF EJS Templates
scmutil: explicitly subclass the `Status` protocol...
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.)

File last commit:

r52857:d7f17819 default
r53348:f5d134e5 default
Show More
modules.py
85 lines | 2.4 KiB | text/x-python | PythonLexer
# modules.py - protocol classes for dynamically loaded modules
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
from __future__ import annotations
import typing
from typing import (
Callable,
List,
Optional,
Protocol,
Tuple,
)
if typing.TYPE_CHECKING:
BDiffBlock = Tuple[int, int, int, int]
"""An entry in the list returned by bdiff.{xdiff,}blocks()."""
BDiffBlocksFnc = Callable[[bytes, bytes], List[BDiffBlock]]
"""The signature of `bdiff.blocks()` and `bdiff.xdiffblocks()`."""
class Base85(Protocol):
"""A Protocol class for the various base85 module implementations."""
def b85encode(self, text: bytes, pad: bool = False) -> bytes:
"""encode text in base85 format"""
def b85decode(self, text: bytes) -> bytes:
"""decode base85-encoded text"""
class BDiff(Protocol):
"""A Protocol class for the various bdiff module implementations."""
def splitnewlines(self, text: bytes) -> List[bytes]:
"""like str.splitlines, but only split on newlines."""
def bdiff(self, a: bytes, b: bytes) -> bytes:
...
def blocks(self, a: bytes, b: bytes) -> List[BDiffBlock]:
...
def fixws(self, text: bytes, allws: bool) -> bytes:
...
xdiffblocks: Optional[BDiffBlocksFnc]
"""This method is currently only available in the ``cext`` module."""
class CharEncoding(Protocol):
"""A Protocol class for the various charencoding module implementations."""
def isasciistr(self, s: bytes) -> bool:
"""Can the byte string be decoded with the ``ascii`` codec?"""
def asciilower(self, s: bytes) -> bytes:
"""convert a string to lowercase if ASCII
Raises UnicodeDecodeError if non-ASCII characters are found."""
def asciiupper(self, s: bytes) -> bytes:
"""convert a string to uppercase if ASCII
Raises UnicodeDecodeError if non-ASCII characters are found."""
def jsonescapeu8fast(self, u8chars: bytes, paranoid: bool) -> bytes:
"""Convert a UTF-8 byte string to JSON-escaped form (fast path)
Raises ValueError if non-ASCII characters have to be escaped.
"""
class MPatch(Protocol):
"""A protocol class for the various mpatch module implementations."""
def patches(self, a: bytes, bins: List[bytes]) -> bytes:
...
def patchedsize(self, orig: int, delta: bytes) -> int:
...