Skip to content

Commit b438739

Browse files
Soluminrchen152
authored andcommitted
Add Python 3.8 signatures for bytes.hex, memoryview.hex and bytearray.hex.
PiperOrigin-RevId: 446520548
1 parent 8364eae commit b438739

File tree

2 files changed

+81
-61
lines changed

2 files changed

+81
-61
lines changed

pytype/stubs/builtins/builtins.pytd

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,10 @@ class bytes(Sequence[int]):
377377
def endswith(self, suffix: Union[bytes, Tuple[bytes, ...]]) -> bool: ...
378378
def expandtabs(self, tabsize: int = ...) -> bytes: ...
379379
def find(self, sub: Union[bytes, int], start: Optional[int] = ..., end: Optional[int] = ...) -> int: ...
380-
def hex(self) -> str: ...
380+
if sys.version_info >= (3,8):
381+
def hex(self, sep: str | bytes = ..., bytes_per_sep: SupportsIndex = ...) -> str: ...
382+
else:
383+
def hex(self) -> str: ...
381384
def index(self, sub: Union[bytes, int], start: Optional[int] = ..., end: Optional[int] = ...) -> int: ...
382385
def isalnum(self) -> bool: ...
383386
def isalpha(self) -> bool: ...
@@ -445,6 +448,10 @@ class bytearray(MutableSequence[int]):
445448
def expandtabs(self, *args, **kwargs) -> bytearray: ...
446449
def extend(self, iterable_int) -> NoneType: ...
447450
def find(self, sub: Union[bytes, bytearray], *args, **kwargs) -> int: ...
451+
if sys.version_info >= (3,8):
452+
def hex(self, sep: str | bytes = ..., bytes_per_sep: SupportsIndex = ...) -> str: ...
453+
else:
454+
def hex(self) -> str: ...
448455
def index(self, sub: Union[bytes, bytearray], *args, **kwargs) -> int: ...
449456
def insert(self, index: int, int: Union[bytes, int]) -> NoneType: ...
450457
def isalnum(self) -> bool: ...
@@ -952,7 +959,9 @@ class memoryview(object):
952959
if sys.version_info >= (3, 2):
953960
def __enter__(self) -> memoryview: ...
954961
def __exit__(self, exc_type: Optional[Type[BaseException]], exc_value: Optional[BaseException], traceback: Optional[Any]) -> Optional[bool]: ...
955-
if sys.version_info >= (3, 5):
962+
if sys.version_info >= (3,8):
963+
def hex(self, sep: str | bytes = ..., bytes_per_sep: SupportsIndex = ...) -> str: ...
964+
else:
956965
def hex(self) -> str: ...
957966

958967
class type(Callable, Type[_T]):

pytype/tests/test_builtins4.py

Lines changed: 70 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def test_basic(self):
1414
""")
1515
self.assertTypesMatchPytd(ty, """
1616
from typing import Iterator
17-
v = ... # type: Iterator[int]
17+
v : Iterator[int]
1818
""")
1919

2020
def test_lambda(self):
@@ -48,9 +48,9 @@ def test_empty(self):
4848
""", deep=False)
4949
self.assertTypesMatchPytd(ty, """
5050
from typing import Any, List, Iterator
51-
lst1 = ... # type: List[nothing]
52-
lst2 = ... # type: List[nothing]
53-
lst3 = ... # type: Iterator[nothing]
51+
lst1 : List[nothing]
52+
lst2 : List[nothing]
53+
lst3 : Iterator[nothing]
5454
""")
5555

5656
def test_heterogeneous(self):
@@ -181,7 +181,7 @@ def g(x: object) -> None: ...
181181
def h(x: Any) -> None: ...
182182
def i(x: type) -> None: ...
183183
def j(x: Type[super]) -> None: ...
184-
v = ... # type: Type[super]
184+
v : Type[super]
185185
""")
186186

187187
def test_bytearray_slice(self):
@@ -195,22 +195,22 @@ def g(x: bytearray) -> bytearray:
195195
def test_set_length(self):
196196
self.Check("""
197197
from typing import Set
198-
x = ... # type: Set[int]
198+
x : Set[int]
199199
len(x)
200200
len(set())
201201
""")
202202

203203
def test_sequence_length(self):
204204
self.Check("""
205205
from typing import Sequence
206-
x = ... # type: Sequence
206+
x : Sequence
207207
len(x)
208208
""")
209209

210210
def test_mapping_length(self):
211211
self.Check("""
212212
from typing import Mapping
213-
x = ... # type: Mapping
213+
x : Mapping
214214
len(x)
215215
""")
216216

@@ -295,12 +295,12 @@ def test_str_join(self):
295295
h = u",".join([u"foo", "bar"])
296296
""", deep=False)
297297
self.assertTypesMatchPytd(ty, """
298-
b = ... # type: str
299-
d = ... # type: str
300-
e = ... # type: str
301-
f = ... # type: str
302-
g = ... # type: str
303-
h = ... # type: str
298+
b : str
299+
d : str
300+
e : str
301+
f : str
302+
g : str
303+
h : str
304304
""")
305305

