Commits

1st1 committed dccfbee

pep 362: PEP-8-ify test_inspect (trim lines to 80 chars)

Comments (0)

Files changed (2)

         else:
             formatted = '{}:{}'.format(formatted,
                                        formatannotation(annotatation))
-
         try:
             default = self._default
         except AttributeError:
         else:
             formatted = '{}={}'.format(formatted, repr(default))
 
-
         if kind == _VAR_POSITIONAL:
             formatted = '*' + formatted
         elif kind == _VAR_KEYWORD:

Lib/test/test_inspect.py

         return (tuple((param.name,
                        getattr(param, 'value', getattr(param, 'default', ...)),
                        getattr(param, 'annotation', ...),
-                       str(param.kind).lower()) for param in sig.parameters.values()),
+                       str(param.kind).lower())
+                                    for param in sig.parameters.values()),
                 getattr(sig, 'return_annotation', ...))
 
     def test_signature_object(self):
     def test_signature_on_wargs(self):
         def test(a, b:'foo') -> 123:
             pass
-        self.assertEqual(self.signature(test), ((('a', ..., ..., "positional_or_keyword"),
-                                                 ('b', ..., 'foo', "positional_or_keyword")),
-                                                123))
+        self.assertEqual(self.signature(test),
+                         ((('a', ..., ..., "positional_or_keyword"),
+                           ('b', ..., 'foo', "positional_or_keyword")),
+                          123))
 
     def test_signature_on_wkwonly(self):
         def test(*, a:float, b:str) -> int:
             pass
-        self.assertEqual(self.signature(test), ((('a', ..., float, "keyword_only"),
-                                                 ('b', ..., str, "keyword_only")),
-                                                int))
+        self.assertEqual(self.signature(test),
+                         ((('a', ..., float, "keyword_only"),
+                           ('b', ..., str, "keyword_only")),
+                           int))
 
     def test_signature_on_complex_args(self):
         def test(a, b:'foo'=10, *args:'bar', spam:'baz', ham=123, **kwargs:int):
             pass
-        self.assertEqual(self.signature(test), ((('a', ..., ..., "positional_or_keyword"),
-                                                 ('b', 10, 'foo', "positional_or_keyword"),
-                                                 ('args', ..., 'bar', "var_positional"),
-                                                 ('spam', ..., 'baz', "keyword_only"),
-                                                 ('ham', 123, ..., "keyword_only"),
-                                                 ('kwargs', ..., int, "var_keyword")),
-                                                ...))
+        self.assertEqual(self.signature(test),
+                         ((('a', ..., ..., "positional_or_keyword"),
+                           ('b', 10, 'foo', "positional_or_keyword"),
+                           ('args', ..., 'bar', "var_positional"),
+                           ('spam', ..., 'baz', "keyword_only"),
+                           ('ham', 123, ..., "keyword_only"),
+                           ('kwargs', ..., int, "var_keyword")),
+                          ...))
 
     def test_signature_on_builtin_function(self):
-        with self.assertRaisesRegexp(ValueError, 'is not supported by signature'):
+        with self.assertRaisesRegexp(ValueError, 'not supported by signature'):
             inspect.signature(type)
-        with self.assertRaisesRegexp(ValueError, 'is not supported by signature'):
+        with self.assertRaisesRegexp(ValueError, 'not supported by signature'):
             # support for 'wrapper_descriptor'
             inspect.signature(type.__call__)
-        with self.assertRaisesRegexp(ValueError, 'is not supported by signature'):
+        with self.assertRaisesRegexp(ValueError, 'not supported by signature'):
             # support for 'method-wrapper'
             inspect.signature(min.__call__)
-        with self.assertRaisesRegexp(ValueError, 'no signature found for builtin function'):
+        with self.assertRaisesRegexp(ValueError,
+                                     'no signature found for builtin function'):
             # support for 'method-wrapper'
             inspect.signature(min)
 
 
         meth = Test().foo
 
