Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use right imports for Annotated #168

Merged
merged 5 commits into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ jobs:
if-no-files-found: ignore

- name: Check error generation
if: ${{ !contains(fromJson('["3.7", "3.8"]'), matrix.python) }}
shell: bash
run: ./tests/check-demo-errors-generation.sh

Expand Down
2 changes: 0 additions & 2 deletions pybind11_stubgen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
FixPybind11EnumStrDoc,
FixRedundantBuiltinsAnnotation,
FixRedundantMethodsFromBuiltinObject,
FixTypingExtTypeNames,
FixTypingTypeNames,
FixValueReprRandomAddress,
OverridePrintSafeValues,
Expand Down Expand Up @@ -228,7 +227,6 @@ class Parser(
FilterTypingModuleAttributes,
FixPEP585CollectionNames,
FixTypingTypeNames,
FixTypingExtTypeNames,
FixMissingFixedSizeImport,
FixMissingEnumMembersAnnotation,
OverridePrintSafeValues,
Expand Down
60 changes: 31 additions & 29 deletions pybind11_stubgen/parser/mixins/fix.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import importlib
import inspect
import re
import sys
import types
from logging import getLogger
from typing import Any
Expand Down Expand Up @@ -323,26 +324,43 @@ class FixTypingTypeNames(IParser):
Identifier,
[
"Annotated",
"Any",
"Buffer",
"Callable",
"Dict",
"Iterator",
"ItemsView",
"Iterable",
"Iterator",
"KeysView",
"List",
"Optional",
"Set",
"Sequence",
"Set",
"Tuple",
"Union",
"ValuesView",
# Old pybind11 annotations were not capitalized
"iterator",
"buffer",
"iterable",
"iterator",
"sequence",
],
)
)
__typing_extensions_names: set[Identifier] = set(
map(
Identifier,
[
"Buffer",
],
)
)

def __init__(self):
super().__init__()
py_version = sys.version_info[:2]
if py_version < (3, 9):
self.__typing_extensions_names.add(Identifier("Annotated"))

