##// END OF EJS Templates
typing: add type hints to argument checking functions in cmdutil...
Matt Harbison -
r50987:ce60c8d4 default
parent child Browse files
Show More
@@ -11,6 +11,15 b' import errno'
11 import os
11 import os
12 import re
12 import re
13
13
14 from typing import (
15 Any,
16 AnyStr,
17 Dict,
18 Iterable,
19 Optional,
20 cast,
21 )
22
14 from .i18n import _
23 from .i18n import _
15 from .node import (
24 from .node import (
16 hex,
25 hex,
@@ -64,14 +73,10 b' from .revlogutils import ('
64 )
73 )
65
74
66 if pycompat.TYPE_CHECKING:
75 if pycompat.TYPE_CHECKING:
67 from typing import (
76 from . import (
68 Any,
77 ui as uimod,
69 Dict,
70 )
78 )
71
79
72 for t in (Any, Dict):
73 assert t
74
75 stringio = util.stringio
80 stringio = util.stringio
76
81
77 # templates of common command options
82 # templates of common command options
@@ -268,13 +273,16 b' debugrevlogopts = ['
268 _linebelow = b"^HG: ------------------------ >8 ------------------------$"
273 _linebelow = b"^HG: ------------------------ >8 ------------------------$"
269
274
270
275
271 def check_at_most_one_arg(opts, *args):
276 def check_at_most_one_arg(
277 opts: Dict[AnyStr, Any],
278 *args: AnyStr,
279 ) -> Optional[AnyStr]:
272 """abort if more than one of the arguments are in opts
280 """abort if more than one of the arguments are in opts
273
281
274 Returns the unique argument or None if none of them were specified.
282 Returns the unique argument or None if none of them were specified.
275 """
283 """
276
284
277 def to_display(name):
285 def to_display(name: AnyStr) -> bytes:
278 return pycompat.sysbytes(name).replace(b'_', b'-')
286 return pycompat.sysbytes(name).replace(b'_', b'-')
279
287
280 previous = None
288 previous = None
@@ -289,7 +297,11 b' def check_at_most_one_arg(opts, *args):'
289 return previous
297 return previous
290
298
291
299
292 def check_incompatible_arguments(opts, first, others):
300 def check_incompatible_arguments(
301 opts: Dict[AnyStr, Any],
302 first: AnyStr,
303 others: Iterable[AnyStr],
304 ) -> None:
293 """abort if the first argument is given along with any of the others
305 """abort if the first argument is given along with any of the others
294
306
295 Unlike check_at_most_one_arg(), `others` are not mutually exclusive
307 Unlike check_at_most_one_arg(), `others` are not mutually exclusive
@@ -299,7 +311,7 b' def check_incompatible_arguments(opts, f'
299 check_at_most_one_arg(opts, first, other)
311 check_at_most_one_arg(opts, first, other)
300
312
301
313
302 def resolve_commit_options(ui, opts):
314 def resolve_commit_options(ui: "uimod.ui", opts: Dict[str, Any]) -> bool:
303 """modify commit options dict to handle related options
315 """modify commit options dict to handle related options
304
316
305 The return value indicates that ``rewrite.update-timestamp`` is the reason
317 The return value indicates that ``rewrite.update-timestamp`` is the reason
@@ -326,7 +338,7 b' def resolve_commit_options(ui, opts):'
326 return datemaydiffer
338 return datemaydiffer
327
339
328
340
329 def check_note_size(opts):
341 def check_note_size(opts: Dict[str, Any]) -> None:
330 """make sure note is of valid format"""
342 """make sure note is of valid format"""
331
343
332 note = opts.get('note')
344 note = opts.get('note')
@@ -1114,12 +1126,12 b' def bailifchanged(repo, merge=True, hint'
1114 ctx.sub(s).bailifchanged(hint=hint)
1126 ctx.sub(s).bailifchanged(hint=hint)
1115
1127
1116
1128
1117 def logmessage(ui, opts):
1129 def logmessage(ui: "uimod.ui", opts: Dict[bytes, Any]) -> Optional[bytes]:
1118 """get the log message according to -m and -l option"""
1130 """get the log message according to -m and -l option"""
1119
1131
1120 check_at_most_one_arg(opts, b'message', b'logfile')
1132 check_at_most_one_arg(opts, b'message', b'logfile')
1121
1133
1122 message = opts.get(b'message')
1134 message = cast(Optional[bytes], opts.get(b'message'))
1123 logfile = opts.get(b'logfile')
1135 logfile = opts.get(b'logfile')
1124
1136
1125 if not message and logfile:
1137 if not message and logfile:
@@ -1464,7 +1476,7 b' def openrevlog(repo, cmd, file_, opts):'
1464 return openstorage(repo, cmd, file_, opts, returnrevlog=True)
1476 return openstorage(repo, cmd, file_, opts, returnrevlog=True)
1465
1477
1466
1478
1467 def copy(ui, repo, pats, opts, rename=False):
1479 def copy(ui, repo, pats, opts: Dict[bytes, Any], rename=False):
1468 check_incompatible_arguments(opts, b'forget', [b'dry_run'])
1480 check_incompatible_arguments(opts, b'forget', [b'dry_run'])
1469
1481
1470 # called with the repo lock held
1482 # called with the repo lock held
@@ -2777,7 +2789,7 b' def cat(ui, repo, ctx, matcher, basefm, '
2777 basefm,
2789 basefm,
2778 fntemplate,
2790 fntemplate,
2779 subprefix,
2791 subprefix,
2780 **pycompat.strkwargs(opts)
2792 **pycompat.strkwargs(opts),
2781 ):
2793 ):
2782 err = 0
2794 err = 0
2783 except error.RepoLookupError:
2795 except error.RepoLookupError:
@@ -2931,7 +2943,7 b' def samefile(f, ctx1, ctx2):'
2931 return f not in ctx2.manifest()
2943 return f not in ctx2.manifest()
2932
2944
2933
2945
2934 def amend(ui, repo, old, extra, pats, opts):
2946 def amend(ui, repo, old, extra, pats, opts: Dict[str, Any]):
2935 # avoid cycle context -> subrepo -> cmdutil
2947 # avoid cycle context -> subrepo -> cmdutil
2936 from . import context
2948 from . import context
2937
2949
General Comments 0
You need to be logged in to leave comments. Login now