##// END OF EJS Templates
encoding: add comment-based type hints for pytype...
Augie Fackler -
r44187:2ade00f3 default
parent child Browse files
Show More
@@ -20,6 +20,23 b' from . import ('
20
20
21 from .pure import charencode as charencodepure
21 from .pure import charencode as charencodepure
22
22
23 if not globals(): # hide this from non-pytype users
24 from typing import (
25 Any,
26 Callable,
27 List,
28 Text,
29 Type,
30 TypeVar,
31 Union,
32 )
33
34 # keep pyflakes happy
35 for t in (Any, Callable, List, Text, Type, Union):
36 assert t
37
38 _Tlocalstr = TypeVar('_Tlocalstr', bound=localstr)
39
23 charencode = policy.importmod(r'charencode')
40 charencode = policy.importmod(r'charencode')
24
41
25 isasciistr = charencode.isasciistr
42 isasciistr = charencode.isasciistr
@@ -45,6 +62,7 b' assert all(i.startswith((b"\\xe2", b"\\xef'
45
62
46
63
47 def hfsignoreclean(s):
64 def hfsignoreclean(s):
65 # type: (bytes) -> bytes
48 """Remove codepoints ignored by HFS+ from s.
66 """Remove codepoints ignored by HFS+ from s.
49
67
50 >>> hfsignoreclean(u'.h\u200cg'.encode('utf-8'))
68 >>> hfsignoreclean(u'.h\u200cg'.encode('utf-8'))
@@ -99,6 +117,7 b' class localstr(bytes):'
99 round-tripped to the local encoding and back'''
117 round-tripped to the local encoding and back'''
100
118
101 def __new__(cls, u, l):
119 def __new__(cls, u, l):
120 # type: (Type[_Tlocalstr], Text, bytes) -> _Tlocalstr
102 s = bytes.__new__(cls, l)
121 s = bytes.__new__(cls, l)
103 s._utf8 = u
122 s._utf8 = u
104 return s
123 return s
@@ -119,6 +138,7 b' class safelocalstr(bytes):'
119
138
120
139
121 def tolocal(s):
140 def tolocal(s):
141 # type: (Text) -> bytes
122 """
142 """
123 Convert a string from internal UTF-8 to local encoding
143 Convert a string from internal UTF-8 to local encoding
124
144
@@ -185,6 +205,7 b' def tolocal(s):'
185
205
186
206
187 def fromlocal(s):
207 def fromlocal(s):
208 # type: (bytes) -> Text
188 """
209 """
189 Convert a string from the local character encoding to UTF-8
210 Convert a string from the local character encoding to UTF-8
190
211
@@ -214,16 +235,19 b' def fromlocal(s):'
214
235
215
236
216 def unitolocal(u):
237 def unitolocal(u):
238 # type: (Text) -> bytes
217 """Convert a unicode string to a byte string of local encoding"""
239 """Convert a unicode string to a byte string of local encoding"""
218 return tolocal(u.encode('utf-8'))
240 return tolocal(u.encode('utf-8'))
219
241
220
242
221 def unifromlocal(s):
243 def unifromlocal(s):
244 # type: (bytes) -> Text
222 """Convert a byte string of local encoding to a unicode string"""
245 """Convert a byte string of local encoding to a unicode string"""
223 return fromlocal(s).decode('utf-8')
246 return fromlocal(s).decode('utf-8')
224
247
225
248
226 def unimethod(bytesfunc):
249 def unimethod(bytesfunc):
250 # type: (Callable[[Any], bytes]) -> Callable[[Any], Text]
227 """Create a proxy method that forwards __unicode__() and __str__() of
251 """Create a proxy method that forwards __unicode__() and __str__() of
228 Python 3 to __bytes__()"""
252 Python 3 to __bytes__()"""
229
253
@@ -281,11 +305,13 b' else:'
281
305
282
306
283 def colwidth(s):
307 def colwidth(s):
308 # type: (bytes) -> int
284 b"Find the column width of a string for display in the local encoding"
309 b"Find the column width of a string for display in the local encoding"
285 return ucolwidth(s.decode(_sysstr(encoding), r'replace'))
310 return ucolwidth(s.decode(_sysstr(encoding), r'replace'))
286
311
287
312
288 def ucolwidth(d):
313 def ucolwidth(d):
314 # type: (Text) -> int
289 b"Find the column width of a Unicode string for display"
315 b"Find the column width of a Unicode string for display"
290 eaw = getattr(unicodedata, 'east_asian_width', None)
316 eaw = getattr(unicodedata, 'east_asian_width', None)
291 if eaw is not None:
317 if eaw is not None:
@@ -294,6 +320,7 b' def ucolwidth(d):'
294
320
295
321
296 def getcols(s, start, c):
322 def getcols(s, start, c):
323 # type: (bytes, int, int) -> bytes
297 '''Use colwidth to find a c-column substring of s starting at byte
324 '''Use colwidth to find a c-column substring of s starting at byte
298 index start'''
325 index start'''
299 for x in pycompat.xrange(start + c, len(s)):
326 for x in pycompat.xrange(start + c, len(s)):
@@ -303,6 +330,7 b' def getcols(s, start, c):'
303
330
304
331
305 def trim(s, width, ellipsis=b'', leftside=False):
332 def trim(s, width, ellipsis=b'', leftside=False):
333 # type: (bytes, int, bytes, bool) -> bytes
306 """Trim string 's' to at most 'width' columns (including 'ellipsis').
334 """Trim string 's' to at most 'width' columns (including 'ellipsis').
307
335
308 If 'leftside' is True, left side of string 's' is trimmed.
336 If 'leftside' is True, left side of string 's' is trimmed.
@@ -400,6 +428,7 b" def trim(s, width, ellipsis=b'', leftsid"
400
428
401
429
402 def lower(s):
430 def lower(s):
431 # type: (bytes) -> bytes
403 b"best-effort encoding-aware case-folding of local string s"
432 b"best-effort encoding-aware case-folding of local string s"
404 try:
433 try:
405 return asciilower(s)
434 return asciilower(s)
@@ -422,6 +451,7 b' def lower(s):'
422
451
423
452
424 def upper(s):
453 def upper(s):
454 # type: (bytes) -> bytes
425 b"best-effort encoding-aware case-folding of local string s"
455 b"best-effort encoding-aware case-folding of local string s"
426 try:
456 try:
427 return asciiupper(s)
457 return asciiupper(s)
@@ -430,6 +460,7 b' def upper(s):'
430
460
431
461
432 def upperfallback(s):
462 def upperfallback(s):
463 # type: (Any) -> Any
433 try:
464 try:
434 if isinstance(s, localstr):
465 if isinstance(s, localstr):
435 u = s._utf8.decode("utf-8")
466 u = s._utf8.decode("utf-8")
@@ -464,6 +495,7 b' class normcasespecs(object):'
464
495
465
496
466 def jsonescape(s, paranoid=False):
497 def jsonescape(s, paranoid=False):
498 # type: (Any, Any) -> Any
467 '''returns a string suitable for JSON
499 '''returns a string suitable for JSON
468
500
469 JSON is problematic for us because it doesn't support non-Unicode
501 JSON is problematic for us because it doesn't support non-Unicode
@@ -527,6 +559,7 b' else:'
527
559
528
560
529 def getutf8char(s, pos):
561 def getutf8char(s, pos):
562 # type: (Any, Any) -> Any
530 '''get the next full utf-8 character in the given string, starting at pos
563 '''get the next full utf-8 character in the given string, starting at pos
531
564
532 Raises a UnicodeError if the given location does not start a valid
565 Raises a UnicodeError if the given location does not start a valid
@@ -545,6 +578,7 b' def getutf8char(s, pos):'
545
578
546
579
547 def toutf8b(s):
580 def toutf8b(s):
581 # type: (Any) -> Any
548 '''convert a local, possibly-binary string into UTF-8b
582 '''convert a local, possibly-binary string into UTF-8b
549
583
550 This is intended as a generic method to preserve data when working
584 This is intended as a generic method to preserve data when working
@@ -613,6 +647,7 b' def toutf8b(s):'
613
647
614
648
615 def fromutf8b(s):
649 def fromutf8b(s):
650 # type: (Text) -> bytes
616 '''Given a UTF-8b string, return a local, possibly-binary string.
651 '''Given a UTF-8b string, return a local, possibly-binary string.
617
652
618 return the original binary string. This
653 return the original binary string. This
@@ -24,4 +24,5 b' run pyflakes on all tracked files ending'
24 contrib/perf.py:*: undefined name 'xrange' (glob) (?)
24 contrib/perf.py:*: undefined name 'xrange' (glob) (?)
25 mercurial/hgweb/server.py:*: undefined name 'reload' (glob) (?)
25 mercurial/hgweb/server.py:*: undefined name 'reload' (glob) (?)
26 mercurial/util.py:*: undefined name 'file' (glob) (?)
26 mercurial/util.py:*: undefined name 'file' (glob) (?)
27 mercurial/encoding.py:*: undefined name 'localstr' (glob) (?)
27
28
General Comments 0
You need to be logged in to leave comments. Login now