Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Renamed cap= -> capacity=
  • Loading branch information
JukkaL committed Apr 23, 2026
commit 55d70861f5661d4030d87b8842181a4b8255ca8d
4 changes: 2 additions & 2 deletions mypy/typeshed/stubs/librt/librt/vecs.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ T = TypeVar("T")

class vec(Generic[T]):
@overload
def __init__(self, *, cap: i64 = ...) -> None: ...
def __init__(self, *, capacity: i64 = ...) -> None: ...
@overload
def __init__(self, items: Iterable[T], /, *, cap: i64 = ...) -> None: ...
def __init__(self, items: Iterable[T], /, *, capacity: i64 = ...) -> None: ...
def __len__(self) -> i64: ...
@overload
def __getitem__(self, i: i64, /) -> T: ...
Expand Down
12 changes: 4 additions & 8 deletions mypyc/irbuild/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,7 @@ def transform_call_expr(builder: IRBuilder, expr: CallExpr) -> Value:
item_type = builder.type_to_rtype(analyzed.types[0])
vec_type = RVec(item_type)
cap = _get_vec_cap(builder, expr)
if len(expr.args) == 0 or (
len(expr.args) == 1 and expr.arg_kinds == [ARG_NAMED]
):
if len(expr.args) == 0 or (len(expr.args) == 1 and expr.arg_kinds == [ARG_NAMED]):
# vec[T]() or vec[T](cap=N)
return vec_create(builder.builder, vec_type, 0, expr.line, cap=cap)
elif (
Expand All @@ -377,9 +375,7 @@ def transform_call_expr(builder: IRBuilder, expr: CallExpr) -> Value:
and expr.arg_kinds == [ARG_POS, ARG_NAMED]
Comment on lines +372 to +375
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

formatting makes this expression a little confusing. add parenthesis so it's `elif ((... and ...) or (... and ...))?

):
# vec[T](items) or vec[T](items, cap=N)
return translate_vec_create_from_iterable(
builder, vec_type, expr.args[0], cap=cap
)
return translate_vec_create_from_iterable(builder, vec_type, expr.args[0], cap=cap)
callee = analyzed.expr # Unwrap type application

