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

Correctly handle positional only arguments #57

Closed
tristanlatr opened this issue Feb 16, 2022 · 1 comment · Fixed by #58
Closed

Correctly handle positional only arguments #57

tristanlatr opened this issue Feb 16, 2022 · 1 comment · Fixed by #58
Labels
type: bug Something isn't working

Comments

@tristanlatr
Copy link
Contributor

tristanlatr commented Feb 16, 2022

Describe the bug
Currently, docspec_python does not create correct Argument objects because it adds an argument like:

{
 "name": "/",
"type": "POSITIONAL"
}

and does not use the arguement kind Argument.Type.PositionalOnly.

To Reproduce

@pytest.mark.skipif(sys.version_info < (3, 8), reason="requires python3.8 or higher")
@docspec_test()
def test_funcdef_7_posonly_args():
  """
  def func1(x, y=3, /, z=5, w=7): pass
  def func2(x, /, *v, a=1, b=2): pass
  def func3(x, /, *, a=1, b=2, **kwargs): pass
  """

  return [
    mkfunc('func1', None, [
      Argument('x', Argument.Type.PositionalOnly),
      Argument('y', Argument.Type.PositionalOnly, default_value='3'),
      Argument('z', Argument.Type.Positional, default_value='5'),
      Argument('w', Argument.Type.Positional, default_value='7'),
    ]),
    mkfunc('func2', None, [
      Argument('x', Argument.Type.PositionalOnly),
      Argument('v', Argument.Type.PositionalRemainder),
      Argument('a', Argument.Type.KeywordOnly, default_value='1'),
      Argument('b', Argument.Type.KeywordOnly, default_value='2'),
    ]),
    mkfunc('func3', None, [
      Argument('x', Argument.Type.PositionalOnly),
      Argument('a', Argument.Type.KeywordOnly, default_value='1'),
      Argument('b', Argument.Type.KeywordOnly, default_value='2'),
      Argument('kwargs', Argument.Type.KeywordRemainder),
    ]),
  ]

Then run tests and see the diff:

E         {
E           "name": "funcdef_7_posonly_args",
E           "location": null,
E           "docstring": null,
E           "members": [
E             {
E               "name": "func1",
E               "location": null,
E               "docstring": null,
E               "modifiers": null,
E               "args": [
E                 {
E                   "name": "x",
E       -           "type": "POSITIONAL"
E       +           "type": "POSITIONAL_ONLY"
E       ?                              +++++
E                 },
E                 {
E                   "name": "y",
E       -           "type": "POSITIONAL",
E       +           "type": "POSITIONAL_ONLY",
E       ?                              +++++
E                   "default_value": "3"
E       -         },
E       -         {
E       -           "name": "/",
E       -           "type": "POSITIONAL"
E                 },
E                 {
E                   "name": "z",
E                   "type": "POSITIONAL",
E                   "default_value": "5"
E                 },
E                 {
E                   "name": "w",
E                   "type": "POSITIONAL",
E                   "default_value": "7"
E                 }
E               ],
E               "return_type": null,
E               "decorations": [],
E               "type": "function"
E             },
E             {
E               "name": "func2",
E               "location": null,
E               "docstring": null,
E               "modifiers": null,
E               "args": [
E                 {
E                   "name": "x",
E       -           "type": "POSITIONAL"
E       +           "type": "POSITIONAL_ONLY"
E       ?                              +++++
E       -         },
E       -         {
E       -           "name": "/",
E       -           "type": "POSITIONAL"
E                 },
E                 {
E                   "name": "v",
E                   "type": "POSITIONAL_REMAINDER"
E                 },
E                 {
E                   "name": "a",
E                   "type": "KEYWORD_ONLY",
E                   "default_value": "1"
E                 },
E                 {
E                   "name": "b",
E                   "type": "KEYWORD_ONLY",
E                   "default_value": "2"
E                 }
E               ],
E               "return_type": null,
E               "decorations": [],
E               "type": "function"
E             },
E             {
E               "name": "func3",
E               "location": null,
E               "docstring": null,
E               "modifiers": null,
E               "args": [
E                 {
E                   "name": "x",
E       -           "type": "POSITIONAL"
E       +           "type": "POSITIONAL_ONLY"
E       ?                              +++++
E       -         },
E       -         {
E       -           "name": "/",
E       -           "type": "POSITIONAL"
E                 },
E                 {
E                   "name": "a",
E                   "type": "KEYWORD_ONLY",
E                   "default_value": "1"
E                 },
E                 {
E                   "name": "b",
E                   "type": "KEYWORD_ONLY",
E                   "default_value": "2"
E                 },
E                 {
E                   "name": "kwargs",
E                   "type": "KEYWORD_REMAINDER"
E                 }
E               ],
E               "return_type": null,
E               "decorations": [],
E               "type": "function"
E             }
E           ]
E         }

Expected behavior
The expected behaviour would be to produce Argument with kind POSITIONAL_ONLY and do not have the extra / argument in the list.

@tristanlatr tristanlatr added the type: bug Something isn't working label Feb 16, 2022
@NiklasRosenstein
Copy link
Owner

Hey @tristanlatr , thank you for reporting this and even providing a test case. Integrating the test case now and looking into a fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants