GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Modernize `test_inspect` by adding real pos-only parameters · Issue #103406 · python/cpython

Modernize test_inspect by adding real pos-only parameters #103406

New issue
Copy link
New issue
Copy link
Closed
Closed
Modernize test_inspect by adding real pos-only parameters#103406
Copy link
Assignees
sobolevn
Labels
testsTests in the Lib/test dirTests in the Lib/test dirtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@sobolevn
sobolevn
opened on Apr 10, 2023
Issue body actions

Right now test_inspect uses several hacks to pretend that some parameters are positional only:

cpython/Lib/test/test_inspect.py

Lines 2465 to 2469 in ecad802

def test(po, pk, pod=42, pkd=100, *args, ko, **kwargs):
pass
sig = inspect.signature(test)
po = sig.parameters['po'].replace(kind=P.POSITIONAL_ONLY)
pod = sig.parameters['pod'].replace(kind=P.POSITIONAL_ONLY)

cpython/Lib/test/test_inspect.py

Lines 2519 to 2522 in ecad802

myparam = MyParameter(name='z', kind=inspect.Parameter.POSITIONAL_ONLY)
myparams = collections.OrderedDict(sig.parameters, a=myparam)
mysig = MySignature().replace(parameters=myparams.values(),
return_annotation=sig.return_annotation)

cpython/Lib/test/test_inspect.py

Lines 3047 to 3052 in ecad802

def foo(a, b, c, d, **kwargs):
pass
sig = inspect.signature(foo)
params = sig.parameters.copy()
params['a'] = params['a'].replace(kind=Parameter.POSITIONAL_ONLY)
params['b'] = params['b'].replace(kind=Parameter.POSITIONAL_ONLY)

cpython/Lib/test/test_inspect.py

Lines 3559 to 3565 in ecad802

def test(a_po, *, b, **kwargs):
return a_po, kwargs
sig = inspect.signature(test)
new_params = list(sig.parameters.values())
new_params[0] = new_params[0].replace(kind=P.POSITIONAL_ONLY)
test.__signature__ = sig.replace(parameters=new_params)

cpython/Lib/test/test_inspect.py

Lines 4160 to 4166 in ecad802

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] = new_params[name].replace(kind=P.POSITIONAL_ONLY)

And maybe others.

It makes code more complex, unclear, and hides the real purpose of these tests.

This is not a design decision, but rather a limitation at the time. Commits are quite old and pos-only syntax was not available 9 and 11 years ago:

  • 3f73ca2
  • 7c7cbfc

So, I propose to simplify these tests and make them more correct by using explicit pos only parameters.

Plus, we can keep one test like this to be sure that chaning a parameter kind still works as before. But, there's no need in keeping these old tests the way they are.

I will send a PR with the fix 👍

Linked PRs

  • gh-103406: Modernize pos-only arguments usage in test_signature #103407
  • [3.11] gh-103406: Modernize pos-only arguments usage in test_signature (GH-103407) #103536

Activity

  1. sobolevn
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    testsTests in the Lib/test dirTests in the Lib/test dir
    on Apr 10, 2023
  2. sobolevn
    self-assigned this
    on Apr 10, 2023
  3. sobolevn commented on Apr 10, 2023

    @sobolevn
    sobolevn
    on Apr 10, 2023
    MemberAuthor
    More actions

    Since all supported Python versions have pos-only parameters this can be backported to reduce the amount of potential future merge conflicts.

  4. sobolevn commented on Apr 10, 2023

    @sobolevn
    sobolevn
    on Apr 10, 2023
    MemberAuthor
    More actions

    Ok, some code samples that I've listed cannot be replaced, because they test different things. But, some - can be.

  5. sobolevn
    added a commit that references this issue on Apr 10, 2023

    pythongh-103406: Modernize pos-only arguments usage in `test_signature`

    f06ee15
  6. miss-islington
    added a commit that references this issue on Apr 14, 2023

    pythongh-103406: Modernize pos-only arguments usage in `test_signatur…

    4114451
  7. JelleZijlstra
    added a commit that references this issue on Apr 14, 2023

    gh-103406: Modernize pos-only arguments usage in `test_signature` (#1…

    7569781
  8. miss-islington
    added a commit that references this issue on Apr 14, 2023

    gh-103406: Modernize pos-only arguments usage in `test_signature` (GH…

    2e0ead5
  9. carljm
    added 2 commits that reference this issue on Apr 14, 2023

    Merge branch 'main' into superopt

    64da49f

    Merge branch 'superopt' into superopt_spec

    b73cff0
  10. sobolevn
    closed this as completedon Apr 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @sobolevn
    sobolevn

Labels

testsTests in the Lib/test dirTests in the Lib/test dirtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.