annotate mercurial/interfaces/status.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 f5d134e57f51
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
52450
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
1 # status.py - Type annotations for status related objects
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
2 #
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
3 # Copyright Matt Harbison <mharbison72@gmail.com>
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
4 #
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
5 # This software may be used and distributed according to the terms of the
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
6 # GNU General Public License version 2 or any later version.
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
7
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
8 from __future__ import annotations
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
9
52451
f5d134e57f51 scmutil: explicitly subclass the `Status` protocol
Matt Harbison <matt_harbison@yahoo.com>
parents: 52450
diff changeset
10 import abc
f5d134e57f51 scmutil: explicitly subclass the `Status` protocol
Matt Harbison <matt_harbison@yahoo.com>
parents: 52450
diff changeset
11
52450
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
12 from typing import (
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
13 Iterator,
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
14 Protocol,
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
15 )
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
16
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
17
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
18 class Status(Protocol):
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
19 """Struct with a list of files per status.
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
20
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
21 The 'deleted', 'unknown' and 'ignored' properties are only
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
22 relevant to the working copy.
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
23 """
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
24
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
25 modified: list[bytes]
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
26 """The list of files with modifications."""
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
27
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
28 added: list[bytes]
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
29 """The list of files that started being tracked."""
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
30
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
31 removed: list[bytes]
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
32 """The list of files that stopped being tracked."""
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
33
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
34 deleted: list[bytes]
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
35 """The list of files in the working directory that are deleted from the
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
36 file system (but not in the removed state)."""
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
37
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
38 unknown: list[bytes]
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
39 """The list of files in the working directory that are not tracked."""
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
40
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
41 ignored: list[bytes]
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
42 """The list of files in the working directory that are ignored."""
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
43
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
44 clean: list[bytes]
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
45 """The list of files that are not in any other state."""
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
46
52451
f5d134e57f51 scmutil: explicitly subclass the `Status` protocol
Matt Harbison <matt_harbison@yahoo.com>
parents: 52450
diff changeset
47 @abc.abstractmethod
52450
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
48 def __iter__(self) -> Iterator[list[bytes]]:
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
49 """Iterates over each of the categories of file lists."""
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
50
52451
f5d134e57f51 scmutil: explicitly subclass the `Status` protocol
Matt Harbison <matt_harbison@yahoo.com>
parents: 52450
diff changeset
51 @abc.abstractmethod
52450
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
52 def __repr__(self) -> str:
a1c0f19e7cb4 interfaces: add a Protocol class for `scmutil.status`
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
53 """Creates a string representation of the file lists."""