Skip to content

Commit 491aef6

Browse files
Update dependencies, module constructor and stubgen (#14)
* Update dependencies * Improve module constructor * Improve stub generator
1 parent 1a4e724 commit 491aef6

File tree

6 files changed

+25
-19
lines changed

6 files changed

+25
-19
lines changed

requirements.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
AMULET_COMPILER_TARGET_REQUIREMENT = "==2.0"
66

7-
PYBIND11_REQUIREMENT = "==3.0.0"
8-
AMULET_PYBIND11_EXTENSIONS_REQUIREMENT = "~=1.2.0.0a0"
7+
PYBIND11_REQUIREMENT = "==3.0.1"
8+
AMULET_PYBIND11_EXTENSIONS_REQUIREMENT = "~=1.2.0.0a2"
99
AMULET_IO_REQUIREMENT = "~=1.0"
10-
AMULET_UTILS_REQUIREMENT = "~=1.1.3.0a0"
10+
AMULET_UTILS_REQUIREMENT = "~=1.1.3.0a6"
1111
AMULET_ZLIB_REQUIREMENT = "~=1.0.8.0a0"
12-
AMULET_NBT_REQUIREMENT = "~=5.0.2.0a0"
12+
AMULET_NBT_REQUIREMENT = "~=5.0.2.0a2"
1313

1414
if os.environ.get("AMULET_PYBIND11_EXTENSIONS_REQUIREMENT", None):
1515
AMULET_PYBIND11_EXTENSIONS_REQUIREMENT = f"{AMULET_PYBIND11_EXTENSIONS_REQUIREMENT},{os.environ['AMULET_PYBIND11_EXTENSIONS_REQUIREMENT']}"

src/amulet/anvil/_amulet_anvil.py.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,5 @@ void init_module(py::module m)
2828

2929
PYBIND11_MODULE(_amulet_anvil, m)
3030
{
31-
py::options options;
32-
options.disable_function_signatures();
33-
m.def("init", &init_module, py::doc("init(arg0: types.ModuleType) -> None"));
34-
options.enable_function_signatures();
31+
m.def("init", &init_module, py::arg("m"));
3532
}

src/amulet/anvil/_amulet_anvil.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ import types
44

55
__all__: list[str] = ["init"]
66

7-
def init(arg0: types.ModuleType) -> None: ...
7+
def init(m: types.ModuleType) -> None: ...

tests/test_amulet_anvil/_test_amulet_anvil.py.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,5 @@ void init_module(py::module m){
1515
}
1616

1717
PYBIND11_MODULE(_test_amulet_anvil, m) {
18-
py::options options;
19-
options.disable_function_signatures();
20-
m.def("init", &init_module, py::doc("init(arg0: types.ModuleType) -> None"));
21-
options.enable_function_signatures();
18+
m.def("init", &init_module, py::arg("m"));
2219
}

tests/test_amulet_anvil/_test_amulet_anvil.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ import types
44

55
__all__: list[str] = ["init"]
66

7-
def init(arg0: types.ModuleType) -> None: ...
7+
def init(m: types.ModuleType) -> None: ...

tools/generate_pybind_stubs.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,28 @@
77
import pybind11_stubgen
88
from pybind11_stubgen.structs import Identifier
99
from pybind11_stubgen.parser.mixins.filter import FilterClassMembers
10-
from pybind11_stubgen import main as pybind11_stubgen_main
10+
11+
12+
ForwardRefPattern = re.compile(r"ForwardRef\('(?P<variable>[a-zA-Z_][a-zA-Z0-9_]*)'\)")
13+
14+
QuotePattern = re.compile(r"'(?P<variable>[a-zA-Z_][a-zA-Z0-9_]*)'")
15+
16+
17+
def fix_value(value: str) -> str:
18+
value = value.replace("NoneType", "None")
19+
value = ForwardRefPattern.sub(lambda match: match.group("variable"), value)
20+
value = QuotePattern.sub(lambda match: match.group("variable"), value)
21+
return value
22+
1123

1224
UnionPattern = re.compile(
13-
r"^(?P<variable>[a-zA-Z_][a-zA-Z0-9_]*): types\.UnionType\s*#\s*value = (?P<value>.*)$",
25+
r"^(?P<variable>[a-zA-Z_][a-zA-Z0-9_]*): (types\.UnionType|typing\._UnionGenericAlias)\s*#\s*value = (?P<value>.*)$",
1426
flags=re.MULTILINE,
1527
)
1628

1729

1830
def union_sub_func(match: re.Match[str]) -> str:
19-
return f'{match.group("variable")}: typing.TypeAlias = {match.group("value")}'
31+
return f'{match.group("variable")}: typing.TypeAlias = {fix_value(match.group("value"))}'
2032

2133

2234
ClassVarUnionPattern = re.compile(
@@ -26,7 +38,7 @@ def union_sub_func(match: re.Match[str]) -> str:
2638

2739

2840
def class_var_union_sub_func(match: re.Match) -> str:
29-
return f'{match.group("variable")}: typing.TypeAlias = {match.group("value")}'
41+
return f'{match.group("variable")}: typing.TypeAlias = {fix_value(match.group("value"))}'
3042

3143

3244
VersionPattern = re.compile(r"(?P<var>[a-zA-Z0-9_].*): str = '.*?'")
@@ -87,7 +99,7 @@ def eq_sub_func(match: re.Match[str]) -> str:
8799

88100

89101
def generic_alias_sub_func(match: re.Match) -> str:
90-
return f"{match.group('variable')}: typing.TypeAlias = {match.group('value')}"
102+
return f'{match.group("variable")}: typing.TypeAlias = {fix_value(match.group("value"))}'
91103

92104

93105
def get_module_path(name: str) -> str:

0 commit comments

Comments
 (0)