Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions mypy/typeanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2915,6 +2915,16 @@ def visit_type_alias_type(self, t: TypeAliasType) -> Type:
return t


# Native int types that librt.vecs supports as vec item types (there is a specialized
# vec type for each of these)
VEC_NATIVE_INT_ITEM_NAMES: Final = (
"mypy_extensions.i64",
"mypy_extensions.i32",
"mypy_extensions.i16",
"mypy_extensions.u8",
)


def check_vec_type_args(
args: tuple[Type, ...] | list[Type], ctx: Context, api: SemanticAnalyzerCoreInterface
) -> bool:
Expand All @@ -2931,6 +2941,13 @@ def check_vec_type_args(
if arg.type.fullname == "builtins.int":
# A fixed-width integer such as 'i64' must be used instead of plain 'int'
ok = False
elif (
arg.type.fullname in MYPYC_NATIVE_INT_NAMES
and arg.type.fullname not in VEC_NATIVE_INT_ITEM_NAMES
):
# librt.vecs has no specialized vec for this native int type, and
# a generic vec can't hold its values
ok = False
elif isinstance(arg, UnionType):
non_optional = None
items = [get_proper_type(item) for item in arg.items]
Expand Down
2 changes: 2 additions & 0 deletions mypyc/doc/librt_vecs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ for these *value item types*:

``int`` is not a valid item type, since it has an arbitrary precision, and vec is an
efficiency-focused type. Use one of the fixed-length integer types instead.
The other native integer types (``i8``, ``u16``, ``u32`` and ``u64``) aren't supported
as item types yet.

Class item types (e.g. ``str`` or ``MyNativeClass``) are represented as regular object references.
Optional class item types (e.g. ``str | None``) are supported for convenience, but arbitrary
Expand Down
21 changes: 21 additions & 0 deletions mypyc/test-data/irbuild-vec-t.test
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,27 @@ main:5: error: Invalid item type for "vec"
main:6: error: Invalid item type for "vec"
main:7: error: Invalid item type for "vec"

[case testVecTCheckNativeIntItemType]
from librt.vecs import vec
from mypy_extensions import i64, i32, i16, i8, u64, u32, u16, u8

def bad1(v: vec[i8]) -> None: pass
def bad2(v: vec[u16]) -> None: pass
def bad3(v: vec[u32]) -> None: pass
def bad4(v: vec[u64]) -> None: pass
def bad5(v: vec[vec[u64]]) -> None: pass
def bad6() -> None:
vec[u64]()

def ok1(v: vec[i64], v2: vec[i32], v3: vec[i16], v4: vec[u8]) -> None: pass
[out]
main:4: error: Invalid item type for "vec"
main:5: error: Invalid item type for "vec"
main:6: error: Invalid item type for "vec"
main:7: error: Invalid item type for "vec"
main:8: error: Invalid item type for "vec"
main:10: error: Invalid item type for "vec"

[case testVecTCreateWithCap_64bit]
from librt.vecs import vec
from mypy_extensions import i64
Expand Down
8 changes: 7 additions & 1 deletion test-data/unit/check-vec.test
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ from types import EllipsisType
from typing import Optional, Any, TypeVar

from librt.vecs import vec
from mypy_extensions import i64, i32, i16, u8
from mypy_extensions import i64, i32, i16, i8, u64, u32, u16, u8

def f(v: vec[i64]) -> None:
x: i64 = v[0]
Expand Down Expand Up @@ -38,6 +38,12 @@ vec_bad_optional1: vec[int | None] # E: Invalid item type for "vec"
vec_bad_optional2: vec[i64 | None] # E: Invalid item type for "vec"
vec_bad_optional3: vec[bool | None] # E: Invalid item type for "vec"
vec_bad_optional4: vec[float | None] # E: Invalid item type for "vec"
vec_bad_i8: vec[i8] # E: Invalid item type for "vec"
vec_bad_u16: vec[u16] # E: Invalid item type for "vec"
vec_bad_u32: vec[u32] # E: Invalid item type for "vec"
vec_bad_u64: vec[u64] # E: Invalid item type for "vec"
vec_bad_nested: vec[vec[u64]] # E: Invalid item type for "vec"
vec_bad_optional5: vec[u64 | None] # E: Invalid item type for "vec"

T = TypeVar("T")

Expand Down
Loading