Skip to content

Commit 5d76c3e

Browse files
committed
Lupa stubs:
Support get/set/del from Lua tables. Lua table values are all Any because we can't typecheck across language boundaries. Support unpack_returned_tuples in the LuaRuntime constructor. # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # # Date: Tue Jul 1 16:43:21 2025 -0700 # # On branch main # Your branch is ahead of 'origin/main' by 1 commit. # (use "git push" to publish your local commits) # # Changes to be committed: # modified: lua51.pyi # modified: lua52.pyi # modified: lua53.pyi # modified: lua54.pyi #
1 parent d270bb0 commit 5d76c3e

File tree

4 files changed

+80
-52
lines changed

4 files changed

+80
-52
lines changed

stubs/lupa/lupa/lua51.pyi

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from _typeshed import MaybeNone
2-
from collections.abc import Callable, Iterator
2+
from collections.abc import Callable, Iterable
33
from typing import Any, Final, Generic, TypeVar, type_check_only
4+
from typing_extensions import TypeAlias
45

56
__all__ = [
67
"LUA_VERSION",
@@ -34,22 +35,29 @@ unpacks_lua_table_method: Callable[[Callable[..., Any]], Callable[..., Any]]
3435

3536
# inner classes
3637

37-
@type_check_only
38-
class _LuaIter:
39-
def __iter__(self) -> Iterator[object]: ...
40-
4138
@type_check_only
4239
class _LuaTable:
43-
def keys(self) -> _LuaIter: ...
44-
def values(self) -> _LuaIter: ...
45-
def items(self) -> _LuaIter: ...
40+
def keys(self) -> Iterable[_LuaKey]: ...
41+
def values(self) -> Iterable[_LuaObject]: ...
42+
def items(self) -> Iterable[tuple[_LuaKey, _LuaObject]]: ...
43+
def __getitem__(self, key: _LuaKey) -> _LuaObject: ...
44+
def __setitem__(self, key: _LuaKey, value: _LuaObject) -> None: ...
45+
def __delitem__(self, key: _LuaKey) -> None: ...
46+
47+
# A Lua object can be a table or a primitive type. Because we have no way of
48+
# knowing the actual type across languages, we simply use an Any for a Lua
49+
# object.
50+
51+
# A previous version of this code had
52+
# _LuaObject: TypeAlias = _LuaTable | int | str | float | bool | None
53+
# but that causes false type failures when running, e.g., `lua.globals()['foo']['bar']`
54+
# (because `lua.globals()['foo']` is not known to be a nested table
55+
_LuaKey: TypeAlias = str | int
56+
_LuaObject: TypeAlias = Any
4657

4758
@type_check_only
4859
class _LuaNoGC: ...
4960

50-
@type_check_only
51-
class _LuaObject: ...
52-
5361
# classes
5462

5563
_bint = TypeVar("_bint", bool, int)
@@ -70,8 +78,7 @@ class LuaRuntime:
7078
lua_implementation: Final[str]
7179
lua_version: Final[tuple[int, int]]
7280

73-
# @classmethod
74-
# def __cinit__(cls, unpack_return_tuples: bool) -> None: ...
81+
def __init__(self, /, unpack_returned_tuples: bool) -> None: ...
7582
# def add_pending_unref(self, ref: int) -> None: ...
7683
# def clean_up_pending_unrefs(self) -> int: ...
7784
def get_max_memory(self, total: bool = False) -> int | MaybeNone: ...

stubs/lupa/lupa/lua52.pyi

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from _typeshed import MaybeNone
2-
from collections.abc import Callable, Iterator
2+
from collections.abc import Callable, Iterable
33
from typing import Any, Final, Generic, TypeVar, type_check_only
4+
from typing_extensions import TypeAlias
45

56
__all__ = [
67
"LUA_VERSION",
@@ -34,22 +35,29 @@ unpacks_lua_table_method: Callable[[Callable[..., Any]], Callable[..., Any]]
3435

3536
# inner classes
3637

37-
@type_check_only
38-
class _LuaIter:
39-
def __iter__(self) -> Iterator[object]: ...
40-
4138
@type_check_only
4239
class _LuaTable:
43-
def keys(self) -> _LuaIter: ...
44-
def values(self) -> _LuaIter: ...
45-
def items(self) -> _LuaIter: ...
40+
def keys(self) -> Iterable[_LuaKey]: ...
41+
def values(self) -> Iterable[_LuaObject]: ...
42+
def items(self) -> Iterable[tuple[_LuaKey, _LuaObject]]: ...
43+
def __getitem__(self, key: _LuaKey) -> _LuaObject: ...
44+
def __setitem__(self, key: _LuaKey, value: _LuaObject) -> None: ...
45+
def __delitem__(self, key: _LuaKey) -> None: ...
46+
47+
# A Lua object can be a table or a primitive type. Because we have no way of
48+
# knowing the actual type across languages, we simply use an Any for a Lua
49+
# object.
50+
51+
# A previous version of this code had
52+
# _LuaObject: TypeAlias = _LuaTable | int | str | float | bool | None
53+
# but that causes false type failures when running, e.g., `lua.globals()['foo']['bar']`
54+
# (because `lua.globals()['foo']` is not known to be a nested table
55+
_LuaKey: TypeAlias = str | int
56+
_LuaObject: TypeAlias = Any
4657

4758
@type_check_only
4859
class _LuaNoGC: ...
4960

50-
@type_check_only
51-
class _LuaObject: ...
52-
5361
# classes
5462

5563
_bint = TypeVar("_bint", bool, int)
@@ -70,8 +78,7 @@ class LuaRuntime:
7078
lua_implementation: Final[str]
7179
lua_version: Final[tuple[int, int]]
7280

73-
# @classmethod
74-
# def __cinit__(cls, unpack_return_tuples: bool) -> None: ...
81+
def __init__(self, /, unpack_returned_tuples: bool) -> None: ...
7582
# def add_pending_unref(self, ref: int) -> None: ...
7683
# def clean_up_pending_unrefs(self) -> int: ...
7784
def get_max_memory(self, total: bool = False) -> int | MaybeNone: ...

stubs/lupa/lupa/lua53.pyi

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from _typeshed import MaybeNone
2-
from collections.abc import Callable, Iterator
2+
from collections.abc import Callable, Iterable
33
from typing import Any, Final, Generic, TypeVar, type_check_only
4+
from typing_extensions import TypeAlias
45

56
__all__ = [
67
"LUA_VERSION",
@@ -34,22 +35,29 @@ unpacks_lua_table_method: Callable[[Callable[..., Any]], Callable[..., Any]]
3435

3536
# inner classes
3637

37-
@type_check_only
38-
class _LuaIter:
39-
def __iter__(self) -> Iterator[object]: ...
40-
4138
@type_check_only
4239
class _LuaTable:
43-
def keys(self) -> _LuaIter: ...
44-
def values(self) -> _LuaIter: ...
45-
def items(self) -> _LuaIter: ...
40+
def keys(self) -> Iterable[_LuaKey]: ...
41+
def values(self) -> Iterable[_LuaObject]: ...
42+
def items(self) -> Iterable[tuple[_LuaKey, _LuaObject]]: ...
43+
def __getitem__(self, key: _LuaKey) -> _LuaObject: ...
44+
def __setitem__(self, key: _LuaKey, value: _LuaObject) -> None: ...
45+
def __delitem__(self, key: _LuaKey) -> None: ...
46+
47+
# A Lua object can be a table or a primitive type. Because we have no way of
48+
# knowing the actual type across languages, we simply use an Any for a Lua
49+
# object.
50+
51+
# A previous version of this code had
52+
# _LuaObject: TypeAlias = _LuaTable | int | str | float | bool | None
53+
# but that causes false type failures when running, e.g., `lua.globals()['foo']['bar']`
54+
# (because `lua.globals()['foo']` is not known to be a nested table
55+
_LuaKey: TypeAlias = str | int
56+
_LuaObject: TypeAlias = Any
4657

4758
@type_check_only
4859
class _LuaNoGC: ...
4960

50-
@type_check_only
51-
class _LuaObject: ...
52-
5361
# classes
5462

5563
_bint = TypeVar("_bint", bool, int)
@@ -70,8 +78,7 @@ class LuaRuntime:
7078
lua_implementation: Final[str]
7179
lua_version: Final[tuple[int, int]]
7280

73-
# @classmethod
74-
# def __cinit__(cls, unpack_return_tuples: bool) -> None: ...
81+
def __init__(self, /, unpack_returned_tuples: bool) -> None: ...
7582
# def add_pending_unref(self, ref: int) -> None: ...
7683
# def clean_up_pending_unrefs(self) -> int: ...
7784
def get_max_memory(self, total: bool = False) -> int | MaybeNone: ...

stubs/lupa/lupa/lua54.pyi

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from _typeshed import MaybeNone
2-
from collections.abc import Callable, Iterator
2+
from collections.abc import Callable, Iterable
33
from typing import Any, Final, Generic, TypeVar, type_check_only
4+
from typing_extensions import TypeAlias
45

56
__all__ = [
67
"LUA_VERSION",
@@ -34,22 +35,29 @@ unpacks_lua_table_method: Callable[[Callable[..., Any]], Callable[..., Any]]
3435

3536
# inner classes
3637

37-
@type_check_only
38-
class _LuaIter:
39-
def __iter__(self) -> Iterator[object]: ...
40-
4138
@type_check_only
4239
class _LuaTable:
43-
def keys(self) -> _LuaIter: ...
44-
def values(self) -> _LuaIter: ...
45-
def items(self) -> _LuaIter: ...
40+
def keys(self) -> Iterable[_LuaKey]: ...
41+
def values(self) -> Iterable[_LuaObject]: ...
42+
def items(self) -> Iterable[tuple[_LuaKey, _LuaObject]]: ...
43+
def __getitem__(self, key: _LuaKey) -> _LuaObject: ...
44+
def __setitem__(self, key: _LuaKey, value: _LuaObject) -> None: ...
45+
def __delitem__(self, key: _LuaKey) -> None: ...
46+
47+
# A Lua object can be a table or a primitive type. Because we have no way of
48+
# knowing the actual type across languages, we simply use an Any for a Lua
49+
# object.
50+
51+
# A previous version of this code had
52+
# _LuaObject: TypeAlias = _LuaTable | int | str | float | bool | None
53+
# but that causes false type failures when running, e.g., `lua.globals()['foo']['bar']`
54+
# (because `lua.globals()['foo']` is not known to be a nested table
55+
_LuaKey: TypeAlias = str | int
56+
_LuaObject: TypeAlias = Any
4657

4758
@type_check_only
4859
class _LuaNoGC: ...
4960

50-
@type_check_only
51-
class _LuaObject: ...
52-
5361
# classes
5462

5563
_bint = TypeVar("_bint", bool, int)
@@ -70,8 +78,7 @@ class LuaRuntime:
7078
lua_implementation: Final[str]
7179
lua_version: Final[tuple[int, int]]
7280

73-
# @classmethod
74-
# def __cinit__(cls, unpack_return_tuples: bool) -> None: ...
81+
def __init__(self, /, unpack_returned_tuples: bool) -> None: ...
7582
# def add_pending_unref(self, ref: int) -> None: ...
7683
# def clean_up_pending_unrefs(self) -> int: ...
7784
def get_max_memory(self, total: bool = False) -> int | MaybeNone: ...

0 commit comments

Comments
 (0)