-        self.assertEqual(self.signature(meth), ((('arg1', ..., ..., "positional_or_keyword"),
-                                                 ('arg2', 1, ..., "positional_or_keyword")),
-                                                int))
+        self.assertEqual(self.signature(meth),
+                         ((('arg1', ..., ..., "positional_or_keyword"),
+                           ('arg2', 1, ..., "positional_or_keyword")),
+                          int))
 
     def test_signature_on_classmethod(self):
         class Test:
                 pass
 
         meth = Test().foo
-        self.assertEqual(self.signature(meth), ((('arg1', ..., ..., "positional_or_keyword"),
-                                                 ('arg2', 1, ..., "keyword_only")),
-                                                ...))
+        self.assertEqual(self.signature(meth),
+                         ((('arg1', ..., ..., "positional_or_keyword"),
+                           ('arg2', 1, ..., "keyword_only")),
+                          ...))
 
         meth = Test.foo
-        self.assertEqual(self.signature(meth), ((('arg1', ..., ..., "positional_or_keyword"),
-                                                 ('arg2', 1, ..., "keyword_only")),
-                                                ...))
+        self.assertEqual(self.signature(meth),
+                         ((('arg1', ..., ..., "positional_or_keyword"),
+                           ('arg2', 1, ..., "keyword_only")),
+                          ...))
 
     def test_signature_on_staticmethod(self):
         class Test:
                 pass
 
         meth = Test().foo
-        self.assertEqual(self.signature(meth), ((('cls', ..., ..., "positional_or_keyword"),
-                                                 ('arg', ..., ..., "keyword_only")),
-                                                ...))
+        self.assertEqual(self.signature(meth),
+                         ((('cls', ..., ..., "positional_or_keyword"),
+                           ('arg', ..., ..., "keyword_only")),
+                          ...))
 
         meth = Test.foo
-        self.assertEqual(self.signature(meth), ((('cls', ..., ..., "positional_or_keyword"),
-                                                 ('arg', ..., ..., "keyword_only")),
-                                                ...))
+        self.assertEqual(self.signature(meth),
+                         ((('cls', ..., ..., "positional_or_keyword"),
+                           ('arg', ..., ..., "keyword_only")),
+                          ...))
 
     def test_signature_on_partial(self):
         from functools import partial
                            ('c', ..., ..., "positional_or_keyword")),
                           ...))
         self.assertEqual(_foo(c=10), (1, 30, 10))
-        _foo = partial(_foo, 2) # now 'b' has two values - positional and keyword
+        _foo = partial(_foo, 2) # now 'b' has two values -
+                                # positional and keyword
         with self.assertRaisesRegexp(ValueError, "has incorrect arguments"):
             inspect.signature(_foo)
 
 
         class ToFail:
             __call__ = type
-        with self.assertRaisesRegexp(ValueError, "is not supported by signature"):
+        with self.assertRaisesRegexp(ValueError, "not supported by signature"):
             inspect.signature(ToFail())
 
 
         self.assertEqual(str(inspect.signature(foo)), '()')
 
     def test_signature_str_positional_only(self):
+        P = inspect.Parameter
+
         def test(a_po, *, b, **kwargs):
             return a_po, kwargs
 
         sig = inspect.signature(test)
-        new_params = collections.OrderedDict(tuple(sig.parameters.items()))
-        new_params['a_po'] = sig.parameters['a_po'].replace(kind=inspect.Parameter.POSITIONAL_ONLY)
-        test.__signature__ = sig.replace(parameters=new_params.values())
+        new_params = list(sig.parameters.values())
+        new_params[0] = new_params[0].replace(kind=P.POSITIONAL_ONLY)
+        test.__signature__ = sig.replace(parameters=new_params)
 
-        self.assertEqual(str(inspect.signature(test)), '(<a_po>, *, b, **kwargs)')
+        self.assertEqual(str(inspect.signature(test)),
+                         '(<a_po>, *, b, **kwargs)')
 
         sig = inspect.signature(test)
         new_params = list(sig.parameters.values())
         new_params[0] = new_params[0].replace(name=None)
         test.__signature__ = sig.replace(parameters=new_params)
