Skip to content

Commit

Permalink
Add mypyc native int types i64, i32, i16 and u8 (#31)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JukkaL committed Jan 30, 2023
1 parent 0a0adae commit 53c8777
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 4 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Mypy Extensions
===============

The "mypy_extensions" module defines experimental extensions to the
standard "typing" module that are supported by the mypy typechecker.

The `mypy_extensions` module defines extensions to the Python standard
library `typing` module that are supported by the mypy type checker and
the mypyc compiler.
49 changes: 49 additions & 0 deletions mypy_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,52 @@ def __getitem__(self, args):


FlexibleAlias = _FlexibleAliasCls()


class _NativeIntMeta(type):
def __instancecheck__(cls, inst):
return isinstance(inst, int)


_sentinel = object()


class i64(metaclass=_NativeIntMeta):
def __new__(cls, x=0, base=_sentinel):
if base is not _sentinel:
return int(x, base)
return int(x)


class i32(metaclass=_NativeIntMeta):
def __new__(cls, x=0, base=_sentinel):
if base is not _sentinel:
return int(x, base)
return int(x)


class i16(metaclass=_NativeIntMeta):
def __new__(cls, x=0, base=_sentinel):
if base is not _sentinel:
return int(x, base)
return int(x)


class u8(metaclass=_NativeIntMeta):
def __new__(cls, x=0, base=_sentinel):
if base is not _sentinel:
return int(x, base)
return int(x)


for _int_type in i64, i32, i16, u8:
_int_type.__doc__ = \
"""A native fixed-width integer type when used with mypyc.
In code not compiled with mypyc, behaves like the 'int' type in these
runtime contexts:
* {name}(x[, base=n]) converts a number or string to 'int'
* isinstance(x, {name}) is the same as isinstance(x, int)
""".format(name=_int_type.__name__)
del _int_type
43 changes: 42 additions & 1 deletion tests/testextensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pickle
import typing
from unittest import TestCase, main, skipUnless
from mypy_extensions import TypedDict
from mypy_extensions import TypedDict, i64, i32, i16, u8


class BaseTestCase(TestCase):
Expand Down Expand Up @@ -140,5 +140,46 @@ def test_total(self):
self.assertEqual(Options.__total__, False) # noqa


native_int_types = [i64, i32, i16, u8]


class MypycNativeIntTests(TestCase):
def test_construction(self):
for native_int in native_int_types:
self.assert_same(native_int(), 0)

self.assert_same(native_int(0), 0)
self.assert_same(native_int(1), 1)
self.assert_same(native_int(-3), -3)
self.assert_same(native_int(2**64), 2**64)
self.assert_same(native_int(-2**64), -2**64)

self.assert_same(native_int(1.234), 1)
self.assert_same(native_int(2.634), 2)
self.assert_same(native_int(-1.234), -1)
self.assert_same(native_int(-2.634), -2)

self.assert_same(native_int("0"), 0)
self.assert_same(native_int("123"), 123)
self.assert_same(native_int("abc", 16), 2748)
self.assert_same(native_int("-101", base=2), -5)

def test_isinstance(self):
for native_int in native_int_types:
assert isinstance(0, native_int)
assert isinstance(1234, native_int)
assert isinstance(True, native_int)
assert not isinstance(1.0, native_int)

def test_docstring(self):
for native_int in native_int_types:
# Just check that a docstring exists
assert native_int.__doc__

def assert_same(self, x, y):
assert type(x) is type(y)
assert x == y


if __name__ == '__main__':
main()

0 comments on commit 53c8777

Please sign in to comment.