| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 561e15b commit 919045c
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2061,7 +2061,7 @@ def _signature_strip_non_python_syntax(signature): | |||
| 2061 | 2061 | self_parameter = None | |
| 2062 | 2062 | last_positional_only = None | |
| 2063 | 2063 | ||
| 2064 | - lines = [l.encode('ascii') for l in signature.split('\n')] | ||
| 2064 | + lines = [l.encode('ascii') for l in signature.split('\n') if l] | ||
| 2065 | 2065 | generator = iter(lines).__next__ | |
| 2066 | 2066 | token_stream = tokenize.tokenize(generator) | |
| 2067 | 2067 | ||
@@ -2141,7 +2141,6 @@ def _signature_fromstr(cls, obj, s, skip_bound_arg=True): | |||
| 2141 | 2141 | ||
| 2142 | 2142 | parameters = [] | |
| 2143 | 2143 | empty = Parameter.empty | |
| 2144 | - invalid = object() | ||
| 2145 | 2144 | ||
| 2146 | 2145 | module = None | |
| 2147 | 2146 | module_dict = {} | |
@@ -2165,11 +2164,11 @@ def wrap_value(s): | |||
| 2165 | 2164 | try: | |
| 2166 | 2165 | value = eval(s, sys_module_dict) | |
| 2167 | 2166 | except NameError: | |
| 2168 | - raise RuntimeError() | ||
| 2167 | + raise ValueError | ||
| 2169 | 2168 | ||
| 2170 | 2169 | if isinstance(value, (str, int, float, bytes, bool, type(None))): | |
| 2171 | 2170 | return ast.Constant(value) | |
| 2172 | - raise RuntimeError() | ||
| 2171 | + raise ValueError | ||
| 2173 | 2172 | ||
| 2174 | 2173 | class RewriteSymbolics(ast.NodeTransformer): | |
| 2175 | 2174 | def visit_Attribute(self, node): | |
@@ -2179,7 +2178,7 @@ def visit_Attribute(self, node): | |||
| 2179 | 2178 | a.append(n.attr) | |
| 2180 | 2179 | n = n.value | |
| 2181 | 2180 | if not isinstance(n, ast.Name): | |
| 2182 | - raise RuntimeError() | ||
| 2181 | + raise ValueError | ||
| 2183 | 2182 | a.append(n.id) | |
| 2184 | 2183 | value = ".".join(reversed(a)) | |
| 2185 | 2184 | return wrap_value(value) | |
@@ -2189,19 +2188,29 @@ def visit_Name(self, node): | |||
| 2189 | 2188 | raise ValueError() | |
| 2190 | 2189 | return wrap_value(node.id) | |
| 2191 | 2190 | ||
| 2191 | + def visit_BinOp(self, node): | ||
| 2192 | + # Support constant folding of a couple simple binary operations | ||
| 2193 | + # commonly used to define default values in text signatures | ||
| 2194 | + left = self.visit(node.left) | ||
| 2195 | + right = self.visit(node.right) | ||
| 2196 | + if not isinstance(left, ast.Constant) or not isinstance(right, ast.Constant): | ||
| 2197 | + raise ValueError | ||
| 2198 | + if isinstance(node.op, ast.Add): | ||
| 2199 | + return ast.Constant(left.value + right.value) | ||
| 2200 | + elif isinstance(node.op, ast.Sub): | ||
| 2201 | + return ast.Constant(left.value - right.value) | ||
| 2202 | + elif isinstance(node.op, ast.BitOr): | ||
| 2203 | + return ast.Constant(left.value | right.value) | ||
| 2204 | + raise ValueError | ||
| 2205 | + | ||
| 2192 | 2206 | def p(name_node, default_node, default=empty): | |
| 2193 | 2207 | name = parse_name(name_node) | |
| 2194 | - if name is invalid: | ||
| 2195 | - return None | ||
| 2196 | 2208 | if default_node and default_node is not _empty: | |
| 2197 | 2209 | try: | |
| 2198 | 2210 | default_node = RewriteSymbolics().visit(default_node) | |
| 2199 | - o = ast.literal_eval(default_node) | ||
| 2211 | + default = ast.literal_eval(default_node) | ||
| 2200 | 2212 | except ValueError: | |
| 2201 | - o = invalid | ||
| 2202 | - if o is invalid: | ||
| 2203 | - return None | ||
| 2204 | - default = o if o is not invalid else default | ||
| 2213 | + raise ValueError("{!r} builtin has invalid signature".format(obj)) from None | ||
| 2205 | 2214 | parameters.append(Parameter(name, kind, default=default, annotation=empty)) | |
| 2206 | 2215 | ||
| 2207 | 2216 | # non-keyword-only parameters | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2477,7 +2477,7 @@ def p(name): return signature.parameters[name].default | |||
| 2477 | 2477 | self.assertEqual(p('f'), False) | |
| 2478 | 2478 | self.assertEqual(p('local'), 3) | |
| 2479 | 2479 | self.assertEqual(p('sys'), sys.maxsize) | |
| 2480 | - self.assertNotIn('exp', signature.parameters) | ||
| 2480 | + self.assertEqual(p('exp'), sys.maxsize - 1) | ||
| 2481 | 2481 | ||
| 2482 | 2482 | test_callable(object) | |
| 2483 | 2483 | ||
@@ -4246,10 +4246,29 @@ def func(*args, **kwargs): | |||
| 4246 | 4246 | sig = inspect.signature(func) | |
| 4247 | 4247 | self.assertIsNotNone(sig) | |
| 4248 | 4248 | self.assertEqual(str(sig), '(self, /, a, b=1, *args, c, d=2, **kwargs)') | |
| 4249 | + | ||
| 4249 | 4250 | func.__text_signature__ = '($self, a, b=1, /, *args, c, d=2, **kwargs)' | |
| 4250 | 4251 | sig = inspect.signature(func) | |
| 4251 | 4252 | self.assertEqual(str(sig), '(self, a, b=1, /, *args, c, d=2, **kwargs)') | |
| 4252 | 4253 | ||
| 4254 | + func.__text_signature__ = '(self, a=1+2, b=4-3, c=1 | 3 | 16)' | ||
| 4255 | + sig = inspect.signature(func) | ||
| 4256 | + self.assertEqual(str(sig), '(self, a=3, b=1, c=19)') | ||
| 4257 | + | ||
| 4258 | + func.__text_signature__ = '(self, a=1,\nb=2,\n\n\n c=3)' | ||
| 4259 | + sig = inspect.signature(func) | ||
| 4260 | + self.assertEqual(str(sig), '(self, a=1, b=2, c=3)') | ||
| 4261 | + | ||
| 4262 | + func.__text_signature__ = '(self, x=does_not_exist)' | ||
| 4263 | + with self.assertRaises(ValueError): | ||
| 4264 | + inspect.signature(func) | ||
| 4265 | + func.__text_signature__ = '(self, x=sys, y=inspect)' | ||
| 4266 | + with self.assertRaises(ValueError): | ||
| 4267 | + inspect.signature(func) | ||
| 4268 | + func.__text_signature__ = '(self, 123)' | ||
| 4269 | + with self.assertRaises(ValueError): | ||
| 4270 | + inspect.signature(func) | ||
| 4271 | + | ||
| 4253 | 4272 | def test_base_class_have_text_signature(self): | |
| 4254 | 4273 | # see issue 43118 | |
| 4255 | 4274 | from test.ann_module7 import BufferedReader | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,6 @@ | |||
| 1 | + Several improvements to :func:`inspect.signature`'s handling of ``__text_signature``. | ||
| 2 | + - Fixes a case where :func:`inspect.signature` dropped parameters | ||
| 3 | + - Fixes a case where :func:`inspect.signature` raised :exc:`tokenize.TokenError` | ||
| 4 | + - Allows :func:`inspect.signature` to understand defaults involving binary operations of constants | ||
| 5 | + - :func:`inspect.signature` is documented as only raising :exc:`TypeError` or :exc:`ValueError`, but sometimes raised :exc:`RuntimeError`. These cases now raise :exc:`ValueError` | ||
| 6 | + - Removed a dead code path | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments