mirror of
https://github.com/python/cpython.git
synced 2025-08-31 05:58:33 +00:00
inspect: Make Signature and Parameter hashable. Issue #20334.
This commit is contained in:
parent
3f73ca23cf
commit
67ae50ee1c
5 changed files with 52 additions and 13 deletions
|
@ -2231,6 +2231,16 @@ class Parameter:
|
|||
return '<{} at {:#x} "{}">'.format(self.__class__.__name__,
|
||||
id(self), self)
|
||||
|
||||
def __hash__(self):
|
||||
hash_tuple = (self.name, int(self.kind))
|
||||
|
||||
if self._annotation is not _empty:
|
||||
hash_tuple += (self._annotation,)
|
||||
if self._default is not _empty:
|
||||
hash_tuple += (self._default,)
|
||||
|
||||
return hash(hash_tuple)
|
||||
|
||||
def __eq__(self, other):
|
||||
return (issubclass(other.__class__, Parameter) and
|
||||
self._name == other._name and
|
||||
|
@ -2524,6 +2534,12 @@ class Signature:
|
|||
return type(self)(parameters,
|
||||
return_annotation=return_annotation)
|
||||
|
||||
def __hash__(self):
|
||||
hash_tuple = tuple(self.parameters.values())
|
||||
if self._return_annotation is not _empty:
|
||||
hash_tuple += (self._return_annotation,)
|
||||
return hash(hash_tuple)
|
||||
|
||||
def __eq__(self, other):
|
||||
if (not issubclass(type(other), Signature) or
|
||||
self.return_annotation != other.return_annotation or
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue