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

Side by Side Diff: Lib/inspect.py

Issue 20334: make inspect Signature hashable
Patch Set: Created 5 years, 6 months ago
Left:
Right:
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 unified diff | Download patch
« no previous file with comments | « no previous file | Lib/test/test_inspect.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 """Get useful information from live Python objects. 1 """Get useful information from live Python objects.
2 2
3 This module encapsulates the interface provided by the internal special 3 This module encapsulates the interface provided by the internal special
4 attributes (co_*, im_*, tb_*, etc.) in a friendlier fashion. 4 attributes (co_*, im_*, tb_*, etc.) in a friendlier fashion.
5 It also provides some help for examining source code and class layout. 5 It also provides some help for examining source code and class layout.
6 6
7 Here are some of the useful functions provided by this module: 7 Here are some of the useful functions provided by this module:
8 8
9 ismodule(), isclass(), ismethod(), isfunction(), isgeneratorfunction(), 9 ismodule(), isclass(), ismethod(), isfunction(), isgeneratorfunction(),
10 isgenerator(), istraceback(), isframe(), iscode(), isbuiltin(), 10 isgenerator(), istraceback(), isframe(), iscode(), isbuiltin(),
(...skipping 1684 matching lines...) Expand 10 before | Expand all | Expand 10 after
1695 if kind == _VAR_POSITIONAL: 1695 if kind == _VAR_POSITIONAL:
1696 formatted = '*' + formatted 1696 formatted = '*' + formatted
1697 elif kind == _VAR_KEYWORD: 1697 elif kind == _VAR_KEYWORD:
1698 formatted = '**' + formatted 1698 formatted = '**' + formatted
1699 1699
1700 return formatted 1700 return formatted
1701 1701
1702 def __repr__(self): 1702 def __repr__(self):
1703 return '<{} at {:#x} {!r}>'.format(self.__class__.__name__, 1703 return '<{} at {:#x} {!r}>'.format(self.__class__.__name__,
1704 id(self), self.name) 1704 id(self), self.name)
1705
1706 def __hash__(self):
1707 hash_tuple = (self.name, int(self.kind), self._partial_kwarg)
1708
1709 if self._annotation is not _empty:
1710 hash_tuple += (self._annotation,)
1711 if self._default is not _empty:
1712 hash_tuple += (self._default,)
1713
1714 return hash(hash_tuple)
1705 1715
1706 def __eq__(self, other): 1716 def __eq__(self, other):
1707 return (issubclass(other.__class__, Parameter) and 1717 return (issubclass(other.__class__, Parameter) and
1708 self._name == other._name and 1718 self._name == other._name and
1709 self._kind == other._kind and 1719 self._kind == other._kind and
1710 self._default == other._default and 1720 self._default == other._default and
1711 self._annotation == other._annotation) 1721 self._annotation == other._annotation)
1712 1722
1713 def __ne__(self, other): 1723 def __ne__(self, other):
1714 return not self.__eq__(other) 1724 return not self.__eq__(other)
(...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after
2084 ''' 2094 '''
2085 2095
2086 if parameters is _void: 2096 if parameters is _void:
2087 parameters = self.parameters.values() 2097 parameters = self.parameters.values()
2088 2098
2089 if return_annotation is _void: 2099 if return_annotation is _void:
2090 return_annotation = self._return_annotation 2100 return_annotation = self._return_annotation
2091 2101
2092 return type(self)(parameters, 2102 return type(self)(parameters,
2093 return_annotation=return_annotation) 2103 return_annotation=return_annotation)
2104
2105 def __hash__(self):
2106 hash_tuple = tuple(self.parameters.values())
Yury Selivanov 2014/02/25 01:56:30 note to self: Add type(self) in the tuple?
2107 if self._return_annotation is not _empty:
2108 hash_tuple += (self._return_annotation,)
2109 return hash(hash_tuple)
2094 2110
2095 def __eq__(self, other): 2111 def __eq__(self, other):
2096 if (not issubclass(type(other), Signature) or 2112 if (not issubclass(type(other), Signature) or
2097 self.return_annotation != other.return_annotation or 2113 self.return_annotation != other.return_annotation or
2098 len(self.parameters) != len(other.parameters)): 2114 len(self.parameters) != len(other.parameters)):
2099 return False 2115 return False
2100 2116
2101 other_positions = {param: idx 2117 other_positions = {param: idx
2102 for idx, param in enumerate(other.parameters.keys())} 2118 for idx, param in enumerate(other.parameters.keys())}
2103 2119
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after
2350 else: 2366 else:
2351 print('Line: {}'.format(lineno)) 2367 print('Line: {}'.format(lineno))
2352 2368
2353 print('\n') 2369 print('\n')
2354 else: 2370 else:
2355 print(getsource(obj)) 2371 print(getsource(obj))
2356 2372
2357 2373
2358 if __name__ == "__main__": 2374 if __name__ == "__main__":
2359 _main() 2375 _main()
OLDNEW
« 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+