-        self.assertEqual(str(inspect.signature(test)), '(<0>, *, b, **kwargs)')
+        self.assertEqual(str(inspect.signature(test)),
+                         '(<0>, *, b, **kwargs)')
 
     def test_signature_replace_anno(self):
         def test() -> 42:
         self.assertTrue('POSITIONAL_ONLY' in repr(P.POSITIONAL_ONLY))
 
     def test_signature_parameter_object(self):
-        p = inspect.Parameter('foo', default=10, kind=inspect.Parameter.VAR_KEYWORD)
+        p = inspect.Parameter('foo', default=10,
+                              kind=inspect.Parameter.VAR_KEYWORD)
         self.assertEqual(p.name, 'foo')
         self.assertEqual(p.default, 10)
         self.assertIs(getattr(p, 'annotation', ...), ...)
         with self.assertRaisesRegexp(ValueError, 'invalid value'):
             inspect.Parameter('foo', default=10, kind='123')
 
-        with self.assertRaisesRegexp(ValueError, 'is not a valid parameter name'):
+        with self.assertRaisesRegexp(ValueError, 'not a valid parameter name'):
             inspect.Parameter('1', kind=inspect.Parameter.VAR_KEYWORD)
 
-        with self.assertRaisesRegexp(ValueError, 'non-positional-only parameter'):
+        with self.assertRaisesRegexp(ValueError,
+                                     'non-positional-only parameter'):
             inspect.Parameter(None, kind=inspect.Parameter.VAR_KEYWORD)
 
         self.assertTrue(repr(p).startswith('<Parameter'))
 
     def test_signature_parameter_equality(self):
-        p = inspect.Parameter('foo', default=42,
-                              kind=inspect.Parameter.VAR_POSITIONAL)
+        P = inspect.Parameter
+        p = P('foo', default=42, kind=inspect.Parameter.VAR_POSITIONAL)
 
         self.assertEqual(p, p)
         self.assertNotEqual(p, 42)
 
