##// END OF EJS Templates
interfaces: convert `repository.ipeerconnection` from zope `Attribute` attrs...
interfaces: convert `repository.ipeerconnection` from zope `Attribute` attrs This is the same transformation as b455dfddfed0 did for dirstate. Since type annotations are wrapped up in this transformation (the file syntax is such that *some* type needs to be declared, even if `Any`), this can be done piecemeal. See that commit for more background detail. Initially, I imported `uimod` here, delayed to the type checking phase, like a bunch of other modules are currently doing. That caused a problem with pytype no longer inferring types in some seemingly unrelated modules, which was also mentioned in a1c0f19e7cb4. None of the following applies to this commit, because dropping the import and aliasing `Ui = Any` until we get a Protocol class in place avoids this issue. But for posterity, the exact problem was that pytype 2023.11.21 on Python 3.10.11 stopped inferring the (currently undeclared) `int` type of the following attributes in `mercurial.revlog`, switching them to `Any`. - COMP_MODE_INLINE - REVIDX_DEFAULT_FLAGS - REVIDX_ELLIPSIS - REVIDX_EXTSTORED - REVIDX_FLAGS_ORDER - REVIDX_HASCOPIESINFO - REVIDX_ISCENSORED - REVIDX_RAWTEXT_CHANGING_FLAGS That had cascading effects on `hgext.remotefilelog.shallowutil`, `mercurial.metadata`, and `mercurial.pure.parsers`, mostly on return types it was no longer able to infer. These fields above are imported directly through a couple of `mercurial.revlogutils` modules. Unfortunately, explicitly typing those original fields only preserved the type for `COMP_MODE_INLINE`. The only difference I see is this field is defined in `mercurial/revlogutils/__init__.py`, and the rest are defined in `mercurial/revlogutils/constants.py`, but I don't know the significance of that. The `mercurial/revlogutils/*.pyi` files have the correct type with or without the explicit typing when the import is present- it's only the `revlog` module's imports that seem to be affected. And since they are direct imports, there's no way that I know of to assign a type, like there would be if there was a field assignment. There is an import cycle of sorts here when importing ui, that is likely the problem: interfaces.repository -> ui -> utils.urlutil -> revlogutils.constants -> interfaces.repository Running `ls -gGrt --time-style=full` on the associated *.pyi files that get generated showed that `revlog.pyi` was created before the `revlogutils` files. I tried moving the import of `utils.urlutil` in `mercurial.ui` into `ui.paths()` instead of a top level import, and that didn't help. It could be argued that `ui.pyi` would need the import anyway because `ui.paths()` returns an instance of `utils.urlutil.paths`, but pytype is currently typing that as `Any`, because it gets confused by the `@util.propertycache` decorator (replacing that with `Any`), and it dropped the import of `utils.urlutil` as expected with that change. I'm a little skeptical that cycle is a problem though, because `interfaces.repository` also started importing `utils.urlutil` here, which means there's this cycle that's not a problem: interfaces.repository -> utils.urlutil -> revlogutils.constants -> interfaces.repository PyCharm is also able to sniff out the types in `mercurial.revlog` with the import, so I'm not sure if this is a bug/limitation in pytype, or what.

File last commit:

r37144:4bd73a95 default
r53360:0cfcc276 default
Show More
compat.py
101 lines | 2.5 KiB | text/x-python | PythonLexer
from math import ldexp
import struct
import sys
if sys.version_info.major < 3:
from datetime import tzinfo, timedelta
class timezone(tzinfo):
def __init__(self, offset):
self.offset = offset
def utcoffset(self, dt):
return self.offset
def dst(self, dt):
return timedelta(0)
def tzname(self, dt):
return 'UTC+00:00'
def as_unicode(string):
return string.decode('utf-8')
def iteritems(self):
return self.iteritems()
def bytes_from_list(values):
return bytes(bytearray(values))
byte_as_integer = ord
timezone.utc = timezone(timedelta(0))
xrange = xrange # noqa: F821
long = long # noqa: F821
unicode = unicode # noqa: F821
else:
from datetime import timezone
def byte_as_integer(bytestr):
return bytestr[0]
def as_unicode(string):
return string
def iteritems(self):
return self.items()
xrange = range
long = int
unicode = str
bytes_from_list = bytes
if sys.version_info.major >= 3 and sys.version_info.minor >= 6:
# Python 3.6 added 16 bit floating point to struct
def pack_float16(value):
try:
return struct.pack('>Be', 0xf9, value)
except OverflowError:
return False
def unpack_float16(payload):
return struct.unpack('>e', payload)[0]
else:
def pack_float16(value):
# Based on node-cbor by hildjj
# which was based in turn on Carsten Borman's cn-cbor
u32 = struct.pack('>f', value)
u = struct.unpack('>I', u32)[0]
if u & 0x1FFF != 0:
return False
s16 = (u >> 16) & 0x8000
exponent = (u >> 23) & 0xff
mantissa = u & 0x7fffff
if 113 <= exponent <= 142:
s16 += ((exponent - 112) << 10) + (mantissa >> 13)
elif 103 <= exponent < 113:
if mantissa & ((1 << (126 - exponent)) - 1):
return False
s16 += ((mantissa + 0x800000) >> (126 - exponent))
else:
return False
return struct.pack('>BH', 0xf9, s16)
def unpack_float16(payload):
# Code adapted from RFC 7049, appendix D
def decode_single(single):
return struct.unpack("!f", struct.pack("!I", single))[0]
payload = struct.unpack('>H', payload)[0]
value = (payload & 0x7fff) << 13 | (payload & 0x8000) << 16
if payload & 0x7c00 != 0x7c00:
return ldexp(decode_single(value), 112)
return decode_single(value | 0x7f800000)