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 — gh-131885: simplify function signatures in the cmath module by skirpichev · Pull Request #131886 · python/cpython

Conversation

@skirpichev

@skirpichev skirpichev commented Mar 30, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Now function docstrings are in sync with the module docs, for example:

>>> import cmath, inspect
>>> help(cmath.sin)
Help on built-in function sin in module cmath:

sin(z)
    Return the sine of z.

>>> inspect.signature(cmath.sin)
<Signature (z)>

As a side effect, this allows sin(z=1.23) calls. The price is slight
performance penalty (maybe this can be fixed on AC side):

Benchmark ref patch
sin(1.1) 417 ns 428 ns: 1.03x slower
sin(1j) 414 ns 422 ns: 1.02x slower
log(2.3) 390 ns 409 ns: 1.05x slower
log(1+2j) 449 ns 468 ns: 1.04x slower
log(2.1, 3.2) 601 ns 630 ns: 1.05x slower
Geometric mean (ref) 1.04x slower
  • Issue: Update function signatures to use * and / as needed #131885

For reviewers:

  • this is on top of gh-125957: sync argument naming in sphinx docs of the cmath and help() #125958
  • alternative: adjust sphinx docs instead, like gh-101118: correct function signatures in the math module docs #101120
benchmark code
# bench.py

import pyperf
from cmath import sin, log

runner = pyperf.Runner()
runner.bench_func('sin(1.1)', sin, 1.1)
runner.bench_func('sin(1j)', sin, 1j)
runner.bench_func('log(2.3)', log, 2.3)
runner.bench_func('log(1+2j)', log, 1+2j)
runner.bench_func('log(2.1, 3.2)', log, 2.1, 3.2)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
skirpichev and others added 7 commits October 25, 2024 09:12
@skirpichev
pythongh-125957: sync argument naming in sphinx docs of the cmath and…
769c65f
… help()
@skirpichev
+ allow cmath.sin(z=1) syntax
f34de6a
In the main:
$ ./python -m timeit -r11 -s 'from cmath import sin;z=1j' 'sin(z)'
1000000 loops, best of 11: 312 nsec per loop

With patch:
$ ./python -m timeit -r11 -s 'from cmath import sin;z=1j' 'sin(z)'
1000000 loops, best of 11: 330 nsec per loop
@skirpichev
Merge branch 'master' into fix-cmath-sig-125957
680e7bf
@skirpichev
Revert second commit
57cc455
@skirpichev @picnixz
Apply suggestions from code review
ec3754e
Co-authored-by: Bénédikt Tran <[email protected]>
@skirpichev
Merge branch 'master' into fix-cmath-sig-125957
ab38770
@skirpichev
pythongh-131885: simplify function signatures in the cmath module
6548be0
Now function docstrings are in sync with the module docs, for example:

```pycon
>>> import cmath, inspect
>>> help(cmath.sin)
Help on built-in function sin in module cmath:

sin(z)
    Return the sine of z.

>>> inspect.signature(cmath.sin)
<Signature (z)>
```

As a side effect, this allows `sin(z=1.23)` calls.  The price is slight
performance penalty (maybe this can be fixed on AC side):

| Benchmark      | ref    | patch                |
|----------------|:------:|:--------------------:|
| sin(1.1)       | 417 ns | 428 ns: 1.03x slower |
| sin(1j)        | 414 ns | 422 ns: 1.02x slower |
| log(2.3)       | 390 ns | 409 ns: 1.05x slower |
| log(1+2j)      | 449 ns | 468 ns: 1.04x slower |
| log(2.1, 3.2)  | 601 ns | 630 ns: 1.05x slower |
| Geometric mean | (ref)  | 1.04x slower         |
@bedevere-app bedevere-app Bot added the awaiting review label Mar 30, 2025
@bedevere-app bedevere-app Bot mentioned this pull request Mar 30, 2025
Update function signatures to use * and / as needed #131885
Open
4 of 5 tasks
skirpichev added 2 commits March 30, 2025 07:04
@skirpichev
+ make regen-all
26085bb
@skirpichev
Merge branch 'main' into cmath-signatures/131885
375b117
AA-Turner
AA-Turner reviewed Apr 1, 2025
View reviewed changes

