Mercurial > public > mercurial-scm > hg
view mercurial/interfaces/modules.py @ 52506:199b0e62b403
interfaces: make the `peer` mixin not a Protocol to fix Python 3.10 failures
I can't find any documentation on this, but it appears that Protocol class
attributes don't get inherited in subclasses that explicitly subclass a Protocol
until Python 3.11, which caused a ton of failures in CI on macOS and Windows
(which both test using Python 3.9). The problem started with 1df97507c6b8, and
typically manifested as most tests failing to access `ui` on various `peer`
classes.
Here's a short proof of concept:
from __future__ import annotations
from typing import (
Protocol,
)
class peer(Protocol):
limitedarguments: bool = False
def __init__(self, arg1, arg2, remotehidden: bool = False) -> None:
self.arg1 = arg1
self.arg2 = arg2
class subclass(peer):
def __init__(self, arg1, arg2):
super(subclass, self).__init__(arg1, arg2, False)
sub = subclass(1, 2)
print("sub.arg1 is %r" % sub.arg1)
When run with Python 3.8.10, 3.9.13, and 3.10.11, the result is:
$ py -3.8 prot-test.py
Traceback (most recent call last):
File "prot-test.py", line 20, in <module>
print("sub.arg1 is %r" % sub.arg1)
AttributeError: 'subclass' object has no attribute 'arg1'
On Python 3.11.9, 3.12.7, and 3.13.0, the result is:
$ py -3.11 ../prot-test.py
sub.arg1 is 1
Explicitly adding annotations to `peer` like `limitedarguments` didn't help.
author | Matt Harbison <matt_harbison@yahoo.com> |
---|---|
date | Sun, 15 Dec 2024 18:52:05 -0500 |
parents | d7f17819ae9e |
children |
line wrap: on
line source
# 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: ...