Skip to content

Commit 2d1aa0f

Browse files
committed
refactor: Remove explicit use of InvalidExpr
1 parent c5e74d6 commit 2d1aa0f

File tree

4 files changed

+16
-41
lines changed

4 files changed

+16
-41
lines changed

pybind11_stubgen/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
RemoveSelfAnnotation,
4949
ReplaceReadWritePropertyWithField,
5050
RewritePybind11EnumValueRepr,
51-
WrapInvalidExpressionInAnnotated,
51+
WrapInvalidExpressions,
5252
)
5353
from pybind11_stubgen.parser.mixins.parse import (
5454
BaseParser,
@@ -252,7 +252,7 @@ def stub_parser_from_args(args: CLIArgs) -> IParser:
252252
if args.print_invalid_expressions_as_is:
253253
wrap_invalid_expressions = []
254254
else:
255-
wrap_invalid_expressions = [WrapInvalidExpressionInAnnotated]
255+
wrap_invalid_expressions = [WrapInvalidExpressions]
256256

257257
class Parser(
258258
*error_handlers_top, # type: ignore[misc]
@@ -268,7 +268,6 @@ class Parser(
268268
FixMissingEnumMembersAnnotation,
269269
OverridePrintSafeValues,
270270
*numpy_fixes, # type: ignore[misc]
271-
*wrap_invalid_expressions,
272271
FixNumpyDtype,
273272
FixNumpyArrayFlags,
274273
FixCurrentModulePrefixInTypeNames,
@@ -283,6 +282,7 @@ class Parser(
283282
FixRedundantMethodsFromBuiltinObject,
284283
RemoveSelfAnnotation,
285284
FixPybind11EnumStrDoc,
285+
*wrap_invalid_expressions,
286286
ExtractSignaturesFromPybind11Docstrings,
287287
ParserDispatchMixin,
288288
BaseParser,

pybind11_stubgen/parser/mixins/fix.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
TypeVar_,
3737
Value,
3838
)
39-
from pybind11_stubgen.typing_ext import DynamicSize, FixedSize, InvalidExpr
39+
from pybind11_stubgen.typing_ext import DynamicSize, FixedSize
4040

4141
logger = getLogger("pybind11_stubgen")
4242

@@ -867,24 +867,20 @@ def handle_class_member(
867867
return result
868868

869869

870-
class WrapInvalidExpressionInAnnotated(IParser):
870+
class WrapInvalidExpressions(IParser):
871871
def parse_annotation_str(
872872
self, annotation_str: str
873873
) -> ResolvedType | InvalidExpression | Value:
874874
result = super().parse_annotation_str(annotation_str)
875875
if not isinstance(result, InvalidExpression):
876876
return result
877877

878-
self.handle_type(InvalidExpr)
879878
substitute_t = ResolvedType(self.handle_type(Any))
880879
return ResolvedType(
881880
QualifiedName.from_str("typing.Annotated"),
882881
parameters=[
883882
substitute_t,
884-
Value(
885-
repr=repr(InvalidExpr(expr=annotation_str.strip())),
886-
is_print_safe=True,
887-
),
883+
result,
888884
],
889885
)
890886

pybind11_stubgen/typing_ext.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22

3+
from typing import Any
4+
35

46
class FixedSize:
57
def __init__(self, *dim: int):
@@ -25,13 +27,8 @@ def __repr__(self):
2527
)
2628

2729

28-
class InvalidExpr:
29-
def __init__(self, expr: str):
30-
self.expr = expr
31-
32-
def __repr__(self):
33-
return (
34-
f"{self.__module__}."
35-
f"{self.__class__.__qualname__}"
36-
f"({repr(self.expr)})"
37-
)
30+
def InvalidExpr(expr: str) -> Any:
31+
raise RuntimeError(
32+
"The method exists only for annotation purposes in stub files. "
33+
"Should never not be used at runtime"
34+
)

tests/stubs/python-3.12/pybind11-master/numpy-array-wrap-with-annotated/demo/_bindings/flawed_bindings.pyi

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ from __future__ import annotations
22

33
import typing
44

5-
import pybind11_stubgen.typing_ext
6-
75
__all__ = [
86
"Enum",
97
"Unbound",
@@ -20,24 +18,8 @@ class Enum:
2018
class Unbound:
2119
pass
2220

23-
def accept_unbound_enum(
24-
arg0: typing.Annotated[
25-
typing.Any,
26-
pybind11_stubgen.typing_ext.InvalidExpr("(anonymous namespace)::Enum"),
27-
]
28-
) -> int: ...
21+
def accept_unbound_enum(arg0: typing.Annotated[typing.Any, ...]) -> int: ...
2922
def accept_unbound_enum_defaulted(x: Enum = ...) -> int: ...
30-
def accept_unbound_type(
31-
arg0: tuple[
32-
typing.Annotated[
33-
typing.Any,
34-
pybind11_stubgen.typing_ext.InvalidExpr("(anonymous namespace)::Unbound"),
35-
],
36-
int,
37-
]
38-
) -> int: ...
23+
def accept_unbound_type(arg0: tuple[typing.Annotated[typing.Any, ...], int]) -> int: ...
3924
def accept_unbound_type_defaulted(x: Unbound = ...) -> int: ...
40-
def get_unbound_type() -> typing.Annotated[
41-
typing.Any,
42-
pybind11_stubgen.typing_ext.InvalidExpr("(anonymous namespace)::Unbound"),
43-
]: ...
25+
def get_unbound_type() -> typing.Annotated[typing.Any, ...]: ...

0 commit comments

Comments
 (0)