changeset 43725: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 1fb19665c166
files mercurial/encoding.py
diffstat 1 files changed, 10 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/encoding.py	Sat Nov 16 15:24:49 2019 +0900
+++ b/mercurial/encoding.py	Sat Nov 16 16:25:28 2019 +0900
@@ -20,11 +20,14 @@
 
 from .pure import charencode as charencodepure
 
+_TYPE_CHECKING = False
+
 if not globals():  # hide this from non-pytype users
     from typing import (
         Any,
         Callable,
         List,
+        TYPE_CHECKING as _TYPE_CHECKING,
         Text,
         Type,
         TypeVar,
@@ -117,11 +120,17 @@
     round-tripped to the local encoding and back'''
 
     def __new__(cls, u, l):
-        # type: (Type[_Tlocalstr], bytes, bytes) -> _Tlocalstr
         s = bytes.__new__(cls, l)
         s._utf8 = u
         return s
 
+    if _TYPE_CHECKING:
+        # pseudo implementation to help pytype see localstr() constructor
+        def __init__(self, u, l):
+            # type: (bytes, bytes) -> None
+            super(localstr, self).__init__(l)
+            self._utf8 = u
+
     def __hash__(self):
         return hash(self._utf8)  # avoid collisions in local string space