comparison mercurial/pure/bdiff.py @ 51934:09f3a6790e56

interfaces: add the optional `bdiff.xdiffblocks()` method PyCharm flagged where this was called on the protocol class in `mdiff.py` in the previous commit, but pytype completely missed it. PyCharm is correct here, but I'm committing this separately to highlight this potential problem- some of the implementations don't implement _all_ of the methods the others do, and there's not a great way to indicate on a protocol class that a method or attribute is optional- that's kinda the opposite of what static typing is about. Making the method an `Optional[Callable]` attribute works here, and keeps both PyCharm and pytype happy, and the generated `mdiff.pyi` and `modules.pyi` look reasonable. We might be getting a little lucky, because the method isn't invoked directly- it is returned from another method that selects which block function to use. Except since it is declared on the protocol class, every module needs this attribute (in theory, but in practice this doesn't seem to be checked), so the check for it on the module has to change from `hasattr()` to `getattr(..., None)`. We defer defining the optional attrs to the type checking phase as an extra precaution- that way it isn't an attr with a `None` value at runtime if someone is still using `hasattr()`. As to why pytype missed this, I have no clue. The generated `mdiff.pyi` even has the global variable typed as `bdiff: intmod.BDiff`, so uses of it really should comply with what is on the class, protocol class or not.
author Matt Harbison <matt_harbison@yahoo.com>
date Sun, 29 Sep 2024 02:03:20 -0400
parents f4733654f144
children
comparison
equal deleted inserted replaced
51933:f2832de2a46c 51934:09f3a6790e56
8 from __future__ import annotations 8 from __future__ import annotations
9 9
10 import difflib 10 import difflib
11 import re 11 import re
12 import struct 12 import struct
13 import typing
13 14
14 from typing import ( 15 from typing import (
15 List, 16 List,
17 Optional,
16 Tuple, 18 Tuple,
19 )
20
21 from ..interfaces import (
22 modules as intmod,
17 ) 23 )
18 24
19 25
20 def splitnewlines(text: bytes) -> List[bytes]: 26 def splitnewlines(text: bytes) -> List[bytes]:
21 '''like str.splitlines, but only split on newlines.''' 27 '''like str.splitlines, but only split on newlines.'''
105 text = re.sub(b'[ \t\r]+', b'', text) 111 text = re.sub(b'[ \t\r]+', b'', text)
106 else: 112 else:
107 text = re.sub(b'[ \t\r]+', b' ', text) 113 text = re.sub(b'[ \t\r]+', b' ', text)
108 text = text.replace(b' \n', b'\n') 114 text = text.replace(b' \n', b'\n')
109 return text 115 return text
116
117
118 # In order to adhere to the module protocol, these functions must be visible to
119 # the type checker, though they aren't actually implemented by this
120 # implementation of the module protocol. Callers are responsible for
121 # checking that the implementation is available before using them.
122 if typing.TYPE_CHECKING:
123 xdiffblocks: Optional[intmod.BDiffBlocksFnc] = None