@AA-Turner AA-Turner left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the justification for these runtime changes? I don't see any value to being able to use z as a keyword argument, and we shouldn't make things slower just for aesthetics (removing the / in help()).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@AA-Turner AA-Turner added the pending The issue will be closed if no feedback is provided label Apr 1, 2025
@skirpichev

skirpichev commented Apr 1, 2025

Copy link
Copy Markdown
Member Author

What's the justification for these runtime changes?

It's explained in the pr description and (in depth) issue thread.

With this change we have correct signatures in docs, without cluttering them with syntax, which might be hard for newbies.

we shouldn't make things slower just for aesthetics (removing the / in help()).

No, it's not for aesthetics, but for readability. In pure-Python world you have to add extra syntax to have effect of positional-only arguments. This is something people have to learn and it's not common for being in recipes, tutorials, etc.

IMO, trade speed for readability is good. Though, maybe not for all cases (e.g. len() builtin?). I'm also not sure if this speed penalty can't be mitigated: this looks rather as AC issue for me.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AA-Turner

AA-Turner commented Apr 1, 2025

Copy link
Copy Markdown
Member

With this change we have correct signatures in docs

The signatures in the documentation are already correct, no? The debate over slash and star is over how precisely we describe runtime semantics. Is there something else I'm missing? The documentation should be driven by the runtime, not vice versa.

A

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@skirpichev

skirpichev commented Apr 1, 2025

Copy link
Copy Markdown
Member Author

The signatures in the documentation are already correct, no?

No. As for many modules, the cmath sphinx docs are out of sync with module docstrings.

Alternative to this pr will be adding trailing slashes to sphinx docs.

The documentation should be driven by the runtime, not vice versa.

I think that documentation should be 1) readable 2) accurately describe runtime behavior. To achieve 1) and 2) we can adjust either docs or runtime, or both.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AA-Turner

AA-Turner commented Apr 1, 2025

Copy link
Copy Markdown
Member

To be clear though, the only difference is the slash? I wouldn't describe that as 'wrong', but as 'slightly less precise'.

On the assumption that the above is true, I'll close this PR. Perhaps there is a JavaScript solution we can use to 'simplify' the documentation for beginners.

A

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AA-Turner AA-Turner closed this Apr 1, 2025
@skirpichev

skirpichev commented Apr 1, 2025

Copy link
Copy Markdown
Member Author

I wouldn't describe that as 'wrong', but as 'slightly less precise'.

@AA-Turner, we have EB decision: "If a function accepts positional-only or keyword-only arguments, include the slash and the star in the signature as appropriate: [...] Although the syntax is terse, it is precise about the allowable ways to call the function and is taken from Python itself."

Sorry, I don't see here option for compromises.

Perhaps there is a JavaScript solution we can use to 'simplify' the documentation for beginners.

Currently stars and slashes are highlighted and tooltips appear in the sphinx docs. Though, same syntax appears e.g. in the help() output without any hints for user...

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@skirpichev
skirpichev deleted the cmath-signatures/131885 branch April 1, 2025 08:41
@skirpichev skirpichev removed the pending The issue will be closed if no feedback is provided label Apr 1, 2025
@skirpichev

skirpichev commented Apr 1, 2025

Copy link
Copy Markdown
Member Author

BTW, can you change your mind if speed issue will be solved?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@skirpichev skirpichev mentioned this pull request Jun 27, 2025
gh-132908: check the return type of isnormal() and issubnormal() #136039
Closed
@skirpichev skirpichev mentioned this pull request Jul 15, 2025
Slow functions calls for C-coded functions, accepting positional-or-kwarg argument vs positional-only #136681
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@AA-Turner AA-Turner AA-Turner left review comments

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

awaiting review

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@skirpichev @AA-Turner
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.