Mercurial > hg
view hgext/beautifygraph.py @ 51671:6fc31e7bd5db
typing: add some type hints for bundle2 capabilities
Somewhere between hg 3dbc7b1ecaba and hg 8e3f6b5bf720, pytype determined the
signature of `bundle20.capabilities` changed from `Dict[bytes, Tuple[bytes]]` to
`Dict[bytes, Union[List[bytes], Tuple[bytes]]]`.
First, I did try to simply be explicit about the previously inferred type, but
it does seem to mix and match list/tuple now (e.g. in `writenewbundle()`). I
tried changing the new list usage to tuple, but a couple of things complained,
(and I think lists of one item are a little more clear to read anyway). So then
I typed the dict value as `Sequence[bytes]`, which worked fine. But there's
also a module level `capabilities` field, and when that's typed, pytype
complains about `Sequence[bytes]` lacking `__add__`[1]. So I gave up, and just
assigned it the type it wanted, with an alias. If somebody feels motivated to
make the type consistent, it's simple enough to change the alias.
The mutable default value to the constructor was removed to appease PyCharm's
type checking on the field. (I didn't bother running the code through pytype
prior to changing it, because we've previously made an effort to remove this
pattern anyway.)
I'm not sure why `getrepocaps()` has a default value for `role` that apparently
raises an exception. It's just flagged for now so this series can land without
risking additional problems.
[1] https://foss.heptapod.net/mercurial/mercurial-devel/-/jobs/2466903
author | Matt Harbison <matt_harbison@yahoo.com> |
---|---|
date | Wed, 10 Jul 2024 17:09:34 -0400 |
parents | caa0a25f7243 |
children | f4733654f144 |
line wrap: on
line source
# -*- coding: UTF-8 -*- # beautifygraph.py - improve graph output by using Unicode characters # # Copyright 2018 John Stiles <johnstiles@gmail.com> # # This software may be used and distributed according to the terms of the # GNU General Public License version 2 or any later version. '''beautify log -G output by using Unicode characters (EXPERIMENTAL) A terminal with UTF-8 support and monospace narrow text are required. ''' from mercurial.i18n import _ from mercurial import ( encoding, extensions, graphmod, templatekw, ) # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should # be specifying the version(s) of Mercurial they are tested with, or # leave the attribute unspecified. testedwith = b'ships-with-hg-core' def prettyedge(before, edge, after): if edge == b'~': return b'\xE2\x95\xA7' # U+2567 ╧ if edge == b'/': return b'\xE2\x95\xB1' # U+2571 ╱ if edge == b'-': return b'\xE2\x94\x80' # U+2500 ─ if edge == b'|': return b'\xE2\x94\x82' # U+2502 │ if edge == b':': return b'\xE2\x94\x86' # U+2506 ┆ if edge == b'\\': return b'\xE2\x95\xB2' # U+2572 ╲ if edge == b'+': if before == b' ' and not after == b' ': return b'\xE2\x94\x9C' # U+251C ├ if after == b' ' and not before == b' ': return b'\xE2\x94\xA4' # U+2524 ┤ return b'\xE2\x94\xBC' # U+253C ┼ return edge def convertedges(line): line = b' %s ' % line pretty = [] for idx in range(len(line) - 2): pretty.append( prettyedge( line[idx : idx + 1], line[idx + 1 : idx + 2], line[idx + 2 : idx + 3], ) ) return b''.join(pretty) def getprettygraphnode(orig, *args, **kwargs): node = orig(*args, **kwargs) if node == b'o': return b'\xE2\x97\x8B' # U+25CB ○ if node == b'@': return b'\xE2\x97\x89' # U+25C9 ◉ if node == b'%': return b'\xE2\x97\x8D' # U+25CE ◎ if node == b'*': return b'\xE2\x88\x97' # U+2217 ∗ if node == b'x': return b'\xE2\x97\x8C' # U+25CC ◌ if node == b'_': return b'\xE2\x95\xA4' # U+2564 ╤ return node def outputprettygraph(orig, ui, graph, *args, **kwargs): (edges, text) = zip(*graph) graph = zip([convertedges(e) for e in edges], text) return orig(ui, graph, *args, **kwargs) def extsetup(ui): if ui.plain(b'graph'): return if encoding.encoding != b'UTF-8': ui.warn(_(b'beautifygraph: unsupported encoding, UTF-8 required\n')) return if 'A' in encoding._wide: ui.warn( _( b'beautifygraph: unsupported terminal settings, ' b'monospace narrow text required\n' ) ) return extensions.wrapfunction(graphmod, 'outputgraph', outputprettygraph) extensions.wrapfunction(templatekw, 'getgraphnode', getprettygraphnode)