|
11 | 11 | importos |
12 | 12 | importsys |
13 | 13 |
|
14 | | - |
15 | 14 | fromgitdb.utils.encodingimport ( |
16 | 15 | force_bytes,# @UnusedImport |
17 | 16 | force_text# @UnusedImport |
18 | 17 | ) |
19 | 18 |
|
| 19 | +# typing -------------------------------------------------------------------- |
| 20 | + |
| 21 | +fromtypingimportAny,AnyStr,Dict,Optional,Type |
| 22 | +fromgit.typesimportTBD |
| 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 | | -defsafe_decode(s): |
| 33 | +defsafe_decode(s:Optional[AnyStr])->Optional[str]: |
28 | 34 | """Safely decodes a binary string to unicode""" |
29 | 35 | ifisinstance(s,str): |
30 | 36 | returns |
31 | 37 | elifisinstance(s,bytes): |
32 | 38 | returns.decode(defenc,'surrogateescape') |
33 | | -elifsisnotNone: |
| 39 | +elifsisNone: |
| 40 | +returnNone |
| 41 | +else: |
34 | 42 | raiseTypeError('Expected bytes or text, but got %r'% (s,)) |
35 | 43 |
|
36 | 44 |
|
37 | | -defsafe_encode(s): |
38 | | -"""Safelydecodes a binary string to unicode""" |
| 45 | +defsafe_encode(s:Optional[AnyStr])->Optional[bytes]: |
| 46 | +"""Safelyencodes a binary string to unicode""" |
39 | 47 | ifisinstance(s,str): |
40 | 48 | returns.encode(defenc) |
41 | 49 | elifisinstance(s,bytes): |
42 | 50 | returns |
43 | | -elifsisnotNone: |
| 51 | +elifsisNone: |
| 52 | +returnNone |
| 53 | +else: |
44 | 54 | raiseTypeError('Expected bytes or text, but got %r'% (s,)) |
45 | 55 |
|
46 | 56 |
|
47 | | -defwin_encode(s): |
| 57 | +defwin_encode(s:Optional[AnyStr])->Optional[bytes]: |
48 | 58 | """Encode unicodes for process arguments on Windows.""" |
49 | 59 | ifisinstance(s,str): |
50 | 60 | returns.encode(locale.getpreferredencoding(False)) |
51 | 61 | elifisinstance(s,bytes): |
52 | 62 | returns |
53 | 63 | elifsisnotNone: |
54 | 64 | raiseTypeError('Expected bytes or text, but got %r'% (s,)) |
| 65 | +returnNone |
| 66 | + |
55 | 67 |
|
56 | 68 |
|
57 | | -defwith_metaclass(meta,*bases): |
| 69 | +defwith_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 | | -classmetaclass(meta): |
| 71 | + |
| 72 | +classmetaclass(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 | ifnbasesisNone: |
65 | 78 | returntype.__new__(cls,name, (),d) |
66 | 79 | returnmeta(name,bases,d) |
| 80 | + |
67 | 81 | returnmetaclass(meta.__name__+'Helper',None, {}) |