Skip to content

Commit 53c8777

Browse files
authored
Add mypyc native int types i64, i32, i16 and u8 (#31)
In code compiled with mypyc, these can be used in annotations to use faster native int operations that don't check for overflow, as an alternative to the default arbitrary-precision int type. See mypyc/mypyc/issues/837 for more context. Note that only i64 and i32 are currently supported by mypyc, but I'm adding the planned i16 and u8 types as well since their implementation is essentially the same. These are not real classes. In particular, there can be no instances of these types. In code that is not compiled with mypyc, there are just regular 'int' objects, in order to allow code using these types to be run without compilation. In code compiled with mypyc, these are represented as native integers that don't have a 1:1 Python replacement. The native integers are impliciticly converted to/from 'int' objects when boxed/unboxed. I originally was planning to make these aliases of `int`, but there are runtime type checking and introspection use cases where it's important to make these distinct objects. The types only support a few runtime operations: * Conversions from numbers and strings * `isinstance` checks We could also add at least the `from_bytes` class method, but it doesn't seem urgent as long as mypyc doesn't support it as a primitive operation.
1 parent 0a0adae commit 53c8777

File tree

3 files changed

+94
-4
lines changed

3 files changed

+94
-4
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Mypy Extensions
22
===============
33

4-
The "mypy_extensions" module defines experimental extensions to the
5-
standard "typing" module that are supported by the mypy typechecker.
6-
4+
The `mypy_extensions` module defines extensions to the Python standard
5+
library `typing` module that are supported by the mypy type checker and
6+
the mypyc compiler.

mypy_extensions.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,52 @@ def __getitem__(self, args):
162162

163163

164164
FlexibleAlias = _FlexibleAliasCls()
165+
166+
167+
class _NativeIntMeta(type):
168+
def __instancecheck__(cls, inst):
169+
return isinstance(inst, int)
170+
171+
172+
_sentinel = object()
173+
174+
175+
class i64(metaclass=_NativeIntMeta):
176+
def __new__(cls, x=0, base=_sentinel):
177+
if base is not _sentinel:
178+
return int(x, base)
179+
return int(x)
180+
181+
182+
class i32(metaclass=_NativeIntMeta):
183+
def __new__(cls, x=0, base=_sentinel):
184+
if base is not _sentinel:
185+
return int(x, base)
186+
return int(x)
187+
188+
189+
class i16(metaclass=_NativeIntMeta):
190+
def __new__(cls, x=0, base=_sentinel):
191+
if base is not _sentinel:
192+
return int(x, base)
193+
return int(x)
194+
195+
196+
class u8(metaclass=_NativeIntMeta):
197+
def __new__(cls, x=0, base=_sentinel):
198+
if base is not _sentinel:
199+
return int(x, base)
200+
return int(x)
201+
202+
203+
for _int_type in i64, i32, i16, u8:
204+
_int_type.__doc__ = \
205+
"""A native fixed-width integer type when used with mypyc.
206+
207+
In code not compiled with mypyc, behaves like the 'int' type in these
208+
runtime contexts:
209+
210+
* {name}(x[, base=n]) converts a number or string to 'int'
211+
* isinstance(x, {name}) is the same as isinstance(x, int)
212+
""".format(name=_int_type.__name__)
213+
del _int_type

tests/testextensions.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import pickle
33
import typing
44
from unittest import TestCase, main, skipUnless
5-
from mypy_extensions import TypedDict
5+
from mypy_extensions import TypedDict, i64, i32, i16, u8
66

77

88
class BaseTestCase(TestCase):
@@ -140,5 +140,46 @@ def test_total(self):
140140
self.assertEqual(Options.__total__, False) # noqa
141141

142142

143+
native_int_types = [i64, i32, i16, u8]
144+
145+
146+
class MypycNativeIntTests(TestCase):
147+
def test_construction(self):
148+
for native_int in native_int_types:
149+
self.assert_same(native_int(), 0)
150+
151+
self.assert_same(native_int(0), 0)
152+
self.assert_same(native_int(1), 1)
153+
self.assert_same(native_int(-3), -3)
154+
self.assert_same(native_int(2**64), 2**64)
155+
self.assert_same(native_int(-2**64), -2**64)
156+
157+
self.assert_same(native_int(1.234), 1)
158+
self.assert_same(native_int(2.634), 2)
159+
self.assert_same(native_int(-1.234), -1)
160+
self.assert_same(native_int(-2.634), -2)
161+
162+
self.assert_same(native_int("0"), 0)
163+
self.assert_same(native_int("123"), 123)
164+
self.assert_same(native_int("abc", 16), 2748)
165+
self.assert_same(native_int("-101", base=2), -5)
166+
167+
def test_isinstance(self):
168+
for native_int in native_int_types:
169+
assert isinstance(0, native_int)
170+
assert isinstance(1234, native_int)
171+
assert isinstance(True, native_int)
172+
assert not isinstance(1.0, native_int)
173+
174+
def test_docstring(self):
175+
for native_int in native_int_types:
176+
# Just check that a docstring exists
177+
assert native_int.__doc__
178+
179+
def assert_same(self, x, y):
180+
assert type(x) is type(y)
181+
assert x == y
182+
183+
143184
if __name__ == '__main__':
144185
main()

0 commit comments

Comments
 (0)