306306
@test_utils.skipBeforePy((3, 9), "removeprefix and removesuffix new in 3.9")
@@ -311,9 +311,9 @@ def test_str_remove_prefix_suffix(self):
311311
c = a.removesuffix("_suffix")
312312
""", deep=False)
313313
self.assertTypesMatchPytd(ty, """
314-
a = ... # type: str
315-
b = ... # type: str
316-
c = ... # type: str
314+
a : str
315+
b : str
316+
c : str
317317
""")
318318

319319
def test_str_is_hashable(self):
@@ -330,8 +330,8 @@ def test_bytearray_join(self):
330330
x2 = b.join([b"x"])
331331
""", deep=False)
332332
self.assertTypesMatchPytd(ty, """
333-
b = ... # type: bytearray
334-
x2 = ... # type: bytearray
333+
b : bytearray
334+
x2 : bytearray
335335
""")
336336

337337
def test_iter1(self):
@@ -342,9 +342,9 @@ def test_iter1(self):
342342
""", deep=False)
343343
self.assertTypesMatchPytd(ty, """
344344
from typing import Union
345-
a = ... # type: int
346-
b = ... # type: int
347-
c = ... # type: Union[int, str]
345+
a : int
346+
b : int
347+
c : Union[int, str]
348348
""")
349349

350350
def test_dict_keys(self):
@@ -357,11 +357,11 @@ def test_dict_keys(self):
357357
""", deep=False)
358358
self.assertTypesMatchPytd(ty, """
359359
from typing import Dict, Set, Union
360-
m = ... # type: Dict[str, None]
361-
a = ... # type: Set[str]
362-
b = ... # type: Set[str]
363-
c = ... # type: Set[Union[int, str]]
364-
d = ... # type: Set[Union[int, str]]
360+
m : Dict[str, None]
361+
a : Set[str]
362+
b : Set[str]
363+
c : Set[Union[int, str]]
364+
d : Set[Union[int, str]]
365365
""")
366366

367367
def test_open(self):
@@ -419,12 +419,12 @@ def f(x: int):
419419
import re
420420
from typing import Iterator
421421
def f(x: int) -> None: ...
422-
x1 = ... # type: Iterator[int]
423-
x2 = ... # type: Iterator[bool, ...]
424-
x3 = ... # type: Iterator[bool]
425-
x4 = ... # type: Iterator[int]
426-
x5 = ... # type: Iterator[int]
427-
x6 = ... # type: Iterator[str]
422+
x1 : Iterator[int]
423+
x2 : Iterator[bool, ...]
424+
x3 : Iterator[bool]
425+
x4 : Iterator[int]
426+
x5 : Iterator[int]
427+
x6 : Iterator[str]
428428
""")
429429

430430
def test_filter_types(self):
@@ -447,11 +447,11 @@ def test_zip(self):
447447
""")
448448
self.assertTypesMatchPytd(ty, """
449449
from typing import Iterator, Tuple, Union
450-
a = ... # type: Iterator[nothing]
451-
b = ... # type: Iterator[Tuple[Union[int, complex]]]
452-
c = ... # type: Iterator[nothing]
453-
d = ... # type: Iterator[nothing]
454-
e = ... # type: Iterator[Tuple[complex, int]]
450+
a : Iterator[nothing]
451+
b : Iterator[Tuple[Union[int, complex]]]
452+
c : Iterator[nothing]
453+
d : Iterator[nothing]
454+
e : Iterator[Tuple[complex, int]]
455455
""")
456456

