|
11 | 11 | import os |
12 | 12 | import sys |
13 | 13 |
|
14 | | - |
15 | 14 | from gitdb.utils.encoding import ( |
16 | 15 | force_bytes, # @UnusedImport |
17 | 16 | force_text # @UnusedImport |
18 | 17 | ) |
19 | 18 |
|
| 19 | +# typing -------------------------------------------------------------------- |
| 20 | + |
| 21 | +from typing import Any, AnyStr, Dict, Optional, Type |
| 22 | +from git.types import TBD |
| 23 | + |
| 24 | +# --------------------------------------------------------------------------- |
20 | 25 |
|
21 | | -is_win = (os.name == 'nt') |
| 26 | + |
| 27 | +is_win = (os.name == 'nt') # type: bool |
22 | 28 | is_posix = (os.name == 'posix') |
23 | 29 | is_darwin = (os.name == 'darwin') |
24 | 30 | defenc = sys.getfilesystemencoding() |
25 | 31 |
|
26 | 32 |
|
27 | | -def safe_decode(s): |
| 33 | +def safe_decode(s: Optional[AnyStr]) -> Optional[str]: |
28 | 34 | """Safely decodes a binary string to unicode""" |
29 | 35 | if isinstance(s, str): |
30 | 36 | return s |
31 | 37 | elif isinstance(s, bytes): |
32 | 38 | return s.decode(defenc, 'surrogateescape') |
33 | | - elif s is not None: |
| 39 | + elif s is None: |
| 40 | + return None |
| 41 | + else: |
34 | 42 | raise TypeError('Expected bytes or text, but got %r' % (s,)) |
35 | 43 |
|
36 | 44 |
|
37 | | -def safe_encode(s): |
38 | | - """Safely decodes a binary string to unicode""" |
| 45 | +def safe_encode(s: Optional[AnyStr]) -> Optional[bytes]: |
| 46 | + """Safely encodes a binary string to unicode""" |
39 | 47 | if isinstance(s, str): |
40 | 48 | return s.encode(defenc) |
41 | 49 | elif isinstance(s, bytes): |
42 | 50 | return s |
43 | | - elif s is not None: |
| 51 | + elif s is None: |
| 52 | + return None |
| 53 | + else: |
44 | 54 | raise TypeError('Expected bytes or text, but got %r' % (s,)) |
45 | 55 |
|
46 | 56 |
|
47 | | -def win_encode(s): |
| 57 | +def win_encode(s: Optional[AnyStr]) -> Optional[bytes]: |
48 | 58 | """Encode unicodes for process arguments on Windows.""" |
49 | 59 | if isinstance(s, str): |
50 | 60 | return s.encode(locale.getpreferredencoding(False)) |
51 | 61 | elif isinstance(s, bytes): |
52 | 62 | return s |
53 | 63 | elif s is not None: |
54 | 64 | raise TypeError('Expected bytes or text, but got %r' % (s,)) |
| 65 | + return None |
| 66 | + |
55 | 67 |
|
56 | 68 |
|
57 | | -def with_metaclass(meta, *bases): |
| 69 | +def with_metaclass(meta: Type[Any], *bases: Any) -> 'metaclass': # type: ignore ## mypy cannot understand dynamic class creation |
58 | 70 | """copied from https://github.com/Byron/bcore/blob/master/src/python/butility/future.py#L15""" |
59 | | - class metaclass(meta): |
| 71 | + |
| 72 | + class metaclass(meta): # type: ignore |
60 | 73 | __call__ = type.__call__ |
61 | | - __init__ = type.__init__ |
| 74 | + __init__ = type.__init__ # type: ignore |
62 | 75 |
|
63 | | - def __new__(cls, name, nbases, d): |
| 76 | + def __new__(cls, name: str, nbases: Optional[int], d: Dict[str, Any]) -> TBD: |
64 | 77 | if nbases is None: |
65 | 78 | return type.__new__(cls, name, (), d) |
66 | 79 | return meta(name, bases, d) |
| 80 | + |
67 | 81 | return metaclass(meta.__name__ + 'Helper', None, {}) |
0 commit comments