if isinstance(callee, MemberExpr):
Expand Down Expand Up @@ -574,9 +570,9 @@ def translate_super_method_call(builder: IRBuilder, expr: CallExpr, callee: Supe


def _get_vec_cap(builder: IRBuilder, expr: CallExpr) -> Value | None:
"""Extract the 'cap' keyword argument value from a vec() call, or None."""
"""Extract the 'capacity' keyword argument value from a vec() call, or None."""
for i, (kind, name) in enumerate(zip(expr.arg_kinds, expr.arg_names)):
if kind == ARG_NAMED and name == "cap":
if kind == ARG_NAMED and name == "capacity":
return builder.accept(expr.args[i])
return None

Expand Down
4 changes: 2 additions & 2 deletions mypyc/lib-rt/vecs/librt_vecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ typedef struct {

static PyObject *vec_generic_alias_call(PyObject *self, PyObject *args, PyObject *kw)
{
static char *kwlist[] = {"", "cap", NULL};
static char *kwlist[] = {"", "capacity", NULL};
PyObject *init = NULL;
int64_t cap = 0;
if (!PyArg_ParseTupleAndKeywords(args, kw, "|OL:vec", kwlist, &init, &cap)) {
return NULL;
}
if (cap < 0) {
PyErr_SetString(PyExc_ValueError, "cap must not be negative");
PyErr_SetString(PyExc_ValueError, "capacity must not be negative");
return NULL;
}
VecGenericAlias *p = (VecGenericAlias *)self;
Expand Down
4 changes: 2 additions & 2 deletions mypyc/lib-rt/vecs/vec_template.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,14 @@ PyObject *FUNC(FromIterable)(PyObject *iterable, int64_t cap) {
}

static PyObject *vec_new(PyTypeObject *self, PyObject *args, PyObject *kw) {
static char *kwlist[] = {"", "cap", NULL};
static char *kwlist[] = {"", "capacity", NULL};
PyObject *init = NULL;
int64_t cap = 0;
if (!PyArg_ParseTupleAndKeywords(args, kw, "|OL:vec", kwlist, &init, &cap)) {
return NULL;
}
if (cap < 0) {
PyErr_SetString(PyExc_ValueError, "cap must not be negative");
PyErr_SetString(PyExc_ValueError, "capacity must not be negative");
return NULL;
}
if (init == NULL) {
Expand Down
8 changes: 4 additions & 4 deletions mypyc/test-data/irbuild-vec-i64.test
Original file line number Diff line number Diff line change
Expand Up @@ -826,13 +826,13 @@ from librt.vecs import vec
from mypy_extensions import i64

def cap_only() -> vec[i64]:
return vec[i64](cap=5)
return vec[i64](capacity=5)

def cap_zero() -> vec[i64]:
return vec[i64](cap=0)
return vec[i64](capacity=0)

def cap_variable(n: i64) -> vec[i64]:
return vec[i64](cap=n)
return vec[i64](capacity=n)
[out]
def cap_only():
r0 :: vec[i64]
Expand All @@ -856,7 +856,7 @@ from librt.vecs import vec
from mypy_extensions import i64

def list_with_cap() -> vec[i64]:
return vec[i64]([1, 2], cap=5)
return vec[i64]([1, 2], capacity=5)
[out]
def list_with_cap():
r0 :: vec[i64]
Expand Down
6 changes: 3 additions & 3 deletions mypyc/test-data/irbuild-vec-nested.test
Original file line number Diff line number Diff line change
Expand Up @@ -315,13 +315,13 @@ from librt.vecs import vec
from mypy_extensions import i64

def cap_str() -> vec[vec[str]]:
return vec[vec[str]](cap=5)
return vec[vec[str]](capacity=5)

def cap_i64() -> vec[vec[i64]]:
return vec[vec[i64]](cap=5)
return vec[vec[i64]](capacity=5)

def cap_variable(n: i64) -> vec[vec[str]]:
return vec[vec[str]](cap=n)
return vec[vec[str]](capacity=n)
[out]
def cap_str():
r0 :: object
Expand Down
8 changes: 4 additions & 4 deletions mypyc/test-data/irbuild-vec-t.test
Original file line number Diff line number Diff line change
Expand Up @@ -365,13 +365,13 @@ from librt.vecs import vec
from mypy_extensions import i64

def cap_only() -> vec[str]:
return vec[str](cap=5)
return vec[str](capacity=5)

def cap_zero() -> vec[str]:
return vec[str](cap=0)
return vec[str](capacity=0)

def cap_variable(n: i64) -> vec[str]:
return vec[str](cap=n)
return vec[str](capacity=n)
[out]
def cap_only():
r0 :: object
Expand Down Expand Up @@ -407,7 +407,7 @@ from librt.vecs import vec
from mypy_extensions import i64

def list_with_cap() -> vec[str]:
return vec[str](['a', 'b'], cap=5)
return vec[str](['a', 'b'], capacity=5)
[out]
def list_with_cap():
r0, r1 :: str
Expand Down
32 changes: 16 additions & 16 deletions mypyc/test-data/run-vecs-i64-interp.test
Original file line number Diff line number Diff line change
Expand Up @@ -419,32 +419,32 @@ def test_pop_index() -> None:
assert v == vec[i64]()

def test_cap_empty() -> None:
v = vec[i64](cap=3)
v = vec[i64](capacity=3)
assert len(v) == 0
assert v == vec[i64]()

def test_cap_with_initializer() -> None:
v = vec[i64]([10, 20], cap=3)
v = vec[i64]([10, 20], capacity=3)
assert len(v) == 2
assert v[0] == 10
assert v[1] == 20
assert v == vec[i64]([10, 20])

def test_cap_zero() -> None:
v = vec[i64](cap=0)
v = vec[i64](capacity=0)
assert len(v) == 0
v = vec[i64]([5], cap=0)
v = vec[i64]([5], capacity=0)
assert v == vec[i64]([5])

def test_cap_negative() -> None:
with assertRaises(ValueError):
vec[i64](cap=-1)
vec[i64](capacity=-1)
with assertRaises(ValueError):
vec[i64]([1], cap=-1)
vec[i64]([1], capacity=-1)

def test_cap_buffer_reuse_empty() -> None:
# cap=3, append 3 items -> all fit, buffer reused (no reallocation)
v = vec[i64](cap=3)
# capacity=3, append 3 items -> all fit, buffer reused (no reallocation)
v = vec[i64](capacity=3)
old = v
v = append(v, 1)
v = append(v, 2)
Expand All @@ -455,8 +455,8 @@ def test_cap_buffer_reuse_empty() -> None:
assert v[0] == 99

def test_cap_buffer_reuse_with_initializer() -> None:
# 2 items with cap=3 -> one more append fits without reallocation
v = vec[i64]([10, 20], cap=3)
# 2 items with capacity=3 -> one more append fits without reallocation
v = vec[i64]([10, 20], capacity=3)
old = v
v = append(v, 30)
assert len(v) == 3
Expand All @@ -465,8 +465,8 @@ def test_cap_buffer_reuse_with_initializer() -> None:
assert old[0] == 99

def test_cap_buffer_reuse_at_capacity() -> None:
# cap=2, fill with 2 items -> at capacity, next append must reallocate
v = vec[i64]([10, 20], cap=2)
# capacity=2, fill with 2 items -> at capacity, next append must reallocate
v = vec[i64]([10, 20], capacity=2)
old = v
v = append(v, 30)
# v now has a new buffer; appending to old should NOT affect v
Expand All @@ -475,7 +475,7 @@ def test_cap_buffer_reuse_at_capacity() -> None:

def test_cap_read_old_after_realloc() -> None:
# After reallocation, old alias must still have valid items
v = vec[i64]([10, 20, 30], cap=3)
v = vec[i64]([10, 20, 30], capacity=3)
old = v
v = append(v, 40)
v[0] = 99
Expand All @@ -484,8 +484,8 @@ def test_cap_read_old_after_realloc() -> None:
assert old[2] == 30

def test_cap_exceeded() -> None:
# cap=2, fill to capacity, then one more append triggers reallocation
v = vec[i64](cap=2)
# capacity=2, fill to capacity, then one more append triggers reallocation
v = vec[i64](capacity=2)
v = append(v, 10)
v = append(v, 20)
old = v
Expand All @@ -494,7 +494,7 @@ def test_cap_exceeded() -> None:
assert v[0] == 10

def test_cap_initializer_longer_than_cap() -> None:
v = vec[i64]([10, 20, 30], cap=1)
v = vec[i64]([10, 20, 30], capacity=1)
assert len(v) == 3
assert v[0] == 10
assert v[1] == 20
Expand Down
8 changes: 4 additions & 4 deletions mypyc/test-data/run-vecs-i64.test
Original file line number Diff line number Diff line change
Expand Up @@ -490,24 +490,24 @@ def test_tuple() -> None:
ftuple(True)

def test_cap_empty() -> None:
v = vec[i64](cap=3)
v = vec[i64](capacity=3)
assert len(v) == 0
assert v == vec[i64]()

def test_cap_with_initializer() -> None:
v = vec[i64]([1, 2], cap=3)
v = vec[i64]([1, 2], capacity=3)
assert len(v) == 2
assert v[0] == 1
assert v[1] == 2

def test_cap_variable(n: i64 = 3) -> None:
v = vec[i64](cap=n)
v = vec[i64](capacity=n)
assert len(v) == 0
v = append(v, 5)
assert v[0] == 5

def test_cap_buffer_reuse() -> None:
v = vec[i64]([1, 2], cap=3)
v = vec[i64]([1, 2], capacity=3)
old = v
v = append(v, 3)
v[0] = 99
Expand Down
20 changes: 10 additions & 10 deletions mypyc/test-data/run-vecs-misc-interp.test
Original file line number Diff line number Diff line change
Expand Up @@ -455,51 +455,51 @@ def test_iterator_protocol() -> None:

def test_cap_empty() -> None:
for t in ITEM_TYPES:
v = vec[t](cap=3)
v = vec[t](capacity=3)
assert len(v) == 0
assert v == vec[t]()

def test_cap_with_initializer() -> None:
v = vec[float]([1.5, 2.5], cap=3)
v = vec[float]([1.5, 2.5], capacity=3)
assert len(v) == 2
assert v[0] == 1.5
assert v[1] == 2.5

v = vec[u8]([10, 20], cap=3)
v = vec[u8]([10, 20], capacity=3)
assert len(v) == 2
assert v[0] == 10

v = vec[i16]([-100, 100], cap=3)
v = vec[i16]([-100, 100], capacity=3)
assert len(v) == 2

v = vec[i32]([1000, 2000], cap=3)
v = vec[i32]([1000, 2000], capacity=3)
assert len(v) == 2

v = vec[bool]([True, False], cap=3)
v = vec[bool]([True, False], capacity=3)
assert len(v) == 2
assert v[0] is True
assert v[1] is False

def test_cap_negative() -> None:
for t in ITEM_TYPES:
with assertRaises(ValueError):
vec[t](cap=-1)
vec[t](capacity=-1)

def test_cap_buffer_reuse() -> None:
# Verify buffer reuse within cap for each type
v = vec[float]([1.5], cap=3)
v = vec[float]([1.5], capacity=3)
old = v
v = append(v, 2.5)
v[0] = 9.5
assert old[0] == 9.5 # shared buffer

v = vec[u8]([10], cap=3)
v = vec[u8]([10], capacity=3)
old = v
v = append(v, 20)
v[0] = 99
assert old[0] == 99

v = vec[bool]([True], cap=3)
v = vec[bool]([True], capacity=3)
old = v
v = append(v, False)
v[0] = False
Expand Down
12 changes: 6 additions & 6 deletions mypyc/test-data/run-vecs-misc.test
Original file line number Diff line number Diff line change
Expand Up @@ -238,28 +238,28 @@ def test_nested_misc() -> None:
assert v[:5] == vec[vec[float]]([vec[float]([3.5])])

def test_cap_misc() -> None:
v_f = vec[float](cap=3)
v_f = vec[float](capacity=3)
assert len(v_f) == 0
v_f = append(v_f, 1.5)
assert v_f[0] == 1.5

v_u8 = vec[u8]([1, 2], cap=5)
v_u8 = vec[u8]([1, 2], capacity=5)
assert len(v_u8) == 2
assert v_u8[0] == 1

v_i16 = vec[i16](cap=2)
v_i16 = vec[i16](capacity=2)
v_i16 = append(v_i16, i16(10))
assert v_i16[0] == 10

v_i32 = vec[i32]([i32(7)], cap=3)
v_i32 = vec[i32]([i32(7)], capacity=3)
assert v_i32[0] == 7

v_bool = vec[bool](cap=4)
v_bool = vec[bool](capacity=4)
assert len(v_bool) == 0
v_bool = append(v_bool, True)
assert v_bool[0] == True

v_nested = vec[vec[float]](cap=2)
v_nested = vec[vec[float]](capacity=2)
assert len(v_nested) == 0
v_nested = append(v_nested, vec[float]([1.0]))
assert v_nested[0] == vec[float]([1.0])
Loading