457457
def test_dict(self):
@@ -482,8 +482,8 @@ def test_list_init(self):
482482
""", deep=False)
483483
self.assertTypesMatchPytd(ty, """
484484
from typing import List
485-
l3 = ... # type: List[str]
486-
l4 = ... # type: List[int]
485+
l3 : List[str]
486+
l4 : List[int]
487487
""")
488488

489489
def test_tuple_init(self):
@@ -493,8 +493,8 @@ def test_tuple_init(self):
493493
""", deep=False)
494494
self.assertTypesMatchPytd(ty, """
495495
from typing import Tuple
496-
t3 = ... # type: Tuple[str, ...]
497-
t4 = ... # type: Tuple[int, ...]
496+
t3 : Tuple[str, ...]
497+
t4 : Tuple[int, ...]
498498
""")
499499

500500
def test_items(self):
@@ -503,7 +503,7 @@ def test_items(self):
503503
""", deep=False)
504504
self.assertTypesMatchPytd(ty, """
505505
from typing import List, Tuple
506-
lst = ... # type: List[Tuple[str, int]]
506+
lst : List[Tuple[str, int]]
507507
""")
508508

509509
def test_int_init(self):
@@ -540,11 +540,11 @@ def test_create_str(self):
540540

541541
def test_bytes_constant(self):
542542
ty = self.Infer("v = b'foo'")
543-
self.assertTypesMatchPytd(ty, "v = ... # type: bytes")
543+
self.assertTypesMatchPytd(ty, "v : bytes")
544544

545545
def test_unicode_constant(self):
546546
ty = self.Infer("v = 'foo\\u00e4'")
547-
self.assertTypesMatchPytd(ty, "v = ... # type: str")
547+
self.assertTypesMatchPytd(ty, "v : str")
548548

549549
def test_memoryview(self):
550550
self.Check("""
@@ -574,10 +574,21 @@ def test_memoryview_methods(self):
574574
""")
575575
self.assertTypesMatchPytd(ty, """
576576
from typing import List
577-
v1 = ... # type: memoryview
578-
v2 = ... # type: bytes
579-
v3 = ... # type: List[int]
580-
v4 = ... # type: str
577+
v1: memoryview
578+
v2: bytes
579+
v3: List[int]
580+
v4: str
581+
""")
582+
583+
@test_utils.skipBeforePy((3, 8), "new hex signature added in 3.8.")
584+
def test_bytes_hex(self):
585+
self.Check("""
586+
b = b'abc'
587+
b.hex(",", 3)
588+
m = memoryview(b)
589+
m.hex(",", 4)
590+
ba = bytearray([1,2,3])
591+
ba.hex(b",", 5)
581592
""")
582593

583594
def test_memoryview_contextmanager(self):
@@ -586,7 +597,7 @@ def test_memoryview_contextmanager(self):
586597
pass
587598
""")
588599
self.assertTypesMatchPytd(ty, """
589-
v = ... # type: memoryview
600+
v : memoryview
590601
""")
591602

592603
def test_array_tobytes(self):
@@ -607,8 +618,8 @@ def test_iterator_builtins(self):
607618
""")
608619
self.assertTypesMatchPytd(ty, """
609620
from typing import Iterator, Tuple
610-
v1 = ... # type: Iterator[int]
611-
v2 = ... # type: Iterator[Tuple[int, int]]
621+
v1 : Iterator[int]
622+
v2 : Iterator[Tuple[int, int]]
612623
""")
613624

614625
def test_next(self):
@@ -618,9 +629,9 @@ def test_next(self):
618629
v2 = next(itr)
619630
""")
620631
self.assertTypesMatchPytd(ty, """
621-
itr = ... # type: tupleiterator[int]
622-
v1 = ... # type: int
623-
v2 = ... # type: int
632+
itr : tupleiterator[int]
633+
v1 : int
634+
v2 : int
624635
""")
625636

626637
def test_aliased_error(self):
@@ -703,10 +714,10 @@ def test_min_max(self):
703714
# type an optional argument as a typevar.
704715
self.assertTypesMatchPytd(ty, """
705716
from typing import Any
706-
x1 = ... # type: int
707-
x2 = ... # type: Any
708-
y1 = ... # type: int
709-
y2 = ... # type: Any
717+
x1 : int
718+
x2 : Any
719+
y1 : int
720+
y2 : Any
710721
""")
711722

712723
def test_str_is_not_int(self):

0 commit comments

Comments
 (0)