From 084d5bd40191aa6a93f092e1d2fb50b5b319ac1c Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Sat, 19 Sep 2026 12:35:52 +0100 Subject: [PATCH] [mypyc] Support the u16 native integer type u16 is an unsigned 16-bit integer, similar to the existing u8 type but wider. Operations wrap around on overflow. Values are represented as uint16_t in C, with 239 as the overlapping error value. The internal uint16 rprimitive already existed; this promotes it to a user-visible type (mypy_extensions.u16) with unboxing/boxing (CPyLong_AsUInt16), overflow errors, conversions to/from other native ints, and docs/tests similar to u8. --- mypy/types.py | 1 + .../stubs/mypy-extensions/mypy_extensions.pyi | 37 +++ mypyc/codegen/emit.py | 16 +- mypyc/codegen/emitfunc.py | 12 + mypyc/doc/float_operations.rst | 2 + mypyc/doc/int_operations.rst | 33 +- mypyc/doc/using_type_annotations.rst | 6 +- mypyc/ir/rtypes.py | 29 +- mypyc/irbuild/ll_builder.py | 11 + mypyc/irbuild/mapper.py | 3 + mypyc/irbuild/specialize.py | 40 ++- mypyc/lib-rt/CPy.h | 32 ++ mypyc/lib-rt/int_ops.c | 23 ++ mypyc/primitives/int_ops.py | 8 + mypyc/test-data/irbuild-u16.test | 255 +++++++++++++++ mypyc/test-data/run-u16.test | 306 ++++++++++++++++++ mypyc/test/test_emitfunc.py | 33 ++ mypyc/test/test_irbuild.py | 1 + mypyc/test/test_run.py | 1 + mypyc/test/test_tuplename.py | 8 + mypyc/test/test_typeops.py | 28 +- test-data/unit/lib-stub/mypy_extensions.pyi | 32 +- 22 files changed, 898 insertions(+), 19 deletions(-) create mode 100644 mypyc/test-data/irbuild-u16.test create mode 100644 mypyc/test-data/run-u16.test diff --git a/mypy/types.py b/mypy/types.py index a6df7bf2e4521..2c37fd9ee3f88 100644 --- a/mypy/types.py +++ b/mypy/types.py @@ -203,6 +203,7 @@ class SentinelValue(NamedTuple): "mypy_extensions.i64", "mypy_extensions.i32", "mypy_extensions.i16", + "mypy_extensions.u16", "mypy_extensions.u8", ) diff --git a/mypy/typeshed/stubs/mypy-extensions/mypy_extensions.pyi b/mypy/typeshed/stubs/mypy-extensions/mypy_extensions.pyi index 2446244df46f3..9b01d2900e5a8 100644 --- a/mypy/typeshed/stubs/mypy-extensions/mypy_extensions.pyi +++ b/mypy/typeshed/stubs/mypy-extensions/mypy_extensions.pyi @@ -188,6 +188,43 @@ class i16: def __eq__(self, x: object) -> bool: ... def __bool__(self) -> bool: ... +class u16: + @overload + def __new__(cls, __x: str | ReadableBuffer | SupportsInt | SupportsIndex | SupportsTrunc = ...) -> u16: ... + @overload + def __new__(cls, __x: str | bytes | bytearray, base: SupportsIndex) -> u16: ... + + def __add__(self, x: u16) -> u16: ... + def __radd__(self, x: u16) -> u16: ... + def __sub__(self, x: u16) -> u16: ... + def __rsub__(self, x: u16) -> u16: ... + def __mul__(self, x: u16) -> u16: ... + def __rmul__(self, x: u16) -> u16: ... + def __floordiv__(self, x: u16) -> u16: ... + def __rfloordiv__(self, x: u16) -> u16: ... + def __mod__(self, x: u16) -> u16: ... + def __rmod__(self, x: u16) -> u16: ... + def __and__(self, x: u16) -> u16: ... + def __rand__(self, x: u16) -> u16: ... + def __or__(self, x: u16) -> u16: ... + def __ror__(self, x: u16) -> u16: ... + def __xor__(self, x: u16) -> u16: ... + def __rxor__(self, x: u16) -> u16: ... + def __lshift__(self, x: u16) -> u16: ... + def __rlshift__(self, x: u16) -> u16: ... + def __rshift__(self, x: u16) -> u16: ... + def __rrshift__(self, x: u16) -> u16: ... + def __neg__(self) -> u16: ... + def __invert__(self) -> u16: ... + def __pos__(self) -> u16: ... + def __lt__(self, x: u16) -> bool: ... + def __le__(self, x: u16) -> bool: ... + def __ge__(self, x: u16) -> bool: ... + def __gt__(self, x: u16) -> bool: ... + def __index__(self) -> int: ... + def __eq__(self, x: object) -> bool: ... + def __bool__(self) -> bool: ... + class u8: @overload def __new__(cls, __x: str | ReadableBuffer | SupportsInt | SupportsIndex | SupportsTrunc = ...) -> u8: ... diff --git a/mypyc/codegen/emit.py b/mypyc/codegen/emit.py index 459af4d0a49a4..c71c3ec04c2f8 100644 --- a/mypyc/codegen/emit.py +++ b/mypyc/codegen/emit.py @@ -65,6 +65,7 @@ is_str_rprimitive, is_tuple_rprimitive, is_uint8_rprimitive, + is_uint16_rprimitive, object_rprimitive, optional_value_type, vec_api_by_item_type, @@ -1166,6 +1167,14 @@ def emit_unbox( self.emit_line(f"{dest} = CPyLong_AsInt16({src});") if not isinstance(error, AssignHandler): self.emit_unbox_failure_with_overlapping_error_value(dest, typ, failure) + elif is_uint16_rprimitive(typ): + # Whether we are borrowing or not makes no difference. + assert not optional # Not supported for overlapping error values + if declare_dest: + self.emit_line(f"uint16_t {dest};") + self.emit_line(f"{dest} = CPyLong_AsUInt16({src});") + if not isinstance(error, AssignHandler): + self.emit_unbox_failure_with_overlapping_error_value(dest, typ, failure) elif is_uint8_rprimitive(typ): # Whether we are borrowing or not makes no difference. assert not optional # Not supported for overlapping error values @@ -1329,7 +1338,12 @@ def emit_box( self.emit_lines(f"{declaration}{dest} = Py_None;") if not can_borrow: self.emit_inc_ref(dest, object_rprimitive) - elif is_int32_rprimitive(typ) or is_int16_rprimitive(typ) or is_uint8_rprimitive(typ): + elif ( + is_int32_rprimitive(typ) + or is_int16_rprimitive(typ) + or is_uint16_rprimitive(typ) + or is_uint8_rprimitive(typ) + ): self.emit_line(f"{declaration}{dest} = PyLong_FromLong({src});") elif is_int64_rprimitive(typ): self.emit_line(f"{declaration}{dest} = PyLong_FromLongLong({src});") diff --git a/mypyc/codegen/emitfunc.py b/mypyc/codegen/emitfunc.py index 07412dfbf73c9..14be734866f8a 100644 --- a/mypyc/codegen/emitfunc.py +++ b/mypyc/codegen/emitfunc.py @@ -79,6 +79,7 @@ from mypyc.ir.rtypes import ( RArray, RInstance, + RPrimitive, RStruct, RTuple, RType, @@ -940,6 +941,17 @@ def visit_int_op(self, op: IntOp) -> None: # Signed right shift lhs = self.emit_signed_int_cast(op.lhs.type) + lhs rhs = self.emit_signed_int_cast(op.rhs.type) + rhs + elif ( + op.op in (IntOp.MUL, IntOp.LEFT_SHIFT) + and isinstance(op.type, RPrimitive) + and op.type.is_native_int + and not op.type.is_signed + and op.type.size < 4 + ): + # C promotes unsigned types narrower than int to (signed) int, so + # e.g. u16 * u16 could overflow int, which is undefined behavior. + # Do the arithmetic using unsigned int to get predictable wraparound. + lhs = f"(unsigned int){lhs}" self.emit_line(f"{dest} = {lhs} {op.op_str[op.op]} {rhs};") def visit_comparison_op(self, op: ComparisonOp) -> None: diff --git a/mypyc/doc/float_operations.rst b/mypyc/doc/float_operations.rst index 0f75b6ddd0dd0..64cba65c908e0 100644 --- a/mypyc/doc/float_operations.rst +++ b/mypyc/doc/float_operations.rst @@ -15,6 +15,7 @@ Construction * ``float(x: i64)`` * ``float(x: i32)`` * ``float(x: i16)`` +* ``float(x: u16)`` * ``float(x: u8)`` * ``float(x: str)`` * ``float(x: float)`` (no-op) @@ -42,6 +43,7 @@ Functions * ``i64(f)`` (convert to 64-bit signed integer) * ``i32(f)`` (convert to 32-bit signed integer) * ``i16(f)`` (convert to 16-bit signed integer) +* ``u16(f)`` (convert to 16-bit unsigned integer) * ``u8(f)`` (convert to 8-bit unsigned integer) * ``abs(f)`` * ``math.sin(f)`` diff --git a/mypyc/doc/int_operations.rst b/mypyc/doc/int_operations.rst index ad67ea908cd1f..21427ef9b75bb 100644 --- a/mypyc/doc/int_operations.rst +++ b/mypyc/doc/int_operations.rst @@ -9,9 +9,10 @@ Mypyc supports these integer types: * ``i64`` (64-bit signed integer) * ``i32`` (32-bit signed integer) * ``i16`` (16-bit signed integer) +* ``u16`` (16-bit unsigned integer) * ``u8`` (8-bit unsigned integer) -``i64``, ``i32``, ``i16`` and ``u8`` are *native integer types* and +``i64``, ``i32``, ``i16``, ``u16`` and ``u8`` are *native integer types* and are available in the ``mypy_extensions`` module. ``int`` corresponds to the Python ``int`` type, but uses a more efficient runtime representation (tagged pointer). Native integer types are value types. @@ -35,6 +36,7 @@ Construction * ``int(x: i64)`` * ``int(x: i32)`` * ``int(x: i16)`` +* ``int(x: u16)`` * ``int(x: u8)`` * ``int(x: str)`` * ``int(x: str, base: int)`` @@ -47,6 +49,7 @@ Construction * ``i64(x: i64)`` (no-op) * ``i64(x: i32)`` * ``i64(x: i16)`` +* ``i64(x: u16)`` * ``i64(x: u8)`` * ``i64(x: str)`` * ``i64(x: str, base: int)`` @@ -58,6 +61,7 @@ Construction * ``i32(x: i64)`` (truncate) * ``i32(x: i32)`` (no-op) * ``i32(x: i16)`` +* ``i32(x: u16)`` * ``i32(x: u8)`` * ``i32(x: str)`` * ``i32(x: str, base: int)`` @@ -69,10 +73,35 @@ Construction * ``i16(x: i64)`` (truncate) * ``i16(x: i32)`` (truncate) * ``i16(x: i16)`` (no-op) +* ``i16(x: u16)`` (truncate) * ``i16(x: u8)`` * ``i16(x: str)`` * ``i16(x: str, base: int)`` +``u16`` type: + +* ``u16(x: int)`` +* ``u16(x: float)`` +* ``u16(x: i64)`` (truncate) +* ``u16(x: i32)`` (truncate) +* ``u16(x: i16)`` (truncate) +* ``u16(x: u16)`` (no-op) +* ``u16(x: u8)`` +* ``u16(x: str)`` +* ``u16(x: str, base: int)`` + +``u8`` type: + +* ``u8(x: int)`` +* ``u8(x: float)`` +* ``u8(x: i64)`` (truncate) +* ``u8(x: i32)`` (truncate) +* ``u8(x: i16)`` (truncate) +* ``u8(x: u16)`` (truncate) +* ``u8(x: u8)`` (no-op) +* ``u8(x: str)`` +* ``u8(x: str, base: int)`` + Conversions from ``int`` to a native integer type raise ``OverflowError`` if the value is too large or small. Conversions from a wider native integer type to a narrower one truncate the value and never @@ -121,7 +150,7 @@ the type. For this reason, the arbitrary-precision ``int`` type is recommended for signed values unless the performance of integer operations is critical. -Operations on unsigned integers (``u8``) wrap around on overflow. +Operations on unsigned integers (``u16`` and ``u8``) wrap around on overflow. It's a compile-time error to mix different native integer types in a binary operation such as addition. An explicit conversion is required:: diff --git a/mypyc/doc/using_type_annotations.rst b/mypyc/doc/using_type_annotations.rst index 4ecc77ab66695..4394e94942094 100644 --- a/mypyc/doc/using_type_annotations.rst +++ b/mypyc/doc/using_type_annotations.rst @@ -33,6 +33,7 @@ implementations: * ``i64`` (:ref:`documentation `, :ref:`native operations `) * ``i32`` (:ref:`documentation `, :ref:`native operations `) * ``i16`` (:ref:`documentation `, :ref:`native operations `) +* ``u16`` (:ref:`documentation `, :ref:`native operations `) * ``u8`` (:ref:`documentation `, :ref:`native operations `) * ``float`` (:ref:`native operations `) * ``bool`` (:ref:`native operations `) @@ -348,8 +349,9 @@ Native integer types -------------------- You can use the native integer types ``i64`` (64-bit signed integer), -``i32`` (32-bit signed integer), ``i16`` (16-bit signed integer), and -``u8`` (8-bit unsigned integer) if you know that integer values will +``i32`` (32-bit signed integer), ``i16`` (16-bit signed integer), +``u16`` (16-bit unsigned integer), and ``u8`` (8-bit unsigned integer) +if you know that integer values will always fit within fixed bounds. These types are faster than the arbitrary-precision ``int`` type, since they don't require overflow checks on operations. They may also use less memory than ``int`` diff --git a/mypyc/ir/rtypes.py b/mypyc/ir/rtypes.py index 84b260d33d82c..5b43a5c30a853 100644 --- a/mypyc/ir/rtypes.py +++ b/mypyc/ir/rtypes.py @@ -400,6 +400,16 @@ def __hash__(self) -> int: size=8, error_overlap=True, ) +uint16_rprimitive: Final = RPrimitive( + "u16", + is_unboxed=True, + is_refcounted=False, + is_native_int=True, + is_signed=False, + ctype="uint16_t", + size=2, + error_overlap=True, +) uint8_rprimitive: Final = RPrimitive( "u8", is_unboxed=True, @@ -411,19 +421,9 @@ def __hash__(self) -> int: error_overlap=True, ) -# The following unsigned native int types (u16, u32, u64) are not +# The following unsigned native int types (u32, u64) are not # exposed to the user. They are for internal use within mypyc only. -u16_rprimitive: Final = RPrimitive( - "u16", - is_unboxed=True, - is_refcounted=False, - is_native_int=True, - is_signed=False, - ctype="uint16_t", - size=2, - error_overlap=True, -) uint32_rprimitive: Final = RPrimitive( "u32", is_unboxed=True, @@ -633,10 +633,15 @@ def is_fixed_width_rtype(rtype: RType) -> TypeGuard[RPrimitive]: is_int64_rprimitive(rtype) or is_int32_rprimitive(rtype) or is_int16_rprimitive(rtype) + or is_uint16_rprimitive(rtype) or is_uint8_rprimitive(rtype) ) +def is_uint16_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]: + return rtype is uint16_rprimitive + + def is_uint8_rprimitive(rtype: RType) -> TypeGuard[RPrimitive]: return rtype is uint8_rprimitive @@ -762,6 +767,8 @@ def visit_rprimitive(self, t: RPrimitive) -> str: return "4" # "4 byte integer" elif t._ctype == "int16_t": return "2" # "2 byte integer" + elif t._ctype == "uint16_t": + return "U2" # "2 byte unsigned integer" elif t._ctype == "uint8_t": return "U1" # "1 byte unsigned integer" elif t._ctype == "double": diff --git a/mypyc/irbuild/ll_builder.py b/mypyc/irbuild/ll_builder.py index a4b0a4cb2eca4..c4dde4667228a 100644 --- a/mypyc/irbuild/ll_builder.py +++ b/mypyc/irbuild/ll_builder.py @@ -122,6 +122,7 @@ is_tagged, is_tuple_rprimitive, is_uint8_rprimitive, + is_uint16_rprimitive, none_rprimitive, object_pointer_rprimitive, object_rprimitive, @@ -171,6 +172,7 @@ int_to_int64_op, ssize_t_to_int_op, uint8_overflow, + uint16_overflow, ) from mypyc.primitives.list_ops import list_build_op, list_extend_op, list_items, new_list_op from mypyc.primitives.misc_ops import ( @@ -721,6 +723,8 @@ def emit_fixed_width_overflow_error(self, target_type: RType, line: int) -> None self.call_c(int32_overflow, [], line) elif is_int16_rprimitive(target_type): self.call_c(int16_overflow, [], line) + elif is_uint16_rprimitive(target_type): + self.call_c(uint16_overflow, [], line) elif is_uint8_rprimitive(target_type): self.call_c(uint8_overflow, [], line) else: @@ -758,6 +762,7 @@ def coerce_fixed_width_to_int(self, src: Value, line: int) -> Value: if ( (is_int32_rprimitive(src.type) and PLATFORM_SIZE == 8) or is_int16_rprimitive(src.type) + or is_uint16_rprimitive(src.type) or is_uint8_rprimitive(src.type) ): # Simple case -- just sign extend and shift. @@ -2726,6 +2731,9 @@ def fixed_width_int_op( prim = int32_divide_op elif is_int16_rprimitive(type): prim = int16_divide_op + elif is_uint16_rprimitive(type): + self.check_for_zero_division(rhs, type, line) + return self.int_op(type, lhs, rhs, op, line) elif is_uint8_rprimitive(type): self.check_for_zero_division(rhs, type, line) return self.int_op(type, lhs, rhs, op, line) @@ -2746,6 +2754,9 @@ def fixed_width_int_op( prim = int32_mod_op elif is_int16_rprimitive(type): prim = int16_mod_op + elif is_uint16_rprimitive(type): + self.check_for_zero_division(rhs, type, line) + return self.int_op(type, lhs, rhs, op, line) elif is_uint8_rprimitive(type): self.check_for_zero_division(rhs, type, line) return self.int_op(type, lhs, rhs, op, line) diff --git a/mypyc/irbuild/mapper.py b/mypyc/irbuild/mapper.py index ac472117c08cb..670ab80e379d6 100644 --- a/mypyc/irbuild/mapper.py +++ b/mypyc/irbuild/mapper.py @@ -50,6 +50,7 @@ str_rprimitive, tuple_rprimitive, uint8_rprimitive, + uint16_rprimitive, ) @@ -123,6 +124,8 @@ def type_to_rtype(self, typ: Type | None) -> RType: return int32_rprimitive elif typ.type.fullname == "mypy_extensions.i16": return int16_rprimitive + elif typ.type.fullname == "mypy_extensions.u16": + return uint16_rprimitive elif typ.type.fullname == "mypy_extensions.u8": return uint8_rprimitive elif typ.type.fullname == "librt.vecs.vec": diff --git a/mypyc/irbuild/specialize.py b/mypyc/irbuild/specialize.py index 6fd60323852df..1f4cb8bf3f40c 100644 --- a/mypyc/irbuild/specialize.py +++ b/mypyc/irbuild/specialize.py @@ -83,12 +83,14 @@ is_str_rprimitive, is_tagged, is_uint8_rprimitive, + is_uint16_rprimitive, list_rprimitive, object_rprimitive, set_rprimitive, str_rprimitive, string_writer_rprimitive, uint8_rprimitive, + uint16_rprimitive, ) from mypyc.irbuild.builder import IRBuilder, find_walrus_targets, get_call_target_fullname from mypyc.irbuild.constant_fold import constant_fold_expr @@ -319,6 +321,7 @@ def translate_globals(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Va @specialize_function("mypy_extensions.i64") @specialize_function("mypy_extensions.i32") @specialize_function("mypy_extensions.i16") +@specialize_function("mypy_extensions.u16") @specialize_function("mypy_extensions.u8") def translate_builtins_with_unary_dunder( builder: IRBuilder, expr: CallExpr, callee: RefExpr @@ -331,7 +334,7 @@ def translate_builtins_with_unary_dunder( arg = expr.args[0] arg_typ = builder.node_type(arg) shortname = callee.fullname.split(".")[1] - if shortname in ("i64", "i32", "i16", "u8"): + if shortname in ("i64", "i32", "i16", "u16", "u8"): method = "__int__" else: method = f"__{shortname}__" @@ -1175,6 +1178,9 @@ def translate_i64(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value elif is_int32_rprimitive(arg_type) or is_int16_rprimitive(arg_type): val = builder.accept(arg) return builder.add(Extend(val, int64_rprimitive, signed=True, line=expr.line)) + elif is_uint16_rprimitive(arg_type): + val = builder.accept(arg) + return builder.add(Extend(val, int64_rprimitive, signed=False, line=expr.line)) elif is_uint8_rprimitive(arg_type): val = builder.accept(arg) return builder.add(Extend(val, int64_rprimitive, signed=False, line=expr.line)) @@ -1198,6 +1204,9 @@ def translate_i32(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value elif is_int16_rprimitive(arg_type): val = builder.accept(arg) return builder.add(Extend(val, int32_rprimitive, signed=True, line=expr.line)) + elif is_uint16_rprimitive(arg_type): + val = builder.accept(arg) + return builder.add(Extend(val, int32_rprimitive, signed=False, line=expr.line)) elif is_uint8_rprimitive(arg_type): val = builder.accept(arg) return builder.add(Extend(val, int32_rprimitive, signed=False, line=expr.line)) @@ -1219,6 +1228,9 @@ def translate_i16(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value elif is_int32_rprimitive(arg_type) or is_int64_rprimitive(arg_type): val = builder.accept(arg) return builder.add(Truncate(val, int16_rprimitive, line=expr.line)) + elif is_uint16_rprimitive(arg_type): + val = builder.accept(arg) + return builder.add(Truncate(val, int16_rprimitive, line=expr.line)) elif is_uint8_rprimitive(arg_type): val = builder.accept(arg) return builder.add(Extend(val, int16_rprimitive, signed=False, line=expr.line)) @@ -1229,6 +1241,31 @@ def translate_i16(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value return None +@specialize_function("mypy_extensions.u16") +def translate_u16(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value | None: + if len(expr.args) != 1 or expr.arg_kinds[0] != ARG_POS: + return None + arg = expr.args[0] + arg_type = builder.node_type(arg) + if is_uint16_rprimitive(arg_type): + return builder.accept(arg) + elif ( + is_int16_rprimitive(arg_type) + or is_int32_rprimitive(arg_type) + or is_int64_rprimitive(arg_type) + or is_uint8_rprimitive(arg_type) + ): + val = builder.accept(arg) + if is_uint8_rprimitive(arg_type): + return builder.add(Extend(val, uint16_rprimitive, signed=False, line=expr.line)) + return builder.add(Truncate(val, uint16_rprimitive, line=expr.line)) + elif is_int_rprimitive(arg_type) or is_bool_rprimitive(arg_type): + val = builder.accept(arg) + val = truncate_literal(val, uint16_rprimitive) + return builder.coerce(val, uint16_rprimitive, expr.line) + return None + + @specialize_function("mypy_extensions.u8") def translate_u8(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value | None: if len(expr.args) != 1 or expr.arg_kinds[0] != ARG_POS: @@ -1241,6 +1278,7 @@ def translate_u8(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value | is_int16_rprimitive(arg_type) or is_int32_rprimitive(arg_type) or is_int64_rprimitive(arg_type) + or is_uint16_rprimitive(arg_type) ): val = builder.accept(arg) return builder.add(Truncate(val, uint8_rprimitive, line=expr.line)) diff --git a/mypyc/lib-rt/CPy.h b/mypyc/lib-rt/CPy.h index 424db8faa454d..60b90e5c1ca7a 100644 --- a/mypyc/lib-rt/CPy.h +++ b/mypyc/lib-rt/CPy.h @@ -217,6 +217,8 @@ int16_t CPyLong_AsInt16_(PyObject *o); int16_t CPyInt16_Divide(int16_t x, int16_t y); int16_t CPyInt16_Remainder(int16_t x, int16_t y); void CPyInt16_Overflow(void); +uint16_t CPyLong_AsUInt16_(PyObject *o); +void CPyUInt16_Overflow(void); uint8_t CPyLong_AsUInt8_(PyObject *o); void CPyUInt8_Overflow(void); double CPyTagged_TrueDivide(CPyTagged x, CPyTagged y); @@ -456,6 +458,36 @@ static inline int16_t CPyLong_AsInt16(PyObject *o) { return CPyLong_AsInt16_(o); } +static inline uint16_t CPyLong_AsUInt16(PyObject *o) { + if (likely(PyLong_Check(o))) { + #if CPY_3_12_FEATURES + PyLongObject *lobj = (PyLongObject *)o; + size_t tag = CPY_LONG_TAG(lobj); + if (likely(tag == (1 << CPY_NON_SIZE_BITS))) { + // Fast path + digit x = CPY_LONG_DIGIT(lobj, 0); + if (x < 65536) + return x; + } else if (likely(tag == CPY_SIGN_ZERO)) { + return 0; + } + #else + PyLongObject *lobj = (PyLongObject *)o; + Py_ssize_t size = lobj->ob_base.ob_size; + if (likely(size == 1)) { + // Fast path + digit x = lobj->ob_digit[0]; + if (x < 65536) + return x; + } else if (likely(size == 0)) { + return 0; + } + #endif + } + // Slow path + return CPyLong_AsUInt16_(o); +} + static inline uint8_t CPyLong_AsUInt8(PyObject *o) { if (likely(PyLong_Check(o))) { #if CPY_3_12_FEATURES diff --git a/mypyc/lib-rt/int_ops.c b/mypyc/lib-rt/int_ops.c index 11fc777b7f9a0..ce54a9a8be834 100644 --- a/mypyc/lib-rt/int_ops.c +++ b/mypyc/lib-rt/int_ops.c @@ -558,6 +558,29 @@ void CPyInt16_Overflow() { PyErr_SetString(PyExc_ValueError, "int too large to convert to i16"); } +// u16 unboxing slow path +uint16_t CPyLong_AsUInt16_(PyObject *o) { + int overflow; + long result = PyLong_AsLongAndOverflow(o, &overflow); + if (result < 0 || result >= 65536) { + overflow = 1; + result = -1; + } + if (result == -1) { + if (PyErr_Occurred()) { + return CPY_LL_UINT_ERROR; + } else if (overflow) { + PyErr_SetString(PyExc_ValueError, "int too large or small to convert to u16"); + return CPY_LL_UINT_ERROR; + } + } + return result; +} + +void CPyUInt16_Overflow() { + PyErr_SetString(PyExc_ValueError, "int too large or small to convert to u16"); +} + // u8 unboxing slow path uint8_t CPyLong_AsUInt8_(PyObject *o) { int overflow; diff --git a/mypyc/primitives/int_ops.py b/mypyc/primitives/int_ops.py index 11e86bea4f498..3929ed1702139 100644 --- a/mypyc/primitives/int_ops.py +++ b/mypyc/primitives/int_ops.py @@ -49,6 +49,7 @@ "mypy_extensions.i64", "mypy_extensions.i32", "mypy_extensions.i16", + "mypy_extensions.u16", "mypy_extensions.u8", ): # These int constructors produce object_rprimitives that then need to be unboxed @@ -306,6 +307,13 @@ def int_unary_op(name: str, c_function_name: str) -> PrimitiveDescription: error_kind=ERR_ALWAYS, ) +uint16_overflow = custom_op( + arg_types=[], + return_type=void_rtype, + c_function_name="CPyUInt16_Overflow", + error_kind=ERR_ALWAYS, +) + uint8_overflow = custom_op( arg_types=[], return_type=void_rtype, diff --git a/mypyc/test-data/irbuild-u16.test b/mypyc/test-data/irbuild-u16.test new file mode 100644 index 0000000000000..65248c2cef302 --- /dev/null +++ b/mypyc/test-data/irbuild-u16.test @@ -0,0 +1,255 @@ +# Test cases for u16 native ints. Focus on things that are different from u8; no need to +# duplicate all u8 test cases here. + +[case testU16BinaryOp] +from mypy_extensions import u16 + +def add_op(x: u16, y: u16) -> u16: + x = y + x + y = x + 5 + y += x + y += 7 + x = 5 + y + return x +def compare(x: u16, y: u16) -> None: + a = x == y + b = x == 5 + c = x < y + d = x < 5 + e = 5 == x + f = 5 < x +[out] +def add_op(x, y): + x, y, r0, r1, r2, r3, r4 :: u16 +L0: + r0 = y + x + x = r0 + r1 = x + 5 + y = r1 + r2 = y + x + y = r2 + r3 = y + 7 + y = r3 + r4 = 5 + y + x = r4 + return x +def compare(x, y): + x, y :: u16 + r0 :: bit + a :: bool + r1 :: bit + b :: bool + r2 :: bit + c :: bool + r3 :: bit + d :: bool + r4 :: bit + e :: bool + r5 :: bit + f :: bool +L0: + r0 = x == y + a = r0 + r1 = x == 5 + b = r1 + r2 = x < y :: unsigned + c = r2 + r3 = x < 5 :: unsigned + d = r3 + r4 = 5 == x + e = r4 + r5 = 5 < x :: unsigned + f = r5 + return 1 + +[case testU16DetectMoreOutOfRangeLiterals] +from mypy_extensions import u16 + +def out_of_range() -> None: + a: u16 = 65536 + b: u16 = -1 + f(65536) + # The following are ok + c: u16 = 0 + d: u16 = 65535 + f(0) + f(65535) + +def f(x: u16) -> None: pass +[out] +main:4: error: Value 65536 is out of range for "u16" +main:5: error: Value -1 is out of range for "u16" +main:6: error: Value 65536 is out of range for "u16" + +[case testU16BoxAndUnbox] +from typing import Any +from mypy_extensions import u16 + +def f(x: Any) -> Any: + y: u16 = x + return y +[out] +def f(x): + x :: object + r0, y :: u16 + r1 :: object +L0: + r0 = unbox(u16, x) + y = r0 + r1 = box(u16, y) + return r1 + +[case testU16ConvertToInt] +from mypy_extensions import u16 + +def u16_to_int(a: u16) -> int: + return a +[out] +def u16_to_int(a): + a :: u16 + r0 :: native_int + r1 :: int +L0: + r0 = extend a: u16 to native_int + r1 = r0 << 1 + return r1 + +[case testU16ExplicitConversionFromNativeInt] +from mypy_extensions import i64, i32, i16, u8, u16 + +def from_u16(x: u16) -> u16: + return u16(x) + +def from_u8(x: u8) -> u16: + return u16(x) + +def from_i16(x: i16) -> u16: + return u16(x) + +def from_i32(x: i32) -> u16: + return u16(x) + +def from_i64(x: i64) -> u16: + return u16(x) +[out] +def from_u16(x): + x :: u16 +L0: + return x +def from_u8(x): + x :: u8 + r0 :: u16 +L0: + r0 = extend x: u8 to u16 + return r0 +def from_i16(x): + x :: i16 + r0 :: u16 +L0: + r0 = truncate x: i16 to u16 + return r0 +def from_i32(x): + x :: i32 + r0 :: u16 +L0: + r0 = truncate x: i32 to u16 + return r0 +def from_i64(x): + x :: i64 + r0 :: u16 +L0: + r0 = truncate x: i64 to u16 + return r0 + +[case testU16ExplicitConversionToNativeInt] +from mypy_extensions import i64, i32, i16, u8, u16 + +def to_u8(x: u16) -> u8: + return u8(x) + +def to_i16(x: u16) -> i16: + return i16(x) + +def to_i32(x: u16) -> i32: + return i32(x) + +def to_i64(x: u16) -> i64: + return i64(x) +[out] +def to_u8(x): + x :: u16 + r0 :: u8 +L0: + r0 = truncate x: u16 to u8 + return r0 +def to_i16(x): + x :: u16 + r0 :: i16 +L0: + r0 = truncate x: u16 to i16 + return r0 +def to_i32(x): + x :: u16 + r0 :: i32 +L0: + r0 = extend x: u16 to i32 + return r0 +def to_i64(x): + x :: u16 + r0 :: i64 +L0: + r0 = extend x: u16 to i64 + return r0 + +[case testU16ExplicitConversionFromInt] +from mypy_extensions import u16 + +def f(x: int) -> u16: + return u16(x) +[out] +def f(x): + x :: int + r0 :: native_int + r1, r2, r3 :: bit + r4 :: native_int + r5, r6 :: u16 +L0: + r0 = x & 1 + r1 = r0 == 0 + if r1 goto L1 else goto L4 :: bool +L1: + r2 = x < 131072 :: signed + if r2 goto L2 else goto L4 :: bool +L2: + r3 = x >= 0 :: signed + if r3 goto L3 else goto L4 :: bool +L3: + r4 = x >> 1 + r5 = truncate r4: native_int to u16 + r6 = r5 + goto L5 +L4: + CPyUInt16_Overflow() + unreachable +L5: + return r6 + +[case testU16ExplicitConversionFromLiteral] +from mypy_extensions import u16 + +def f() -> None: + x = u16(0) + y = u16(11) + z = u16(-3) # Truncate + zz = u16(65538) # Truncate + a = u16(65535) +[out] +def f(): + x, y, z, zz, a :: u16 +L0: + x = 0 + y = 11 + z = 65533 + zz = 2 + a = 65535 + return 1 diff --git a/mypyc/test-data/run-u16.test b/mypyc/test-data/run-u16.test new file mode 100644 index 0000000000000..7278cd8d5b1a7 --- /dev/null +++ b/mypyc/test-data/run-u16.test @@ -0,0 +1,306 @@ +[case testU16BasicOps] +from typing import Any, Final, Tuple + +from mypy_extensions import u16, i16, i32, i64, u8 + +from testutil import assertRaises + +ERROR: Final = 239 + +def test_box_and_unbox() -> None: + for i in [0, 1, 255, 256, 1000, 32767, 65534, 65535]: + o: Any = i + x: u16 = o + o2: Any = x + assert o == o2 + assert x == i + with assertRaises(ValueError, "int too large or small to convert to u16"): + o = 65536 + x2: u16 = o + with assertRaises(ValueError, "int too large or small to convert to u16"): + o = -1 + x3: u16 = o + +def div_by_7(x: u16) -> u16: + return x // 7 + +def div(x: u16, y: u16) -> u16: + return x // y + +def test_divide_by_constant() -> None: + for i in [0, 1, 100, 1000, 65535]: + assert div_by_7(i) == i // 7 + +def test_divide_by_variable() -> None: + for x in [0, 1, 255, 256, 1000, 65535]: + for y in [0, 1, 7, 255, 256, 1000, 65535]: + if y != 0: + assert div(x, y) == x // y + else: + with assertRaises(ZeroDivisionError, "integer division or modulo by zero"): + div(x, y) + +def mod_by_7(x: u16) -> u16: + return x % 7 + +def mod(x: u16, y: u16) -> u16: + return x % y + +def test_mod_by_constant() -> None: + for i in [0, 1, 100, 1000, 65535]: + assert mod_by_7(i) == i % 7 + +def test_mod_by_variable() -> None: + for x in [0, 1, 255, 256, 1000, 65535]: + for y in [0, 1, 7, 255, 256, 1000, 65535]: + if y != 0: + assert mod(x, y) == x % y + else: + with assertRaises(ZeroDivisionError, "integer division or modulo by zero"): + mod(x, y) + +def test_simple_arithmetic_ops() -> None: + zero: u16 = int() + one: u16 = zero + 1 + two: u16 = one + 1 + neg_one: u16 = -one + assert neg_one == 65535 + assert one + one == 2 + assert one + two == 3 + assert one + neg_one == 0 + assert one - one == 0 + assert one - two == 65535 + assert one * one == 1 + assert one * two == 2 + assert two * two == 4 + assert two * neg_one == 65534 + assert neg_one * one == 65535 + assert neg_one * neg_one == 1 + assert two * 0 == 0 + assert 0 * two == 0 + assert -one == 65535 + assert -two == 65534 + assert -neg_one == 1 + assert -zero == 0 + +def test_bitwise_ops() -> None: + x: u16 = 47384 + int() + y: u16 = 20239 + int() + z: u16 = 28913 + int() + zero: u16 = int() + one: u16 = zero + 1 + two: u16 = zero + 2 + neg_one: u16 = -one + + assert x & y == 2312 + assert x & z == 12304 + assert z & z == z + assert x & zero == 0 + + assert x | y == 65311 + assert x | z == 63993 + assert z | z == z + assert x | 0 == x + + assert x ^ y == 62999 + assert x ^ z == 51689 + assert z ^ z == 0 + assert z ^ 0 == z + + assert x << one == 29232 + assert x << two == 58464 + assert z << two == 50116 + assert z << 0 == z + + assert x >> one == 23692 + assert x >> two == 11846 + assert z >> two == 7228 + assert z >> 0 == z + + for i in [0, 1, 255, 256, 1000, 32767, 65535]: + t: u16 = i + assert ~t == (~(i + int()) & 0xffff) + +def eq(x: u16, y: u16) -> bool: + return x == y + +def test_eq() -> None: + assert eq(int(), int()) + assert eq(5 + int(), 5 + int()) + assert not eq(int(), 1 + int()) + assert not eq(5 + int(), 6 + int()) + +def test_comparisons() -> None: + one: u16 = 1 + int() + one2: u16 = 1 + int() + two: u16 = 2 + int() + assert one < two + assert not (one < one2) + assert not (two < one) + assert two > one + assert not (one > one2) + assert not (one > two) + assert one <= two + assert one <= one2 + assert not (two <= one) + assert two >= one + assert one >= one2 + assert not (one >= two) + assert one == one2 + assert not (one == two) + assert one != two + assert not (one != one2) + +def test_mixed_comparisons() -> None: + u16_3: u16 = int() + 3 + int_5 = int() + 5 + assert u16_3 < int_5 + assert int_5 > u16_3 + b = u16_3 > int_5 + assert not b + + int_largest = int() + 65535 + assert int_largest > u16_3 + int_smallest = int() + assert u16_3 > int_smallest + + int_too_big = int() + 65536 + int_too_small = int() -1 + with assertRaises(ValueError): + assert u16_3 < int_too_big + with assertRaises(ValueError): + assert int_too_big < u16_3 + with assertRaises(ValueError): + assert u16_3 > int_too_small + with assertRaises(ValueError): + assert int_too_small < u16_3 + +def test_mixed_arithmetic_and_bitwise_ops() -> None: + u16_3: u16 = int() + 3 + int_5 = int() + 5 + assert u16_3 + int_5 == 8 + assert int_5 - u16_3 == 2 + assert u16_3 << int_5 == 96 + assert int_5 << u16_3 == 40 + assert u16_3 ^ int_5 == 6 + assert int_5 | u16_3 == 7 + + int_largest = int() + 65535 + assert int_largest - u16_3 == 65532 + int_smallest = int() + assert int_smallest + u16_3 == 3 + + int_too_big = int() + 65536 + int_too_small = int() - 1 + with assertRaises(ValueError): + assert u16_3 & int_too_big + with assertRaises(ValueError): + assert int_too_small & u16_3 + +def test_coerce_to_and_from_int() -> None: + for n in [0, 1, 255, 256, 1000, 32767, 65535]: + x: u16 = n + m: int = x + assert m == n + +def test_explicit_conversion_to_u16() -> None: + x = u16(5) + assert x == 5 + y = int() + ERROR + x = u16(y) + assert x == ERROR + n64: i64 = 233 + x = u16(n64) + assert x == 233 + n32: i32 = 234 + x = u16(n32) + assert x == 234 + z = u16(x) + assert z == 234 + n16: i16 = 231 + x = u16(n16) + assert x == 231 + n8: u8 = 232 + x = u16(n8) + assert x == 232 + +def test_explicit_conversion_overflow() -> None: + max_u16 = int() + 65535 + x = u16(max_u16) + assert x == 65535 + assert int(x) == max_u16 + + min_u16 = int() + y = u16(min_u16) + assert y == 0 + assert int(y) == min_u16 + + too_big = int() + 65536 + with assertRaises(ValueError): + x = u16(too_big) + + too_small = int() - 1 + with assertRaises(ValueError): + x = u16(too_small) + +def test_u16_from_large_small_literal() -> None: + x = u16(65535) + assert x == 65535 + x = u16(0) + assert x == 0 + +def test_u16_truncate_from_i64() -> None: + large = i64(2**32 + 65536 + 157 + int()) + x = u16(large) + assert x == 157 + small = i64(-2**32 - 65536 - 157 + int()) + x = u16(small) + assert x == 65536 - 157 + large2 = i64(2**16 + int()) + x = u16(large2) + assert x == 0 + small2 = i64(-2**16 - 1 - int()) + x = u16(small2) + assert x == 65535 + +def test_u16_truncate_from_i32() -> None: + large = i32(2**20 + 2**16 + 5 + int()) + assert u16(large) == 5 + small = i32(-2**20 - 2**16 - 1 + int()) + assert u16(small) == 65535 + +def from_float(x: float) -> u16: + return u16(x) + +def test_explicit_conversion_from_float() -> None: + assert from_float(0.0) == 0 + assert from_float(1.456) == 1 + assert from_float(234.567) == 234 + assert from_float(65535) == 65535 + assert from_float(0) == 0 + assert from_float(-0.999) == 0 + # The error message could be better, but this is acceptable + with assertRaises(ValueError, "int too large or small to convert to u16"): + assert from_float(float(65536)) + with assertRaises(ValueError, "int too large or small to convert to u16"): + # One ulp below the lowest valid i64 value + from_float(float(-1.0)) + +def test_tuple_u16() -> None: + a: u16 = 1 + b: u16 = 2 + t = (a, b) + a, b = t + assert a == 1 + assert b == 2 + x: Any = t + tt: Tuple[u16, u16] = x + assert tt == (1, 2) + +def test_convert_u16_to_native_int() -> None: + for i in [0, 1, 255, 1000, 32767, 65535]: + x: u16 = i + assert u8(x) == (i & 0xff) + assert i16(x) == (i if i < 32768 else i - 65536) + assert i32(x) == i + assert i64(x) == i diff --git a/mypyc/test/test_emitfunc.py b/mypyc/test/test_emitfunc.py index 490a0ae2d2057..b3cabb67214a9 100644 --- a/mypyc/test/test_emitfunc.py +++ b/mypyc/test/test_emitfunc.py @@ -75,6 +75,9 @@ pointer_rprimitive, short_int_rprimitive, str_rprimitive, + uint8_rprimitive, + uint16_rprimitive, + uint32_rprimitive, ) from mypyc.irbuild.vtable import compute_vtable from mypyc.namegen import NameGenerator @@ -118,6 +121,12 @@ def add_local(name: str, rtype: RType) -> Register: self.i32_1 = add_local("i32_1", int32_rprimitive) self.i64 = add_local("i64", int64_rprimitive) self.i64_1 = add_local("i64_1", int64_rprimitive) + self.u8 = add_local("u8", uint8_rprimitive) + self.u8_1 = add_local("u8_1", uint8_rprimitive) + self.u16 = add_local("u16", uint16_rprimitive) + self.u16_1 = add_local("u16_1", uint16_rprimitive) + self.u32 = add_local("u32", uint32_rprimitive) + self.u32_1 = add_local("u32_1", uint32_rprimitive) self.ptr = add_local("ptr", pointer_rprimitive) self.t = add_local("t", RTuple([int_rprimitive, bool_rprimitive])) self.tt = add_local( @@ -990,6 +999,30 @@ def test_int_op(self) -> None: """cpy_r_r0 = cpy_r_i64 >> cpy_r_i64_1;""", ) + def test_int_op_narrow_unsigned(self) -> None: + # Unsigned types narrower than int would be promoted to signed int in C, + # and e.g. u16 * u16 could overflow int (undefined behavior) + self.assert_emit( + IntOp(uint16_rprimitive, self.u16, self.u16_1, IntOp.MUL, 1), + """cpy_r_r0 = (unsigned int)cpy_r_u16 * cpy_r_u16_1;""", + ) + self.assert_emit( + IntOp(uint16_rprimitive, self.u16, self.u16_1, IntOp.LEFT_SHIFT, 1), + """cpy_r_r0 = (unsigned int)cpy_r_u16 << cpy_r_u16_1;""", + ) + self.assert_emit( + IntOp(uint8_rprimitive, self.u8, self.u8_1, IntOp.MUL, 1), + """cpy_r_r0 = (unsigned int)cpy_r_u8 * cpy_r_u8_1;""", + ) + self.assert_emit( + IntOp(uint16_rprimitive, self.u16, self.u16_1, IntOp.ADD, 1), + """cpy_r_r0 = cpy_r_u16 + cpy_r_u16_1;""", + ) + self.assert_emit( + IntOp(uint32_rprimitive, self.u32, self.u32_1, IntOp.MUL, 1), + """cpy_r_r0 = cpy_r_u32 * cpy_r_u32_1;""", + ) + def test_comparison_op(self) -> None: # signed self.assert_emit( diff --git a/mypyc/test/test_irbuild.py b/mypyc/test/test_irbuild.py index 0dc19b62a60fb..86ef4d264bbd1 100644 --- a/mypyc/test/test_irbuild.py +++ b/mypyc/test/test_irbuild.py @@ -46,6 +46,7 @@ "irbuild-i32.test", "irbuild-i16.test", "irbuild-u8.test", + "irbuild-u16.test", "irbuild-vec-i64.test", "irbuild-vec-misc.test", "irbuild-vec-t.test", diff --git a/mypyc/test/test_run.py b/mypyc/test/test_run.py index 217c8f9446ac8..d56b514859c9f 100644 --- a/mypyc/test/test_run.py +++ b/mypyc/test/test_run.py @@ -50,6 +50,7 @@ "run-i32.test", "run-i16.test", "run-u8.test", + "run-u16.test", "run-floats.test", "run-math.test", "run-bools.test", diff --git a/mypyc/test/test_tuplename.py b/mypyc/test/test_tuplename.py index 5dd51d45c16f7..255575e088290 100644 --- a/mypyc/test/test_tuplename.py +++ b/mypyc/test/test_tuplename.py @@ -8,9 +8,14 @@ RTuple, RUnion, bool_rprimitive, + int16_rprimitive, + int32_rprimitive, + int64_rprimitive, int_rprimitive, list_rprimitive, object_rprimitive, + uint8_rprimitive, + uint16_rprimitive, ) @@ -21,6 +26,9 @@ def setUp(self) -> None: def test_names(self) -> None: assert RTuple([int_rprimitive, int_rprimitive]).unique_id == "T2II" + assert RTuple([int64_rprimitive, int32_rprimitive]).unique_id == "T284" + assert RTuple([int16_rprimitive, uint16_rprimitive]).unique_id == "T22U2" + assert RTuple([uint8_rprimitive]).unique_id == "T1U1" assert RTuple([list_rprimitive, object_rprimitive, self.inst_a]).unique_id == "T3OOO" assert RTuple([list_rprimitive, object_rprimitive, self.inst_b]).unique_id == "T3OOO" assert RTuple([]).unique_id == "T0" diff --git a/mypyc/test/test_typeops.py b/mypyc/test/test_typeops.py index ff2c05ad983e1..f551ae3e74530 100644 --- a/mypyc/test/test_typeops.py +++ b/mypyc/test/test_typeops.py @@ -15,11 +15,19 @@ object_rprimitive, short_int_rprimitive, str_rprimitive, + uint8_rprimitive, + uint16_rprimitive, ) from mypyc.rt_subtype import is_runtime_subtype from mypyc.subtype import is_subtype -native_int_types = [int64_rprimitive, int32_rprimitive, int16_rprimitive] +native_int_types = [ + int64_rprimitive, + int32_rprimitive, + int16_rprimitive, + uint16_rprimitive, + uint8_rprimitive, +] class TestSubtype(unittest.TestCase): @@ -58,6 +66,24 @@ def test_int16(self) -> None: assert not is_subtype(int16_rprimitive, int64_rprimitive) assert not is_subtype(int16_rprimitive, int32_rprimitive) + def test_uint16(self) -> None: + assert is_subtype(uint16_rprimitive, uint16_rprimitive) + assert is_subtype(uint16_rprimitive, int_rprimitive) + assert not is_subtype(uint16_rprimitive, short_int_rprimitive) + assert not is_subtype(uint16_rprimitive, int64_rprimitive) + assert not is_subtype(uint16_rprimitive, int32_rprimitive) + assert not is_subtype(uint16_rprimitive, int16_rprimitive) + assert not is_subtype(uint16_rprimitive, uint8_rprimitive) + + def test_uint8(self) -> None: + assert is_subtype(uint8_rprimitive, uint8_rprimitive) + assert is_subtype(uint8_rprimitive, int_rprimitive) + assert not is_subtype(uint8_rprimitive, short_int_rprimitive) + assert not is_subtype(uint8_rprimitive, int64_rprimitive) + assert not is_subtype(uint8_rprimitive, int32_rprimitive) + assert not is_subtype(uint8_rprimitive, int16_rprimitive) + assert not is_subtype(uint8_rprimitive, uint16_rprimitive) + class TestRuntimeSubtype(unittest.TestCase): def test_bit(self) -> None: diff --git a/test-data/unit/lib-stub/mypy_extensions.pyi b/test-data/unit/lib-stub/mypy_extensions.pyi index 4295c33f81ade..eedc2bceb921a 100644 --- a/test-data/unit/lib-stub/mypy_extensions.pyi +++ b/test-data/unit/lib-stub/mypy_extensions.pyi @@ -50,7 +50,37 @@ class FlexibleAlias(Generic[_T, _U]): ... class __SupportsInt(Protocol[T_co]): def __int__(self) -> int: pass -_Int = Union[int, u8, i16, i32, i64] +_Int = Union[int, u16, u8, i16, i32, i64] + +class u16: + def __init__(self, x: Union[_Int, str, bytes, SupportsInt], base: int = 10) -> None: ... + def __add__(self, x: u16) -> u16: ... + def __radd__(self, x: u16) -> u16: ... + def __sub__(self, x: u16) -> u16: ... + def __rsub__(self, x: u16) -> u16: ... + def __mul__(self, x: u16) -> u16: ... + def __rmul__(self, x: u16) -> u16: ... + def __floordiv__(self, x: u16) -> u16: ... + def __rfloordiv__(self, x: u16) -> u16: ... + def __mod__(self, x: u16) -> u16: ... + def __rmod__(self, x: u16) -> u16: ... + def __and__(self, x: u16) -> u16: ... + def __rand__(self, x: u16) -> u16: ... + def __or__(self, x: u16) -> u16: ... + def __ror__(self, x: u16) -> u16: ... + def __xor__(self, x: u16) -> u16: ... + def __rxor__(self, x: u16) -> u16: ... + def __lshift__(self, x: u16) -> u16: ... + def __rlshift__(self, x: u16) -> u16: ... + def __rshift__(self, x: u16) -> u16: ... + def __rrshift__(self, x: u16) -> u16: ... + def __neg__(self) -> u16: ... + def __invert__(self) -> u16: ... + def __pos__(self) -> u16: ... + def __lt__(self, x: u16) -> bool: ... + def __le__(self, x: u16) -> bool: ... + def __ge__(self, x: u16) -> bool: ... + def __gt__(self, x: u16) -> bool: ... class u8: def __init__(self, x: Union[_Int, str, bytes, SupportsInt], base: int = 10) -> None: ...