From 692b42d6be44e629b6e4f6438ae8950b308314a3 Mon Sep 17 00:00:00 2001 From: Aaron Miller <78561124+aaron-skydio@users.noreply.github.com> Date: Thu, 19 Dec 2024 18:46:28 -0800 Subject: [PATCH] Fix `__hash__` type annotation Annotating the type of this as `None` like is currently done causes mypy to generate an error, like this: ``` hash.py:2: error: Incompatible types in assignment (expression has type "None", base class "object" defined the type as "Callable[[object], int]") ``` https://github.com/python/mypy/issues/4266 Annotating this as `ClassVar[None]` seems to work. My other attempt of annotating as e.g. `Optional[Callable]` doesn't --- pybind11_stubgen/parser/mixins/fix.py | 2 +- .../numpy-array-use-type-var/demo/_bindings/stl_bind.pyi | 2 +- .../numpy-array-wrap-with-annotated/demo/_bindings/stl_bind.pyi | 2 +- .../numpy-array-wrap-with-annotated/demo/_bindings/stl_bind.pyi | 2 +- .../numpy-array-wrap-with-annotated/demo/_bindings/stl_bind.pyi | 2 +- .../numpy-array-wrap-with-annotated/demo/_bindings/stl_bind.pyi | 2 +- .../numpy-array-wrap-with-annotated/demo/_bindings/stl_bind.pyi | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pybind11_stubgen/parser/mixins/fix.py b/pybind11_stubgen/parser/mixins/fix.py index 782b7de..c1f5ee1 100644 --- a/pybind11_stubgen/parser/mixins/fix.py +++ b/pybind11_stubgen/parser/mixins/fix.py @@ -307,7 +307,7 @@ def handle_field(self, path: QualifiedName, field: Any) -> Field | None: return None if field is None and path[-1] == "__hash__": result.attribute.annotation = self.parse_annotation_str( - "typing.ClassVar[None]" + "typing.ClassVar[typing.Any]" ) return result diff --git a/tests/stubs/python-3.12/pybind11-master/numpy-array-use-type-var/demo/_bindings/stl_bind.pyi b/tests/stubs/python-3.12/pybind11-master/numpy-array-use-type-var/demo/_bindings/stl_bind.pyi index 3362b98..3a63997 100644 --- a/tests/stubs/python-3.12/pybind11-master/numpy-array-use-type-var/demo/_bindings/stl_bind.pyi +++ b/tests/stubs/python-3.12/pybind11-master/numpy-array-use-type-var/demo/_bindings/stl_bind.pyi @@ -33,7 +33,7 @@ class MapStringComplex: def values(self) -> typing.ValuesView: ... class VectorPairStringDouble: - __hash__: typing.ClassVar[None] = None + __hash__: typing.ClassVar[typing.Any] = None def __bool__(self) -> bool: """ Check whether the list is nonempty diff --git a/tests/stubs/python-3.12/pybind11-master/numpy-array-wrap-with-annotated/demo/_bindings/stl_bind.pyi b/tests/stubs/python-3.12/pybind11-master/numpy-array-wrap-with-annotated/demo/_bindings/stl_bind.pyi index 3362b98..3a63997 100644 --- a/tests/stubs/python-3.12/pybind11-master/numpy-array-wrap-with-annotated/demo/_bindings/stl_bind.pyi +++ b/tests/stubs/python-3.12/pybind11-master/numpy-array-wrap-with-annotated/demo/_bindings/stl_bind.pyi @@ -33,7 +33,7 @@ class MapStringComplex: def values(self) -> typing.ValuesView: ... class VectorPairStringDouble: - __hash__: typing.ClassVar[None] = None + __hash__: typing.ClassVar[typing.Any] = None def __bool__(self) -> bool: """ Check whether the list is nonempty diff --git a/tests/stubs/python-3.12/pybind11-v2.11/numpy-array-wrap-with-annotated/demo/_bindings/stl_bind.pyi b/tests/stubs/python-3.12/pybind11-v2.11/numpy-array-wrap-with-annotated/demo/_bindings/stl_bind.pyi index c6bb21c..3eccfab 100644 --- a/tests/stubs/python-3.12/pybind11-v2.11/numpy-array-wrap-with-annotated/demo/_bindings/stl_bind.pyi +++ b/tests/stubs/python-3.12/pybind11-v2.11/numpy-array-wrap-with-annotated/demo/_bindings/stl_bind.pyi @@ -33,7 +33,7 @@ class MapStringComplex: def values(self) -> typing.ValuesView[complex]: ... class VectorPairStringDouble: - __hash__: typing.ClassVar[None] = None + __hash__: typing.ClassVar[typing.Any] = None def __bool__(self) -> bool: """ Check whether the list is nonempty diff --git a/tests/stubs/python-3.12/pybind11-v2.9/numpy-array-wrap-with-annotated/demo/_bindings/stl_bind.pyi b/tests/stubs/python-3.12/pybind11-v2.9/numpy-array-wrap-with-annotated/demo/_bindings/stl_bind.pyi index 7abf41f..22f7eb4 100644 --- a/tests/stubs/python-3.12/pybind11-v2.9/numpy-array-wrap-with-annotated/demo/_bindings/stl_bind.pyi +++ b/tests/stubs/python-3.12/pybind11-v2.9/numpy-array-wrap-with-annotated/demo/_bindings/stl_bind.pyi @@ -33,7 +33,7 @@ class MapStringComplex: def values(self) -> typing.ValuesView[MapStringComplex]: ... class VectorPairStringDouble: - __hash__: typing.ClassVar[None] = None + __hash__: typing.ClassVar[typing.Any] = None def __bool__(self) -> bool: """ Check whether the list is nonempty diff --git a/tests/stubs/python-3.7/pybind11-master/numpy-array-wrap-with-annotated/demo/_bindings/stl_bind.pyi b/tests/stubs/python-3.7/pybind11-master/numpy-array-wrap-with-annotated/demo/_bindings/stl_bind.pyi index 3362b98..3a63997 100644 --- a/tests/stubs/python-3.7/pybind11-master/numpy-array-wrap-with-annotated/demo/_bindings/stl_bind.pyi +++ b/tests/stubs/python-3.7/pybind11-master/numpy-array-wrap-with-annotated/demo/_bindings/stl_bind.pyi @@ -33,7 +33,7 @@ class MapStringComplex: def values(self) -> typing.ValuesView: ... class VectorPairStringDouble: - __hash__: typing.ClassVar[None] = None + __hash__: typing.ClassVar[typing.Any] = None def __bool__(self) -> bool: """ Check whether the list is nonempty diff --git a/tests/stubs/python-3.8/pybind11-master/numpy-array-wrap-with-annotated/demo/_bindings/stl_bind.pyi b/tests/stubs/python-3.8/pybind11-master/numpy-array-wrap-with-annotated/demo/_bindings/stl_bind.pyi index 3362b98..3a63997 100644 --- a/tests/stubs/python-3.8/pybind11-master/numpy-array-wrap-with-annotated/demo/_bindings/stl_bind.pyi +++ b/tests/stubs/python-3.8/pybind11-master/numpy-array-wrap-with-annotated/demo/_bindings/stl_bind.pyi @@ -33,7 +33,7 @@ class MapStringComplex: def values(self) -> typing.ValuesView: ... class VectorPairStringDouble: - __hash__: typing.ClassVar[None] = None + __hash__: typing.ClassVar[typing.Any] = None def __bool__(self) -> bool: """ Check whether the list is nonempty