33from collections .abc import Sequence
44from typing import TYPE_CHECKING , Final
55
6+ from mypy_extensions import u8
7+
68try :
79 from native_internal import (
810 Buffer as Buffer ,
911 read_bool as read_bool ,
1012 read_float as read_float ,
1113 read_int as read_int ,
1214 read_str as read_str ,
15+ read_tag as read_tag ,
1316 write_bool as write_bool ,
1417 write_float as write_float ,
1518 write_int as write_int ,
1619 write_str as write_str ,
20+ write_tag as write_tag ,
1721 )
1822except ImportError :
1923 # TODO: temporary, remove this after we publish mypy-native on PyPI.
@@ -32,6 +36,12 @@ def read_int(data: Buffer) -> int:
3236 def write_int (data : Buffer , value : int ) -> None :
3337 raise NotImplementedError
3438
39+ def read_tag (data : Buffer ) -> u8 :
40+ raise NotImplementedError
41+
42+ def write_tag (data : Buffer , value : u8 ) -> None :
43+ raise NotImplementedError
44+
3545 def read_str (data : Buffer ) -> str :
3646 raise NotImplementedError
3747
@@ -51,45 +61,48 @@ def write_float(data: Buffer, value: float) -> None:
5161 raise NotImplementedError
5262
5363
54- LITERAL_INT : Final = 1
55- LITERAL_STR : Final = 2
56- LITERAL_BOOL : Final = 3
57- LITERAL_FLOAT : Final = 4
58- LITERAL_COMPLEX : Final = 5
59- LITERAL_NONE : Final = 6
64+ # Always use this type alias to refer to type tags.
65+ Tag = u8
66+
67+ LITERAL_INT : Final [Tag ] = 1
68+ LITERAL_STR : Final [Tag ] = 2
69+ LITERAL_BOOL : Final [Tag ] = 3
70+ LITERAL_FLOAT : Final [Tag ] = 4
71+ LITERAL_COMPLEX : Final [Tag ] = 5
72+ LITERAL_NONE : Final [Tag ] = 6
6073
6174
62- def read_literal (data : Buffer , marker : int ) -> int | str | bool | float :
63- if marker == LITERAL_INT :
75+ def read_literal (data : Buffer , tag : Tag ) -> int | str | bool | float :
76+ if tag == LITERAL_INT :
6477 return read_int (data )
65- elif marker == LITERAL_STR :
78+ elif tag == LITERAL_STR :
6679 return read_str (data )
67- elif marker == LITERAL_BOOL :
80+ elif tag == LITERAL_BOOL :
6881 return read_bool (data )
69- elif marker == LITERAL_FLOAT :
82+ elif tag == LITERAL_FLOAT :
7083 return read_float (data )
71- assert False , f"Unknown literal marker { marker } "
84+ assert False , f"Unknown literal tag { tag } "
7285
7386
7487def write_literal (data : Buffer , value : int | str | bool | float | complex | None ) -> None :
7588 if isinstance (value , bool ):
76- write_int (data , LITERAL_BOOL )
89+ write_tag (data , LITERAL_BOOL )
7790 write_bool (data , value )
7891 elif isinstance (value , int ):
79- write_int (data , LITERAL_INT )
92+ write_tag (data , LITERAL_INT )
8093 write_int (data , value )
8194 elif isinstance (value , str ):
82- write_int (data , LITERAL_STR )
95+ write_tag (data , LITERAL_STR )
8396 write_str (data , value )
8497 elif isinstance (value , float ):
85- write_int (data , LITERAL_FLOAT )
98+ write_tag (data , LITERAL_FLOAT )
8699 write_float (data , value )
87100 elif isinstance (value , complex ):
88- write_int (data , LITERAL_COMPLEX )
101+ write_tag (data , LITERAL_COMPLEX )
89102 write_float (data , value .real )
90103 write_float (data , value .imag )
91104 else :
92- write_int (data , LITERAL_NONE )
105+ write_tag (data , LITERAL_NONE )
93106
94107
95108def read_int_opt (data : Buffer ) -> int | None :
0 commit comments