Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(159142)

Unified Diff: Lib/inspect.py

Issue 20334: make inspect Signature hashable
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | Lib/test/test_inspect.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -2239,14 +2239,7 @@
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)
+ return hash((self.name, self.kind, self._annotation, self._default))
def __eq__(self, other):
return (issubclass(other.__class__, Parameter) and
@@ -2541,41 +2534,20 @@
return type(self)(parameters,
return_annotation=return_annotation)
+ def _hash_helper(self):
+ return (tuple(param for param in self.parameters.values()
+ if param.kind != _KEYWORD_ONLY),
+ {param.name: param for param in self.parameters.values()
+ if param.kind == _KEYWORD_ONLY},
+ self._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)
+ params, kwo_params, return_annotation = self._hash_helper()
+ return hash((params, frozenset(kwo_params.items()), return_annotation))
def __eq__(self, other):
- if (not issubclass(type(other), Signature) or
- self.return_annotation != other.return_annotation or
- len(self.parameters) != len(other.parameters)):
- return False
-
- other_positions = {param: idx
- for idx, param in enumerate(other.parameters.keys())}
-
- for idx, (param_name, param) in enumerate(self.parameters.items()):
- if param.kind == _KEYWORD_ONLY:
- try:
- other_param = other.parameters[param_name]
- except KeyError:
- return False
- else:
- if param != other_param:
- return False
- else:
- try:
- other_idx = other_positions[param_name]
- except KeyError:
- return False
- else:
- if (idx != other_idx or
- param != other.parameters[param_name]):
- return False
-
- return True
+ return (isinstance(other, Signature) and
+ self._hash_helper() == other._hash_helper())
def __ne__(self, other):
return not self.__eq__(other)
« no previous file with comments | « no previous file | Lib/test/test_inspect.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+