diff --git a/ffi/README.md b/ffi/README.md index f67915d72..7c75054dc 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -613,7 +613,8 @@ caller imports it with no ignores. snake_case without their type's name (`moq.TrackProducer.append_group`), and two types whose names would match kept apart as Kotlin's are (`TokioConnect_Error`), as are top-level names that clash. A keyword, a member Python gives every such class, or a - method named as a field takes a trailing `_`. + method named as a field takes a trailing `_`. A member or top-level name may be a + builtin's (`bytes`, `int`): the module reads each builtin through an alias (`_bytes`). Constructors such as `new` stay static methods: `Counter.new(1)`. - **Objects** are classes owning their handle, freed once their last reference goes, which a call holds until it returns, so there is no `close()`. A `self` method, or diff --git a/ffi/fixtures/api/src/lib.rs b/ffi/fixtures/api/src/lib.rs index b3e7f593a..6fc969365 100644 --- a/ffi/fixtures/api/src/lib.rs +++ b/ffi/fixtures/api/src/lib.rs @@ -178,7 +178,7 @@ impl Clash { } /// Its bytes, its text's or its own. - pub fn raw(&self) -> Vec { + pub fn bytes(&self) -> Vec { match self { Clash::Bytes(b) => b.clone(), Clash::String(s) => s.as_bytes().to_vec(), diff --git a/ffi/runtime/native.py b/ffi/runtime/native.py index 4194d37fe..d20787fc0 100644 --- a/ffi/runtime/native.py +++ b/ffi/runtime/native.py @@ -4,7 +4,7 @@ class _n: - PENDING: object + PENDING: _object class Object: __slots__ = () @@ -15,23 +15,23 @@ class Value: class Future: __slots__ = () - def poll(self, queue: _n.Queue, /) -> object: ... + def poll(self, queue: _n.Queue, /) -> _object: ... - def claim(self) -> int: ... + def claim(self) -> _int: ... def close(self) -> None: ... class Queue: def __init__(self, loop: _asyncio.AbstractEventLoop, /) -> None: ... - def fileno(self) -> int: ... + def fileno(self) -> _int: ... - def take(self, read: bool, /) -> list[int]: ... + def take(self, read: _bool, /) -> _list[_int]: ... class Node: def __init__(self, queue: _n.Queue, /) -> None: ... - def token(self) -> int: ... + def token(self) -> _int: ... def wake(self) -> None: ... diff --git a/ffi/runtime/rt.py b/ffi/runtime/rt.py index d05e044d6..7061aca46 100644 --- a/ffi/runtime/rt.py +++ b/ffi/runtime/rt.py @@ -38,13 +38,13 @@ class _Wakeup: def __init__(self, loop: _asyncio.AbstractEventLoop) -> None: self.queue = _n.Queue(loop) # The future each parked poll waits on, and each `Waker`'s callback, by token. - self.parked: dict[int, _asyncio.Future[None]] = {} - self.wakers: dict[int, _typing.Callable[[], object]] = {} + self.parked: _dict[_int, _asyncio.Future[None]] = {} + self.wakers: _dict[_int, _typing.Callable[[], _object]] = {} self.pump: _asyncio.Task[None] | None = None fd = self.queue.fileno() try: loop.add_reader(fd, self.dispatch, True) - except NotImplementedError: + except _NotImplementedError: # A proactor loop, Windows' default, reads only through its own calls. sock = _socket.fromfd(fd, _socket.AF_INET if _sys.platform == "win32" else _socket.AF_UNIX, _socket.SOCK_STREAM) sock.setblocking(False) @@ -56,7 +56,7 @@ async def _pump(self, loop: _asyncio.AbstractEventLoop, sock: _socket.socket) -> await loop.sock_recv(sock, 64) self.dispatch(True) - def dispatch(self, read: bool) -> None: + def dispatch(self, read: _bool) -> None: loop: _asyncio.AbstractEventLoop | None = None for token in self.queue.take(read): if token < 0: @@ -103,7 +103,7 @@ class Waker: _node: _n.Node | _n.Returned - def __init__(self, wake: _typing.Callable[[], object], loop: _asyncio.AbstractEventLoop | None = None) -> None: + def __init__(self, wake: _typing.Callable[[], _object], loop: _asyncio.AbstractEventLoop | None = None) -> None: w = _wakeup(_get_running_loop() if loop is None else loop) self._node = _n.Node(w.queue) w.wakers[self._node.token()] = wake @@ -126,22 +126,22 @@ def __init__(self, awaitable: _typing.Awaitable[None]) -> None: self._loop = _get_running_loop() # Refused at the call, before Rust takes it, rather than stranding Rust once started. if not _inspect.isawaitable(awaitable): - raise TypeError(f"{type(awaitable).__name__} is not awaitable") + raise _TypeError(f"{_type(awaitable).__name__} is not awaitable") if _asyncio.isfuture(awaitable) and awaitable.get_loop() is not self._loop: - raise ValueError("a future of another event loop") + raise _ValueError("a future of another event loop") self._awaitable = awaitable self._task: _asyncio.Future[None] | None = None self._closed = False - def start(self, ended: _typing.Callable[[_asyncio.Future[None]], object]) -> None: + def start(self, ended: _typing.Callable[[_asyncio.Future[None]], _object]) -> None: self._loop.call_soon_threadsafe(self._run, ended) - def _run(self, ended: _typing.Callable[[_asyncio.Future[None]], object]) -> None: + def _run(self, ended: _typing.Callable[[_asyncio.Future[None]], _object]) -> None: if self._closed: return try: self._task = _asyncio.ensure_future(self._awaitable, loop=self._loop) - except BaseException as e: + except _BaseException as e: # `ended` aborts on it, as on a task that raised. failed = self._loop.create_future() failed.set_exception(e) @@ -181,9 +181,9 @@ class ForeignReader(_typing.Protocol): `SEEK_CUR`, or `SEEK_END` and returns the position from the start. An `OSError` either raises fails Rust's read or seek with its message; any other exception aborts.""" - def readinto(self, buffer: memoryview, /) -> int | None: ... + def readinto(self, buffer: _memoryview, /) -> _int | None: ... - def seek(self, offset: int, whence: int = 0, /) -> int: ... + def seek(self, offset: _int, whence: _int = 0, /) -> _int: ... class _Const: @@ -191,8 +191,8 @@ class _Const: __slots__ = ("get",) - def __init__(self, get: _typing.Callable[[], object]) -> None: + def __init__(self, get: _typing.Callable[[], _object]) -> None: self.get = get - def __get__(self, obj: object, cls: object = None) -> object: + def __get__(self, obj: _object, cls: _object = None) -> _object: return self.get() diff --git a/ffi/src/python.rs b/ffi/src/python.rs index 392d94fe8..c0fb84301 100644 --- a/ffi/src/python.rs +++ b/ffi/src/python.rs @@ -89,15 +89,22 @@ const TAKEN: &[&str] = &[ "_Exception", "_ValueError", "_AttributeError", + "_BaseException", + "_ImportError", + "_NotImplementedError", + "_bool", + "_bytearray", + "_object", + "_range", "ForeignReader", ]; /// Any buffer, as Python before 3.12's `collections.abc.Buffer` spells it. -const BUFFER: &str = "bytes | bytearray | memoryview"; +const BUFFER: &str = "_bytes | _bytearray | _memoryview"; /// A socket address as Python's `socket` module holds it: IPv4's, and IPv6's. -const V4: &str = "tuple[str, int]"; -const V6: &str = "tuple[str, int, int, int]"; +const V4: &str = "_tuple[_str, _int]"; +const V6: &str = "_tuple[_str, _int, _int, _int]"; /// The registry's first entries, the runtime's: the glue's `T_FUTURE`, `T_TIMEDELTA`, /// `T_RESUME`, `T_WAKER`, and `T_POLLABLE`. @@ -157,7 +164,7 @@ pub fn generate(abi: &Abi<'_>, packages: &str, contract: u64) -> Result object:\n\t\t\"\"\"A constant, made anew each time it is read, as Rust makes it.\"\"\"\n\t\tget = _consts.get(name)\n\t\tif get is None:\n\t\t\traise _AttributeError(f\"module {{__name__!r}} has no attribute {{name!r}}\")\n\t\treturn get()\n", + "{RUNTIME}\n{}{}if _typing.TYPE_CHECKING:\n{declared}else:\n\t_n._register(\n{registry}\t)\n\n\t_consts = {{\n{consts}\t}}\n\n\tdef __getattr__(name: _str) -> _object:\n\t\t\"\"\"A constant, made anew each time it is read, as Rust makes it.\"\"\"\n\t\tget = _consts.get(name)\n\t\tif get is None:\n\t\t\traise _AttributeError(f\"module {{__name__!r}} has no attribute {{name!r}}\")\n\t\treturn get()\n", g.py, g.top ); let mut hasher = StableHasher::new(); @@ -183,10 +190,15 @@ pub fn generate(abi: &Abi<'_>, packages: &str, contract: u64) -> Result, packages: &str, contract: u64) -> Result, packages: &str, contract: u64) -> Result bool:\n\ + def _same(a: _object, b: _object, /) -> _bool:\n\ \t\"\"\"Whether a and b are equal as Rust's `==` compares them: never by identity, so a NaN\n\ \tequals nothing, while a list's, tuple's, or dict's own `==` takes an item as equal to itself.\"\"\"\n\ \tif _isinstance(a, (_list, _tuple)):\n\ - \t\txs = _typing.cast(\"_typing.Sequence[object]\", a)\n\ - \t\tys = _typing.cast(\"_typing.Sequence[object]\", b)\n\ + \t\txs = _typing.cast(\"_typing.Sequence[_object]\", a)\n\ + \t\tys = _typing.cast(\"_typing.Sequence[_object]\", b)\n\ \t\treturn _type(xs) is _type(ys) and _len(xs) == _len(ys) and _all(_same(x, y) for x, y in _zip(xs, ys))\n\ \tif _isinstance(a, _dict):\n\ \t\tif not _isinstance(b, _dict):\n\ \t\t\treturn False\n\ - \t\tm = _typing.cast(\"_typing.Mapping[object, object]\", a)\n\ - \t\tn = _typing.cast(\"_typing.Mapping[object, object]\", b)\n\ + \t\tm = _typing.cast(\"_typing.Mapping[_object, _object]\", a)\n\ + \t\tn = _typing.cast(\"_typing.Mapping[_object, _object]\", b)\n\ \t\treturn m.keys() == n.keys() and _all(_same(v, n[k]) for k, v in m.items())\n\ \tif a is None or b is None or _isinstance(a, (_int, _float, _str)):\n\ \t\treturn a == b\n\ \ttry:\n\ \t\t# Any buffer, through a memoryview, which compares numbers as Rust does, where\n\ \t\t# numpy's own `==` compares item by item into another array.\n\ - \t\treturn _memoryview(_typing.cast(\"bytes\", a)) == _memoryview(_typing.cast(\"bytes\", b))\n\ + \t\treturn _memoryview(_typing.cast(\"_bytes\", a)) == _memoryview(_typing.cast(\"_bytes\", b))\n\ \texcept _TypeError:\n\ \t\treturn a == b\n\n\n\ - def _load() -> object:\n\ + def _load() -> _object:\n\ \timport importlib.machinery\n\ \timport importlib.util\n\ \timport os\n\n\ @@ -242,7 +256,7 @@ pub fn generate(abi: &Abi<'_>, packages: &str, contract: u64) -> Result Gen<'a> { Type::Const(_) => unreachable!("a const argument is no value"), Type::Array(n) => Abi::array_class(*n), Type::Unit => "None".to_string(), - Type::Bool => "bool".to_string(), - Type::Int(_) | Type::NonZero(_) | Type::U128 => "int".to_string(), - Type::F32 | Type::F64 => "float".to_string(), - Type::Char | Type::Str | Type::String | Type::Path | Type::PathBuf => "str".to_string(), - Type::Slice | Type::Vec | Type::Bytes => "memoryview".to_string(), + Type::Bool => "_bool".to_string(), + Type::Int(_) | Type::NonZero(_) | Type::U128 => "_int".to_string(), + Type::F32 | Type::F64 => "_float".to_string(), + Type::Char | Type::Str | Type::String | Type::Path | Type::PathBuf => "_str".to_string(), + Type::Slice | Type::Vec | Type::Bytes => "_memoryview".to_string(), Type::Duration => "_datetime.timedelta".to_string(), Type::SocketAddr(family) => match family { Family::V4 => V4.to_string(), @@ -477,7 +491,7 @@ impl<'a> Gen<'a> { Family::Any => format!("{V4} | {V6}"), }, Type::List(_, inner) if ty.block().is_some() => format!("_array.array[{}]", self.py_ty(inner)), - Type::List(_, inner) => format!("list[{}]", self.py_ty(inner)), + Type::List(_, inner) => format!("_list[{}]", self.py_ty(inner)), Type::Waker => "Waker".to_string(), Type::Reader => "ForeignReader".to_string(), Type::Future => "_typing.Awaitable[None]".to_string(), @@ -492,11 +506,11 @@ impl<'a> Gen<'a> { let params: Vec = params.iter().map(|p| self.py_lent(p)).collect(); format!("_typing.Callable[[{}], {}]", params.join(", "), self.py_param(ret)) } - Type::Range(_) => "range".to_string(), + Type::Range(_) => "_range".to_string(), Type::Bound(_) | Type::Bounds(_) => self.class(&abi.range(ty)), Type::Tuple(elements) => { let elements: Vec = elements.iter().map(|e| self.py_ty(e)).collect(); - format!("tuple[{}]", elements.join(", ")) + format!("_tuple[{}]", elements.join(", ")) } } } @@ -520,17 +534,17 @@ impl<'a> Gen<'a> { fn py_in(&self, ty: &Type, list: &str) -> String { match ty { Type::Slice | Type::Vec | Type::Bytes => BUFFER.to_string(), - Type::Path | Type::PathBuf => "str".to_string(), + Type::Path | Type::PathBuf => "_str".to_string(), // Python's own IPv6 address may leave out its flowinfo and scope_id. Type::SocketAddr(Family::V6) => format!("{V4} | {V6}"), // Any buffer of its numbers, as numpy's arrays are too. - Type::List(..) if ty.block().is_some() => format!("{} | memoryview", self.py_ty(ty)), + Type::List(..) if ty.block().is_some() => format!("{} | _memoryview", self.py_ty(ty)), Type::List(_, inner) => format!("{list}[{}]", self.py_in(inner, list)), Type::Option(inner) => format!("{} | None", self.py_in(inner, list)), Type::Box(inner) | Type::Ref(_, inner) => self.py_in(inner, list), Type::Tuple(elements) => { let elements: Vec = elements.iter().map(|e| self.py_in(e, list)).collect(); - format!("tuple[{}]", elements.join(", ")) + format!("_tuple[{}]", elements.join(", ")) } _ => self.py_ty(ty), } @@ -804,7 +818,7 @@ impl<'a> Gen<'a> { let py = self.py_ty(ty); let _ = write!( self.py, - "class {name}(_Exception):\n\t\"\"\"A `{py}` Rust returned as an error, in `error`, whose `str` is the exception's.\"\"\"\n\n\tdef __init__(self, error: {py}) -> None:\n\t\t_Exception.__init__(self, error)\n\t\tself.error = error\n\n\tdef __str__(self) -> str:\n\t\treturn _str(self.error)\n\n\n" + "class {name}(_Exception):\n\t\"\"\"A `{py}` Rust returned as an error, in `error`, whose `str` is the exception's.\"\"\"\n\n\tdef __init__(self, error: {py}) -> None:\n\t\t_Exception.__init__(self, error)\n\t\tself.error = error\n\n\tdef __str__(self) -> _str:\n\t\treturn _str(self.error)\n\n\n" ); } } @@ -820,7 +834,7 @@ impl<'a> Gen<'a> { let (kind, value) = (self.key("kind"), self.key("value")); let t_bound = self.t(&bound); ( - "\tkind: Bound\n\tvalue: int\n".to_string(), + "\tkind: Bound\n\tvalue: _int\n".to_string(), format!( "let k = rt::field(o, {t}, {kind}, s)?;\n\tlet v = rt::attr(o, {value}, s)?;\n\tOk(crate::{c} {{ kind: rt::member_in(k, {t_bound})?, value: rt::int_in::<{r}>(v)? }})", t = self.t(c), @@ -943,11 +957,11 @@ impl<'a> Gen<'a> { }; if t.display { unary(self, "to_string", "__str__", "", "rt::str_out(ret)".to_string()); - body.native.push("\tdef __str__(self) -> str: ...\n".to_string()); + body.native.push("\tdef __str__(self) -> _str: ...\n".to_string()); } if t.debug { unary(self, "debug", "__repr__", "", "rt::str_out(ret)".to_string()); - body.native.push("\tdef __repr__(self) -> str: ...\n".to_string()); + body.native.push("\tdef __repr__(self) -> _str: ...\n".to_string()); } if object && t.clone { let lower = self.lower(ty, "ret"); @@ -982,10 +996,10 @@ impl<'a> Gen<'a> { ); // Python makes a class that defines `__eq__` alone unhashable, as `Eq` without `Hash` is. body.python.push(format!( - "\tdef __eq__(self, other: object) -> bool:\n\t\treturn _isinstance(other, {class}) and _n.{bare}_eq(self, other)\n" + "\tdef __eq__(self, other: _object) -> _bool:\n\t\treturn _isinstance(other, {class}) and _n.{bare}_eq(self, other)\n" )); self.natives.push(format!( - "@_staticmethod\ndef {bare}_eq(this: {class}, other: {class}, /) -> bool: ...\n" + "@_staticmethod\ndef {bare}_eq(this: {class}, other: {class}, /) -> _bool: ...\n" )); } if t.default { @@ -1329,8 +1343,12 @@ impl<'a> Gen<'a> { self.register(&c, name.clone()); let mut body = Vec::new(); let mut table = vec!["free: Some(rt::free_callback)".to_string()]; + let mut names: Vec = Vec::new(); for (n, method) in callback.methods.iter().enumerate() { - let member = member(&abi::snake(method.name()), &[]); + // A name an earlier method took, its own takes a trailing `_` against. + let reserved: Vec<&str> = names.iter().map(String::as_str).collect(); + let member = member(&abi::snake(method.name()), &reserved); + names.push(member.clone()); crate::lock::function(&mut self.locked, method, &member); let params: Vec = std::iter::once("self".to_string()) .chain( @@ -1382,9 +1400,9 @@ impl<'a> Gen<'a> { /// The Python annotation of a value Rust lends a callback or closure: its bytes copied. fn py_lent(&self, ty: &Type) -> String { match ty.base() { - Type::Slice | Type::Vec | Type::Bytes => "bytes".to_string(), + Type::Slice | Type::Vec | Type::Bytes => "_bytes".to_string(), Type::Option(inner) if matches!(inner.base(), Type::Slice | Type::Vec | Type::Bytes) => { - "bytes | None".to_string() + "_bytes | None".to_string() } _ => self.py_ty(ty), } @@ -1998,18 +2016,22 @@ fn kw(name: &str) -> String { } /// A member's Python name: its snake-case name, with `_` appended if a keyword, and until -/// it is none of `reserved`, such as one Python gives every such class. +/// it is none of `reserved`, such as one Python gives every such class, nor of [`TAKEN`]: +/// a member named as a runtime alias, such as `_int`, would be what a later annotation +/// `-> _int` names in the class body. fn member(snake: &str, reserved: &[&str]) -> String { // A positional field's accessor, `0_get`, is `_0_get`, as its field is `_0`. let digit = snake .starts_with(|c: char| c.is_ascii_digit()) .then(|| format!("_{snake}")); - let snake = digit.as_deref().unwrap_or(snake); - if !reserved.contains(&snake) { - return kw(snake); + // A keyword's `_` first, so a reservation is checked against the name the class emits. + let snake = kw(digit.as_deref().unwrap_or(snake)); + let taken = |name: &str| reserved.contains(&name) || TAKEN.contains(&name); + if !taken(&snake) { + return snake; } let mut name = format!("{snake}_"); - while reserved.contains(&name.as_str()) { + while taken(&name) { name.push('_'); } name @@ -2101,7 +2123,26 @@ fn ieee_eq(class: &str, fields: &[String]) -> String { .map(|f| format!(" and _same(self.{f}, other.{f})")) .collect(); format!( - "\tdef __eq__(self, other: object) -> bool:\n\t\treturn _isinstance(other, {class}){}\n", + "\tdef __eq__(self, other: _object) -> _bool:\n\t\treturn _isinstance(other, {class}){}\n", same.concat() ) } + +#[cfg(test)] +mod tests { + use super::member; + + #[test] + fn member_keeps_clear_of_the_runtime_aliases() { + assert_eq!(member("_int", &[]), "_int_"); + assert_eq!(member("_int", &["_int_"]), "_int__"); + assert_eq!(member("joined", &[]), "joined"); + assert_eq!(member("bytes", &[]), "bytes"); + assert_eq!(member("name", &["name"]), "name_"); + // A callback's `_int` then `_int_` keep apart: the second reserves the first's name. + assert_eq!(member("_int_", &[member("_int", &[]).as_str()]), "_int__"); + // A callback's `async_` then `async` (raw `r#async`) keep apart: the keyword's `_` is added before the check. + assert_eq!(member("async", &[member("async_", &[]).as_str()]), "async__"); + assert_eq!(member("async_", &[member("async", &[]).as_str()]), "async__"); + } +} diff --git a/ffi/tests/api.kt b/ffi/tests/api.kt index 22150ebbc..c5be99878 100644 --- a/ffi/tests/api.kt +++ b/ffi/tests/api.kt @@ -1472,7 +1472,7 @@ fun clash() { check(runBlocking { Clash.Int(1).tagged("t") } == "t Int(1)", "clash tagged") check(Clash.Unit.stamp(2500u).secs() == 2uL && Clash.Unit.stamp(5u) == Stamp.fromMillis(5u), "clash stamp") check(all[16].moment(9u) == Moment.new(3u, 1000u) && Clash.Unit.moment(4000u).millis() == 4000uL, "clash moment") - check(Clash.String("hi").raw().toByteArray().decodeToString() == "hi", "clash bytes") + check(Clash.String("hi").bytes().toByteArray().decodeToString() == "hi", "clash bytes") val out = Mut(0u) check(Clash.Int(4).describe("t", 3u, Shape.SQUARE, out) == "t Int(4) 3 Square" && out.value == 6u, "clash describe") check(Handle.new(5u).use { it.count() } == 5u && runBlocking { Handle.later(6u) }.use { it.count() } == 6u, "an object named as a former runtime class") diff --git a/ffi/tests/api.py b/ffi/tests/api.py index 9e9f1d092..8ad9f3208 100644 --- a/ffi/tests/api.py +++ b/ffi/tests/api.py @@ -1429,6 +1429,7 @@ def raises(error, f, what): path = api.stray() check(os.fsencode(path) == b"\xff", "a path that is not UTF-8 keeps its bytes") check(api.parents([path, os.fsdecode(b"\xfe/\xff")])[1] == os.fsdecode(b"\xfe"), "and crosses back") + check(bytes(api.Clash.String("hi").bytes()) == b"hi", "a method named as a builtin runs") print("values ok") diff --git a/ffi/tests/api_types.py b/ffi/tests/api_types.py index 53df36d2f..cf1a5c16d 100644 --- a/ffi/tests/api_types.py +++ b/ffi/tests/api_types.py @@ -38,6 +38,9 @@ def seek(self, offset: int, whence: int = 0, /) -> int: tail = api.tail(Reader(), 1) +# A method named as a builtin leaves the builtin its annotations name. +clash_bytes: memoryview = api.Clash.String("hi").bytes() + # Closures returning what Rust owns, or raising the exception of their error. def drive(now: api.Instant, waiter: api.Waiter) -> api.Instant | None: diff --git a/ffi/tests/bare_builtins.py b/ffi/tests/bare_builtins.py new file mode 100644 index 000000000..346d209be --- /dev/null +++ b/ffi/tests/bare_builtins.py @@ -0,0 +1,20 @@ +"""Fails when a generated module reads a Python builtin by its bare name. + +Run as `python3 -I bare_builtins.py api.py`. A method or function named as a builtin, such as +`bytes`, shadows that builtin in the class or module that declares it, so the module +reads each builtin through an import alias such as `_bytes` instead. +""" + +import ast +import builtins +import sys + +allowed = {"__name__"} +tree = ast.parse(open(sys.argv[1]).read()) +bare = sorted( + f"{sys.argv[1]}:{n.lineno}: {n.id}" + for n in ast.walk(tree) + if isinstance(n, ast.Name) and isinstance(n.ctx, ast.Load) and hasattr(builtins, n.id) and n.id not in allowed +) +if bare: + sys.exit("a builtin read by its bare name:\n" + "\n".join(bare)) diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index 1dac08c77..0c5a4aee1 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -994,8 +994,9 @@ fn api_python() { } /// The api fixture's Python, and `tests/api_types.py` calling it as a typed caller does, -/// pass mypy and pyright, both strict, at the oldest CPython its extension loads in. Apart -/// from `api_python`, which the runners without the dev shell's checkers run alone. +/// pass mypy and pyright, both strict, at the oldest CPython its extension loads in, and +/// `tests/bare_builtins.py` finds it reading no builtin by its bare name. Apart from +/// `api_python`, which the runners without the dev shell's checkers run alone. #[test] fn api_py_types() { let dir = scratch().join("api").join("python-types"); @@ -1003,6 +1004,12 @@ fn api_py_types() { let caller = dir.join("api_types.py"); step::copy(&api().out.join("python").join("api.py"), &module); step::copy(&manifest_dir().join("tests").join("api_types.py"), &caller); + run( + Command::new(std::env::var_os("PYTHON").unwrap_or_else(|| "python3".into())) + .arg("-I") + .arg(manifest_dir().join("tests").join("bare_builtins.py")) + .arg(&module), + ); // Rooted in `dir`, so `import api` finds the module rather than `tests/api.py`. let config = dir.join("pyrightconfig.json"); step::copy(&manifest_dir().join("tests").join("pyright.json"), &config); diff --git a/quest/m1/ffi-export/python-method-shadow.md b/quest/m1/ffi-export/python-method-shadow.md deleted file mode 100644 index fcaa95701..000000000 --- a/quest/m1/ffi-export/python-method-shadow.md +++ /dev/null @@ -1,12 +0,0 @@ -# [S] Python annotations survive a method named as a builtin - -## Goal - -A method named as a Python builtin, such as `bytes`, `int`, or `list`, no longer shadows that builtin in the annotations of later methods in its class: the stub of `Clash` with a `bytes()` method beside `joined(lent: &[u8])` passes `mypy --strict`. - -## Plan - -- Found by kixel-ai/rust2#777, whose fixture renamed its `Clash::bytes` to `raw` to step around it. -- ✅ Keep this quest (rejected: drop): any crate with a method named like a Python builtin hits it. Maintainer, 2026-11-14. -- ✅ Generated Python refers to a shadowed builtin through an import alias such as `_bytes`, as `_array` is imported (rejected: `builtins.bytes`). Maintainer, 2026-11-14. -- The module header in `ffi/src/python.rs` already imports `_bytes`, `_int`, `_list`, `_float`, `_str`, `_dict`, and `_tuple` from `builtins` for runtime code, but the annotation types (`py_ty` and its kin) still emit bare `bytes`, `int`, `list[...]`. Switch every builtin an annotation names to its alias, adding any missing one (such as `bool` or `object`) to the imports and the locked names, then restore a `bytes` method on a fixture type and type-check it.