Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for positional-only parameters in Python #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions Lib/idlelib/idle_test/test_calltip.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,20 @@ def gtest(obj, out):
self.assertEqual(signature(obj), out)

if List.__doc__ is not None:
gtest(List, '(iterable=(), /)' + calltip._argument_positional
gtest(List, '(__iterable=())'
+ '\n' + List.__doc__)
gtest(list.__new__,
'(*args, **kwargs)\n'
'Create and return a new object. '
'See help(type) for accurate signature.')
gtest(list.__init__,
'(self, /, *args, **kwargs)'
+ calltip._argument_positional + '\n' +
'(__self, *args, **kwargs)'
+ '\n' +
'Initialize self. See help(type(self)) for accurate signature.')
append_doc = (calltip._argument_positional
+ "\nAppend object to the end of the list.")
gtest(list.append, '(self, object, /)' + append_doc)
gtest(List.append, '(self, object, /)' + append_doc)
gtest([].append, '(object, /)' + append_doc)
append_doc = "\nAppend object to the end of the list."
gtest(list.append, '(__self, __object)' + append_doc)
gtest(List.append, '(__self, __object)' + append_doc)
gtest([].append, '(__object)' + append_doc)

gtest(types.MethodType, "method(function, instance)")
gtest(SB(), default_tip)
Expand Down
27 changes: 10 additions & 17 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1918,6 +1918,7 @@ def _signature_strip_non_python_syntax(signature):

current_parameter = 0
OP = token.OP
NAME = token.NAME
ERRORTOKEN = token.ERRORTOKEN

# token stream always starts with ENCODING token, skip it
Expand All @@ -1939,14 +1940,17 @@ def _signature_strip_non_python_syntax(signature):

if string == '/':
assert not skip_next_comma
assert last_positional_only is None
skip_next_comma = True
last_positional_only = current_parameter - 1
continue

if type == NAME and string.startswith('__'):
last_positional_only = current_parameter

if (type == ERRORTOKEN) and (string == '$'):
assert self_parameter is None
self_parameter = current_parameter
last_positional_only = current_parameter
continue

if delayed_comma:
Expand Down Expand Up @@ -2192,7 +2196,7 @@ def _signature_from_function(cls, func):
# parameters list (for correct order and defaults), it should be OK.
return cls(parameters,
return_annotation=annotations.get('return', _empty),
__validate_parameters__=is_duck_function)
_validate_parameters_=is_duck_function)


def _signature_from_callable(obj, *,
Expand Down Expand Up @@ -2749,15 +2753,15 @@ class Signature:
empty = _empty

def __init__(self, parameters=None, *, return_annotation=_empty,
__validate_parameters__=True):
_validate_parameters_=True):
"""Constructs Signature from the given list of Parameter
objects and 'return_annotation'. All arguments are optional.
"""

if parameters is None:
params = OrderedDict()
else:
if __validate_parameters__:
if _validate_parameters_:
params = OrderedDict()
top_kind = _POSITIONAL_ONLY
kind_defaults = False
Expand Down Expand Up @@ -3035,20 +3039,14 @@ def __repr__(self):

def __str__(self):
result = []
render_pos_only_separator = False
render_kw_only_separator = True
for param in self.parameters.values():
formatted = str(param)

kind = param.kind

if kind == _POSITIONAL_ONLY:
render_pos_only_separator = True
elif render_pos_only_separator:
# It's not a positional-only parameter, and the flag
# is set to 'True' (there were pos-only params before.)
result.append('/')
render_pos_only_separator = False
if kind == _POSITIONAL_ONLY and not formatted.startswith('__'):
formatted = '__' + formatted

if kind == _VAR_POSITIONAL:
# OK, we have an '*args'-like parameter, so we won't need
Expand All @@ -3065,11 +3063,6 @@ def __str__(self):

result.append(formatted)

if render_pos_only_separator:
# There were only positional-only parameters, hence the
# flag was not reset to 'False'
result.append('/')

rendered = '({})'.format(', '.join(result))

if self.return_annotation is not _empty:
Expand Down
Loading