comparison mercurial/encoding.py @ 43685:da925257a39e

typing: add pseudo localstr.__init__() to help pytype Apparently, pytype failed to parse localstr.__new__()? This fixes the following errors: line 126, in __hash__: No attribute '_utf8' on localstr [attribute-error] line 188, in tolocal: Function localstr.__init__ was called with the wrong arguments [wrong-arg-types] Expected: (self, string: str, ...) Actually passed: (self, string: bytes, ...)
author Yuya Nishihara <yuya@tcha.org>
date Sat, 16 Nov 2019 16:25:28 +0900
parents 009c115eba95
children 7b14d649af1b
comparison
equal deleted inserted replaced
43684:009c115eba95 43685:da925257a39e
18 pycompat, 18 pycompat,
19 ) 19 )
20 20
21 from .pure import charencode as charencodepure 21 from .pure import charencode as charencodepure
22 22
23 _TYPE_CHECKING = False
24
23 if not globals(): # hide this from non-pytype users 25 if not globals(): # hide this from non-pytype users
24 from typing import ( 26 from typing import (
25 Any, 27 Any,
26 Callable, 28 Callable,
27 List, 29 List,
30 TYPE_CHECKING as _TYPE_CHECKING,
28 Text, 31 Text,
29 Type, 32 Type,
30 TypeVar, 33 TypeVar,
31 Union, 34 Union,
32 ) 35 )
115 class localstr(bytes): 118 class localstr(bytes):
116 '''This class allows strings that are unmodified to be 119 '''This class allows strings that are unmodified to be
117 round-tripped to the local encoding and back''' 120 round-tripped to the local encoding and back'''
118 121
119 def __new__(cls, u, l): 122 def __new__(cls, u, l):
120 # type: (Type[_Tlocalstr], bytes, bytes) -> _Tlocalstr
121 s = bytes.__new__(cls, l) 123 s = bytes.__new__(cls, l)
122 s._utf8 = u 124 s._utf8 = u
123 return s 125 return s
126
127 if _TYPE_CHECKING:
128 # pseudo implementation to help pytype see localstr() constructor
129 def __init__(self, u, l):
130 # type: (bytes, bytes) -> None
131 super(localstr, self).__init__(l)
132 self._utf8 = u
124 133
125 def __hash__(self): 134 def __hash__(self):
126 return hash(self._utf8) # avoid collisions in local string space 135 return hash(self._utf8) # avoid collisions in local string space
127 136
128 137