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

Side by Side Diff: Lib/test/test_inspect.py

Issue 20334: make inspect Signature hashable
Patch Set: Created 5 years, 9 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
« Lib/inspect.py ('K') | « Lib/inspect.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 import re 1 import re
2 import sys 2 import sys
3 import types 3 import types
4 import unittest 4 import unittest
5 import inspect 5 import inspect
6 import linecache 6 import linecache
7 import datetime 7 import datetime
8 import collections 8 import collections
9 import os 9 import os
10 import shutil 10 import shutil
(...skipping 2033 matching lines...) Expand 10 before | Expand all | Expand 10 after
2044 self.assertEqual(inspect.signature(foo), inspect.signature(bar)) 2044 self.assertEqual(inspect.signature(foo), inspect.signature(bar))
2045 2045
2046 def foo(pos, *, a, b, c): pass 2046 def foo(pos, *, a, b, c): pass
2047 def bar(pos, *, c, b, a=1): pass 2047 def bar(pos, *, c, b, a=1): pass
2048 self.assertNotEqual(inspect.signature(foo), inspect.signature(bar)) 2048 self.assertNotEqual(inspect.signature(foo), inspect.signature(bar))
2049 2049
2050 def foo(pos, *args, a=42, b, c, **kwargs:int): pass 2050 def foo(pos, *args, a=42, b, c, **kwargs:int): pass
2051 def bar(pos, *args, c, b, a=42, **kwargs:int): pass 2051 def bar(pos, *args, c, b, a=42, **kwargs:int): pass
2052 self.assertEqual(inspect.signature(foo), inspect.signature(bar)) 2052 self.assertEqual(inspect.signature(foo), inspect.signature(bar))
2053 2053
2054 def test_signature_unhashable(self): 2054 def test_signature_hashable(self):
2055 S = inspect.Signature
2056 P = inspect.Parameter
2057
2055 def foo(a): pass 2058 def foo(a): pass
2056 sig = inspect.signature(foo) 2059 auto_sig = inspect.signature(foo)
2057 with self.assertRaisesRegex(TypeError, 'unhashable type'): 2060
2058 hash(sig) 2061 manual_sig = S(parameters=[P('a', P.POSITIONAL_OR_KEYWORD)])
2062
2063 self.assertEqual(hash(auto_sig), hash(manual_sig))
2064 self.assertNotEqual(hash(auto_sig),
2065 hash(manual_sig.replace(return_annotation='spam')))
2059 2066
2060 def test_signature_str(self): 2067 def test_signature_str(self):
2061 def foo(a:int=1, *, b, c=None, **kwargs) -> 42: 2068 def foo(a:int=1, *, b, c=None, **kwargs) -> 42:
2062 pass 2069 pass
2063 self.assertEqual(str(inspect.signature(foo)), 2070 self.assertEqual(str(inspect.signature(foo)),
2064 '(a:int=1, *, b, c=None, **kwargs) -> 42') 2071 '(a:int=1, *, b, c=None, **kwargs) -> 42')
2065 2072
2066 def foo(a:int=1, *args, b, c=None, **kwargs) -> 42: 2073 def foo(a:int=1, *args, b, c=None, **kwargs) -> 42:
2067 pass 2074 pass
2068 self.assertEqual(str(inspect.signature(foo)), 2075 self.assertEqual(str(inspect.signature(foo)),
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
2141 with self.assertRaisesRegex(ValueError, 'cannot have default values'): 2148 with self.assertRaisesRegex(ValueError, 'cannot have default values'):
2142 inspect.Parameter('a', default=42, 2149 inspect.Parameter('a', default=42,
2143 kind=inspect.Parameter.VAR_POSITIONAL) 2150 kind=inspect.Parameter.VAR_POSITIONAL)
2144 2151
2145 p = inspect.Parameter('a', default=42, 2152 p = inspect.Parameter('a', default=42,
2146 kind=inspect.Parameter.POSITIONAL_OR_KEYWORD) 2153 kind=inspect.Parameter.POSITIONAL_OR_KEYWORD)
2147 with self.assertRaisesRegex(ValueError, 'cannot have default values'): 2154 with self.assertRaisesRegex(ValueError, 'cannot have default values'):
2148 p.replace(kind=inspect.Parameter.VAR_POSITIONAL) 2155 p.replace(kind=inspect.Parameter.VAR_POSITIONAL)
2149 2156
2150 self.assertTrue(repr(p).startswith('<Parameter')) 2157 self.assertTrue(repr(p).startswith('<Parameter'))
2158
2159 def test_signature_parameter_hashable(self):
2160 P = inspect.Parameter
2161 foo = P('foo', kind=P.POSITIONAL_ONLY)
2162 self.assertEqual(hash(foo), hash(P('foo', kind=P.POSITIONAL_ONLY)))
2163 self.assertNotEqual(hash(foo), hash(P('foo', kind=P.POSITIONAL_ONLY,
2164 default=42)))
2165 self.assertNotEqual(hash(foo),
2166 hash(foo.replace(kind=P.VAR_POSITIONAL)))
2151 2167
2152 def test_signature_parameter_equality(self): 2168 def test_signature_parameter_equality(self):
2153 P = inspect.Parameter 2169 P = inspect.Parameter
2154 p = P('foo', default=42, kind=inspect.Parameter.KEYWORD_ONLY) 2170 p = P('foo', default=42, kind=inspect.Parameter.KEYWORD_ONLY)
2155 2171
2156 self.assertEqual(p, p) 2172 self.assertEqual(p, p)
2157 self.assertNotEqual(p, 42) 2173 self.assertNotEqual(p, 42)
2158 2174
2159 self.assertEqual(p, P('foo', default=42, 2175 self.assertEqual(p, P('foo', default=42,
2160 kind=inspect.Parameter.KEYWORD_ONLY)) 2176 kind=inspect.Parameter.KEYWORD_ONLY))
2161
2162 def test_signature_parameter_unhashable(self):
2163 p = inspect.Parameter('foo', default=42,
2164 kind=inspect.Parameter.KEYWORD_ONLY)
2165
2166 with self.assertRaisesRegex(TypeError, 'unhashable type'):
2167 hash(p)
2168 2177
2169 def test_signature_parameter_replace(self): 2178 def test_signature_parameter_replace(self):
2170 p = inspect.Parameter('foo', default=42, 2179 p = inspect.Parameter('foo', default=42,
2171 kind=inspect.Parameter.KEYWORD_ONLY) 2180 kind=inspect.Parameter.KEYWORD_ONLY)
2172 2181
2173 self.assertIsNot(p, p.replace()) 2182 self.assertIsNot(p, p.replace())
2174 self.assertEqual(p, p.replace()) 2183 self.assertEqual(p, p.replace())
2175 2184
2176 p2 = p.replace(annotation=1) 2185 p2 = p.replace(annotation=1)
2177 self.assertEqual(p2.annotation, 1) 2186 self.assertEqual(p2.annotation, 1)
(...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after
2555 TestDecorators, TestRetrievingSourceCode, TestOneliners, TestBuggyCases, 2564 TestDecorators, TestRetrievingSourceCode, TestOneliners, TestBuggyCases,
2556 TestInterpreterStack, TestClassesAndFunctions, TestPredicates, 2565 TestInterpreterStack, TestClassesAndFunctions, TestPredicates,
2557 TestGetcallargsFunctions, TestGetcallargsMethods, 2566 TestGetcallargsFunctions, TestGetcallargsMethods,
2558 TestGetcallargsUnboundMethods, TestGetattrStatic, TestGetGeneratorState, 2567 TestGetcallargsUnboundMethods, TestGetattrStatic, TestGetGeneratorState,
2559 TestNoEOL, TestSignatureObject, TestSignatureBind, TestParameterObject, 2568 TestNoEOL, TestSignatureObject, TestSignatureBind, TestParameterObject,
2560 TestBoundArguments, TestGetClosureVars, TestUnwrap, TestMain 2569 TestBoundArguments, TestGetClosureVars, TestUnwrap, TestMain
2561 ) 2570 )
2562 2571
2563 if __name__ == "__main__": 2572 if __name__ == "__main__":
2564 test_main() 2573 test_main()
OLDNEW
« Lib/inspect.py ('K') | « Lib/inspect.py ('k') | no next file » | no next file with comments »

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