def parse_annotation_str(
self, annotation_str: str
Expand All @@ -353,7 +371,12 @@ def parse_annotation_str(

word = result.name[0]
if word in self.__typing_names:
result.name = QualifiedName.from_str(f"typing.{word[0].upper()}{word[1:]}")
package = "typing"
if word in self.__typing_extensions_names:
package = "typing_extensions"
result.name = QualifiedName.from_str(
f"{package}.{word[0].upper()}{word[1:]}"
)
if word == "function" and result.parameters is None:
result.name = QualifiedName.from_str("typing.Callable")
if word in ("object", "handle") and result.parameters is None:
Expand All @@ -362,30 +385,6 @@ def parse_annotation_str(
return result


class FixTypingExtTypeNames(IParser):
__typing_names: set[Identifier] = set(
map(
Identifier,
["buffer", "Buffer"],
)
)

def parse_annotation_str(
self, annotation_str: str
) -> ResolvedType | InvalidExpression | Value:
result = super().parse_annotation_str(annotation_str)
if not isinstance(result, ResolvedType):
return result
assert len(result.name) > 0

word = result.name[0]
if word in self.__typing_names and result.parameters is None:
result.name = QualifiedName.from_str(
f"typing_extensions.{word[0].upper()}{word[1:]}"
)
return result


class FixCurrentModulePrefixInTypeNames(IParser):
def __init__(self):
super().__init__()
Expand Down Expand Up @@ -469,7 +468,10 @@ class FixNumpyArrayDimAnnotation(IParser):
for arr in ["array", "matrix"]
),
}
__annotated_name = QualifiedName.from_str("typing.Annotated")
# NB: Not using full name due to ambiguity `typing.Annotated` vs
# `typing_extension.Annotated` in different python versions
# Rely on later fix by `FixTypingTypeNames`
__annotated_name = QualifiedName.from_str("Annotated")
numpy_primitive_types: set[QualifiedName] = set(
map(
lambda name: QualifiedName.from_str(f"numpy.{name}"),
Expand Down
1 change: 0 additions & 1 deletion tests/check-demo-stubs-generation.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ run_stubgen() {
--output-dir=${STUBS_DIR} \
--numpy-array-wrap-with-annotated \
--ignore-invalid-expressions="\(anonymous namespace\)::(Enum|Unbound)|<demo\._bindings\.flawed_bindings\..*" \
--ignore-unresolved-names="typing\.Annotated" \
--enum-class-locations="ConsoleForegroundColor:demo._bindings.enum" \
--print-safe-value-reprs="Foo\(\d+\)" \
--exit-code
Expand Down
39 changes: 20 additions & 19 deletions tests/stubs/python-3.7/pybind11-master/demo/_bindings/eigen.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ from __future__ import annotations
import numpy
import pybind11_stubgen.typing_ext
import scipy.sparse
import typing_extensions

__all__ = [
"accept_matrix_int",
Expand All @@ -21,39 +22,39 @@ __all__ = [
]

def accept_matrix_int(
arg0: typing.Annotated[
arg0: typing_extensions.Annotated[
numpy.ndarray, numpy.int32, pybind11_stubgen.typing_ext.FixedSize(3, 3)
]
) -> None: ...
def accept_vector_float64(
arg0: typing.Annotated[
arg0: typing_extensions.Annotated[
numpy.ndarray, numpy.float64, pybind11_stubgen.typing_ext.FixedSize(3, 1)
]
) -> None: ...
def dense_matrix_c(
arg0: typing.Annotated[
arg0: typing_extensions.Annotated[
numpy.ndarray, numpy.float32, pybind11_stubgen.typing_ext.DynamicSize("m", "n")
]
) -> typing.Annotated[
) -> typing_extensions.Annotated[
numpy.ndarray, numpy.float32, pybind11_stubgen.typing_ext.DynamicSize("m", "n")
]: ...
def dense_matrix_r(
arg0: typing.Annotated[
arg0: typing_extensions.Annotated[
numpy.ndarray, numpy.float32, pybind11_stubgen.typing_ext.DynamicSize("m", "n")
]
) -> typing.Annotated[
) -> typing_extensions.Annotated[
numpy.ndarray, numpy.float32, pybind11_stubgen.typing_ext.DynamicSize("m", "n")
]: ...
def fixed_mutator_a(
arg0: typing.Annotated[
arg0: typing_extensions.Annotated[
numpy.ndarray,
numpy.float32,
pybind11_stubgen.typing_ext.FixedSize(5, 6),
numpy.ndarray.flags.writeable,
]
) -> None: ...
def fixed_mutator_c(
arg0: typing.Annotated[
arg0: typing_extensions.Annotated[
numpy.ndarray,
numpy.float32,
pybind11_stubgen.typing_ext.FixedSize(5, 6),
Expand All @@ -62,7 +63,7 @@ def fixed_mutator_c(
]
) -> None: ...
def fixed_mutator_r(
arg0: typing.Annotated[
arg0: typing_extensions.Annotated[
numpy.ndarray,
numpy.float32,
pybind11_stubgen.typing_ext.FixedSize(5, 6),
Expand All @@ -71,28 +72,28 @@ def fixed_mutator_r(
]
) -> None: ...
def four_col_matrix_r(
arg0: typing.Annotated[
arg0: typing_extensions.Annotated[
numpy.ndarray, numpy.float32, pybind11_stubgen.typing_ext.DynamicSize("m", 4)
]
) -> typing.Annotated[
) -> typing_extensions.Annotated[
numpy.ndarray, numpy.float32, pybind11_stubgen.typing_ext.DynamicSize("m", 4)
]: ...
def four_row_matrix_r(
arg0: typing.Annotated[
arg0: typing_extensions.Annotated[
numpy.ndarray, numpy.float32, pybind11_stubgen.typing_ext.DynamicSize(4, "n")
]
) -> typing.Annotated[
) -> typing_extensions.Annotated[
numpy.ndarray, numpy.float32, pybind11_stubgen.typing_ext.DynamicSize(4, "n")
]: ...
def get_matrix_int() -> typing.Annotated[
def get_matrix_int() -> typing_extensions.Annotated[
numpy.ndarray, numpy.int32, pybind11_stubgen.typing_ext.FixedSize(3, 3)
]: ...
def get_vector_float64() -> typing.Annotated[
def get_vector_float64() -> typing_extensions.Annotated[
numpy.ndarray, numpy.float64, pybind11_stubgen.typing_ext.FixedSize(3, 1)
]: ...
def sparse_matrix_c(
arg0: typing.Annotated[scipy.sparse.csc_matrix, numpy.float32]
) -> typing.Annotated[scipy.sparse.csc_matrix, numpy.float32]: ...
arg0: typing_extensions.Annotated[scipy.sparse.csc_matrix, numpy.float32]
) -> typing_extensions.Annotated[scipy.sparse.csc_matrix, numpy.float32]: ...
def sparse_matrix_r(
arg0: typing.Annotated[scipy.sparse.csr_matrix, numpy.float32]
) -> typing.Annotated[scipy.sparse.csr_matrix, numpy.float32]: ...
arg0: typing_extensions.Annotated[scipy.sparse.csr_matrix, numpy.float32]
) -> typing_extensions.Annotated[scipy.sparse.csr_matrix, numpy.float32]: ...
13 changes: 9 additions & 4 deletions tests/stubs/python-3.7/pybind11-master/demo/_bindings/numpy.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import numpy
import typing_extensions

__all__ = [
"accept_ndarray_float64",
Expand All @@ -10,8 +11,12 @@ __all__ = [
]

def accept_ndarray_float64(
arg0: typing.Annotated[numpy.ndarray, numpy.float64]
arg0: typing_extensions.Annotated[numpy.ndarray, numpy.float64]
) -> None: ...
def accept_ndarray_int(arg0: typing.Annotated[numpy.ndarray, numpy.int32]) -> None: ...
def get_ndarray_float64() -> typing.Annotated[numpy.ndarray, numpy.float64]: ...
def get_ndarray_int() -> typing.Annotated[numpy.ndarray, numpy.int32]: ...
def accept_ndarray_int(
arg0: typing_extensions.Annotated[numpy.ndarray, numpy.int32]
) -> None: ...
def get_ndarray_float64() -> typing_extensions.Annotated[
numpy.ndarray, numpy.float64
]: ...
def get_ndarray_int() -> typing_extensions.Annotated[numpy.ndarray, numpy.int32]: ...
9 changes: 7 additions & 2 deletions tests/stubs/python-3.7/pybind11-master/demo/_bindings/stl.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ from __future__ import annotations
import typing

import pybind11_stubgen.typing_ext
import typing_extensions

__all__ = ["std_array", "std_map", "std_optional", "std_variant", "std_vector"]

def std_array(
arg0: typing.Annotated[list[int], pybind11_stubgen.typing_ext.FixedSize(3)]
) -> typing.Annotated[list[int], pybind11_stubgen.typing_ext.FixedSize(3)]: ...
arg0: typing_extensions.Annotated[
list[int], pybind11_stubgen.typing_ext.FixedSize(3)
]
) -> typing_extensions.Annotated[
list[int], pybind11_stubgen.typing_ext.FixedSize(3)
]: ...
def std_map() -> dict[int, complex]: ...
def std_optional(arg0: int | None) -> None: ...
def std_variant(arg0: int | float | tuple[int, int]) -> None: ...
Expand Down
39 changes: 20 additions & 19 deletions tests/stubs/python-3.8/pybind11-master/demo/_bindings/eigen.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ from __future__ import annotations
import numpy
import pybind11_stubgen.typing_ext
import scipy.sparse
import typing_extensions

__all__ = [
"accept_matrix_int",
Expand All @@ -21,39 +22,39 @@ __all__ = [
]

def accept_matrix_int(
arg0: typing.Annotated[
arg0: typing_extensions.Annotated[
numpy.ndarray, numpy.int32, pybind11_stubgen.typing_ext.FixedSize(3, 3)
]
) -> None: ...
def accept_vector_float64(
arg0: typing.Annotated[
arg0: typing_extensions.Annotated[
numpy.ndarray, numpy.float64, pybind11_stubgen.typing_ext.FixedSize(3, 1)
]
) -> None: ...
def dense_matrix_c(
arg0: typing.Annotated[
arg0: typing_extensions.Annotated[
numpy.ndarray, numpy.float32, pybind11_stubgen.typing_ext.DynamicSize("m", "n")
]
) -> typing.Annotated[
) -> typing_extensions.Annotated[
numpy.ndarray, numpy.float32, pybind11_stubgen.typing_ext.DynamicSize("m", "n")
]: ...
def dense_matrix_r(
arg0: typing.Annotated[
arg0: typing_extensions.Annotated[
numpy.ndarray, numpy.float32, pybind11_stubgen.typing_ext.DynamicSize("m", "n")
]
) -> typing.Annotated[
) -> typing_extensions.Annotated[
numpy.ndarray, numpy.float32, pybind11_stubgen.typing_ext.DynamicSize("m", "n")
]: ...
def fixed_mutator_a(
arg0: typing.Annotated[
arg0: typing_extensions.Annotated[
numpy.ndarray,
numpy.float32,
pybind11_stubgen.typing_ext.FixedSize(5, 6),
numpy.ndarray.flags.writeable,
]
) -> None: ...
def fixed_mutator_c(
arg0: typing.Annotated[
arg0: typing_extensions.Annotated[
numpy.ndarray,
numpy.float32,
pybind11_stubgen.typing_ext.FixedSize(5, 6),
Expand All @@ -62,7 +63,7 @@ def fixed_mutator_c(
]
) -> None: ...
def fixed_mutator_r(
arg0: typing.Annotated[
arg0: typing_extensions.Annotated[
numpy.ndarray,
numpy.float32,
pybind11_stubgen.typing_ext.FixedSize(5, 6),
Expand All @@ -71,28 +72,28 @@ def fixed_mutator_r(
]
) -> None: ...
def four_col_matrix_r(
arg0: typing.Annotated[
arg0: typing_extensions.Annotated[
numpy.ndarray, numpy.float32, pybind11_stubgen.typing_ext.DynamicSize("m", 4)
]
) -> typing.Annotated[
) -> typing_extensions.Annotated[
numpy.ndarray, numpy.float32, pybind11_stubgen.typing_ext.DynamicSize("m", 4)
]: ...
def four_row_matrix_r(
arg0: typing.Annotated[
arg0: typing_extensions.Annotated[
numpy.ndarray, numpy.float32, pybind11_stubgen.typing_ext.DynamicSize(4, "n")
]
) -> typing.Annotated[
) -> typing_extensions.Annotated[
numpy.ndarray, numpy.float32, pybind11_stubgen.typing_ext.DynamicSize(4, "n")
]: ...
def get_matrix_int() -> typing.Annotated[
def get_matrix_int() -> typing_extensions.Annotated[
numpy.ndarray, numpy.int32, pybind11_stubgen.typing_ext.FixedSize(3, 3)
]: ...
def get_vector_float64() -> typing.Annotated[
def get_vector_float64() -> typing_extensions.Annotated[
numpy.ndarray, numpy.float64, pybind11_stubgen.typing_ext.FixedSize(3, 1)
]: ...
def sparse_matrix_c(
arg0: typing.Annotated[scipy.sparse.csc_matrix, numpy.float32]
) -> typing.Annotated[scipy.sparse.csc_matrix, numpy.float32]: ...
arg0: typing_extensions.Annotated[scipy.sparse.csc_matrix, numpy.float32]
) -> typing_extensions.Annotated[scipy.sparse.csc_matrix, numpy.float32]: ...
def sparse_matrix_r(
arg0: typing.Annotated[scipy.sparse.csr_matrix, numpy.float32]
) -> typing.Annotated[scipy.sparse.csr_matrix, numpy.float32]: ...
arg0: typing_extensions.Annotated[scipy.sparse.csr_matrix, numpy.float32]
) -> typing_extensions.Annotated[scipy.sparse.csr_matrix, numpy.float32]: ...
Loading