Skip to content

Commit 6effa8c

Browse files
authored
Add special type hinting for __hash__ to declare class as unhashable (#110)
1 parent cdeb08d commit 6effa8c

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

pybind11_stubgen/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,10 @@ def is_safe_to_use_repr(self, value):
479479
return False
480480

481481
def to_lines(self): # type: () -> List[str]
482+
# special case for __hash__ to mark the surrounding class as unhashable
483+
if self.name == "__hash__" and self.attr is None:
484+
return ["__hash__: typing.ClassVar[None] = None"]
485+
482486
if self.is_safe_to_use_repr(self.attr):
483487
return ["{name} = {repr}".format(name=self.name, repr=repr(self.attr))]
484488

tests/stubs/expected/cpp_library_bindings/_core/opaque_types/__init__.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class VectorPairStringDouble:
118118
"""
119119
Remove the first item from the list whose value is x. It is an error if there is no such item.
120120
"""
121-
__hash__ = None
121+
__hash__: typing.ClassVar[None] = None
122122
pass
123123

124124
def get_complex_map() -> MapStringComplex:

0 commit comments

Comments
 (0)