comparison mercurial/diffutil.py @ 49705:b8cac4e37100 stable

typing: add typehints to mercurial/diffutil.py Lack of typehints here caused the fact that TortoiseHg was passing str instead of bytes as the key in `opts` to be missed, resulting in shelf corruption in cases where `diff.git` is required.
author Matt Harbison <matt_harbison@yahoo.com>
date Tue, 28 Feb 2023 16:42:38 -0500
parents 6000f5b25c9b
children a6b8b1ab9116
comparison
equal deleted inserted replaced
49704:a6b497872b97 49705:b8cac4e37100
5 # Copyright 2018 Octobus <octobus@octobus.net> 5 # Copyright 2018 Octobus <octobus@octobus.net>
6 # 6 #
7 # This software may be used and distributed according to the terms of the 7 # This software may be used and distributed according to the terms of the
8 # GNU General Public License version 2 or any later version. 8 # GNU General Public License version 2 or any later version.
9 9
10 import typing
11
12 from typing import (
13 Any,
14 Dict,
15 Optional,
16 )
10 17
11 from .i18n import _ 18 from .i18n import _
12 19
13 from . import ( 20 from . import (
14 mdiff, 21 mdiff,
15 pycompat, 22 pycompat,
16 ) 23 )
17 24
25 if typing.TYPE_CHECKING:
26 from . import ui as uimod
27
28 # TODO: narrow the value after the config module is typed
29 _Opts = Dict[bytes, Any]
30
18 31
19 def diffallopts( 32 def diffallopts(
20 ui, opts=None, untrusted=False, section=b'diff', configprefix=b'' 33 ui: "uimod.ui",
21 ): 34 opts: Optional[_Opts] = None,
35 untrusted: bool = False,
36 section: bytes = b'diff',
37 configprefix: bytes = b'',
38 ) -> mdiff.diffopts:
22 '''return diffopts with all features supported and parsed''' 39 '''return diffopts with all features supported and parsed'''
23 return difffeatureopts( 40 return difffeatureopts(
24 ui, 41 ui,
25 opts=opts, 42 opts=opts,
26 untrusted=untrusted, 43 untrusted=untrusted,
31 configprefix=configprefix, 48 configprefix=configprefix,
32 ) 49 )
33 50
34 51
35 def difffeatureopts( 52 def difffeatureopts(
36 ui, 53 ui: "uimod.ui",
37 opts=None, 54 opts: Optional[_Opts] = None,
38 untrusted=False, 55 untrusted: bool = False,
39 section=b'diff', 56 section: bytes = b'diff',
40 git=False, 57 git: bool = False,
41 whitespace=False, 58 whitespace: bool = False,
42 formatchanging=False, 59 formatchanging: bool = False,
43 configprefix=b'', 60 configprefix: bytes = b'',
44 ): 61 ) -> mdiff.diffopts:
45 """return diffopts with only opted-in features parsed 62 """return diffopts with only opted-in features parsed
46 63
47 Features: 64 Features:
48 - git: git-style diffs 65 - git: git-style diffs
49 - whitespace: whitespace options like ignoreblanklines and ignorews 66 - whitespace: whitespace options like ignoreblanklines and ignorews
50 - formatchanging: options that will likely break or cause correctness issues 67 - formatchanging: options that will likely break or cause correctness issues
51 with most diff parsers 68 with most diff parsers
52 """ 69 """
53 70
54 def get(key, name=None, getter=ui.configbool, forceplain=None): 71 def get(
72 key: bytes,
73 name: Optional[bytes] = None,
74 getter=ui.configbool,
75 forceplain: Optional[bool] = None,
76 ) -> Any:
55 if opts: 77 if opts:
56 v = opts.get(key) 78 v = opts.get(key)
57 # diffopts flags are either None-default (which is passed 79 # diffopts flags are either None-default (which is passed
58 # through unchanged, so we can identify unset values), or 80 # through unchanged, so we can identify unset values), or
59 # some other falsey default (eg --unified, which defaults 81 # some other falsey default (eg --unified, which defaults