-        self.assertEqual(p, inspect.Parameter('foo', default=42,
-                                              kind=inspect.Parameter.VAR_POSITIONAL))
+        self.assertEqual(p, P('foo', default=42,
+                              kind=inspect.Parameter.VAR_POSITIONAL))
 
     def test_signature_parameter_unhashable(self):
         p = inspect.Parameter('foo', default=42,
         self.assertEqual(p2.name, 'bar')
         self.assertNotEqual(p2, p)
 
-        with self.assertRaisesRegexp(ValueError, 'is not a valid parameter name'):
+        with self.assertRaisesRegexp(ValueError, 'not a valid parameter name'):
             p2 = p2.replace(name=p2.empty)
 
         p2 = p2.replace(name='foo', default=None)
         self.assertEqual(self.call(test, 1, 2), ((1, 2), {}))
         self.assertEqual(self.call(test, foo='bar'), ((), {'foo': 'bar'}))
         self.assertEqual(self.call(test, 1, foo='bar'), ((1,), {'foo': 'bar'}))
-        self.assertEqual(self.call(test, 1, 2, foo='bar'), ((1, 2), {'foo': 'bar'}))
         self.assertEqual(self.call(test, args=10), ((), {'args': 10}))
+        self.assertEqual(self.call(test, 1, 2, foo='bar'),
+                         ((1, 2), {'foo': 'bar'}))
 
     def test_signature_bind_just_args(self):
         def test(a, b, c):
         self.assertEqual(self.call(test, b=1, a=2), (2, 1, 3, ()))
         self.assertEqual(self.call(test, 1, b=2), (1, 2, 3, ()))
 
-        with self.assertRaisesRegexp(TypeError, "multiple values for argument 'c'"):
+        with self.assertRaisesRegexp(TypeError,
+                                     "multiple values for argument 'c'"):
             self.call(test, 1, 2, 3, c=4)
 
     def test_signature_bind_just_kwargs(self):
     def test_signature_bind_kwonly(self):
         def test(*, foo):
             return foo
-        with self.assertRaisesRegexp(TypeError, 'too many positional arguments'):
+        with self.assertRaisesRegexp(TypeError,
+                                     'too many positional arguments'):
             self.call(test, 1)
         self.assertEqual(self.call(test, foo=1), 1)
 
         def test(a, *, foo=1, bar):
             return foo
-        with self.assertRaisesRegexp(TypeError, "'bar' parameter lacking default value"):
+        with self.assertRaisesRegexp(TypeError,
+                                     "'bar' parameter lacking default value"):
             self.call(test, 1)
 
         def test(foo, *, bar):
         self.assertEqual(self.call(test, 1, bar=2), (1, 2))
         self.assertEqual(self.call(test, bar=2, foo=1), (1, 2))
 
-        with self.assertRaisesRegexp(TypeError, 'too many keyword arguments'):
+        with self.assertRaisesRegexp(TypeError,
+                                     'too many keyword arguments'):
             self.call(test, bar=2, foo=1, spam=10)
 
-        with self.assertRaisesRegexp(TypeError, 'too many positional arguments'):
+        with self.assertRaisesRegexp(TypeError,
+                                     'too many positional arguments'):
             self.call(test, 1, 2)
 
-        with self.assertRaisesRegexp(TypeError, 'too many positional arguments'):
+        with self.assertRaisesRegexp(TypeError,
+                                     'too many positional arguments'):
             self.call(test, 1, 2, bar=2)
 
-        with self.assertRaisesRegexp(TypeError, 'too many keyword arguments'):
+        with self.assertRaisesRegexp(TypeError,
+                                     'too many keyword arguments'):
             self.call(test, 1, bar=2, spam='ham')
 
-        with self.assertRaisesRegexp(TypeError, "'bar' parameter lacking default value"):
+        with self.assertRaisesRegexp(TypeError,
+                                     "'bar' parameter lacking default value"):
             self.call(test, 1)
 
         def test(foo, *, bar, **bin):
             return foo, bar, bin
         self.assertEqual(self.call(test, 1, bar=2), (1, 2, {}))
         self.assertEqual(self.call(test, foo=1, bar=2), (1, 2, {}))
-        self.assertEqual(self.call(test, 1, bar=2, spam='ham'), (1, 2, {'spam': 'ham'}))
-        self.assertEqual(self.call(test, spam='ham', foo=1, bar=2), (1, 2, {'spam': 'ham'}))
-        with self.assertRaisesRegexp(TypeError, "'foo' parameter lacking default value"):
+        self.assertEqual(self.call(test, 1, bar=2, spam='ham'),
+                         (1, 2, {'spam': 'ham'}))
+        self.assertEqual(self.call(test, spam='ham', foo=1, bar=2),
+                         (1, 2, {'spam': 'ham'}))
+        with self.assertRaisesRegexp(TypeError,
+                                     "'foo' parameter lacking default value"):
             self.call(test, spam='ham', bar=2)
         self.assertEqual(self.call(test, 1, bar=2, bin=1, spam=10),
                          (1, 2, {'bin': 1, 'spam': 10}))
         self.assertEqual(tuple(ba.arguments.items()),
                          (('a', 10), ('args', (20,)), ('b', 30),
                           ('kwargs', {'c': 40, 'args': 50, 'kwargs': 60})))
+        self.assertEqual(ba.kwargs,
+                         {'b': 30, 'c': 40, 'args': 50, 'kwargs': 60})
         self.assertEqual(ba.args, (10, 20))
-        self.assertEqual(ba.kwargs, {'b': 30, 'c': 40, 'args': 50, 'kwargs': 60})
 
     def test_signature_bind_positional_only(self):
+        P = inspect.Parameter
+
         def test(a_po, b_po, c_po=3, foo=42, *, bar=50, **kwargs):
             return a_po, b_po, c_po, foo, bar, kwargs
 
         sig = inspect.signature(test)
         new_params = collections.OrderedDict(tuple(sig.parameters.items()))
         for name in ('a_po', 'b_po', 'c_po'):
-            new_params[name] = sig.parameters[name].replace(kind=inspect.Parameter.POSITIONAL_ONLY)
+            new_params[name] = new_params[name].replace(kind=P.POSITIONAL_ONLY)
         new_sig = sig.replace(parameters=new_params.values())
         test.__signature__ = new_sig
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.