classification
Title: inspect.signature does not respect PEP 8
Type: behavior Stage: resolved
Components: Versions: Python 3.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: corona10, eric.smith, gvanrossum, levkivskyi, r.david.murray, yselivanov
Priority: normal Keywords: patch

Created on 2017-11-13 22:06 by levkivskyi, last changed 2017-11-15 18:31 by yselivanov. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 4408 merged corona10, 2017-11-15 17:36
Messages (6)
msg306171 - (view) Author: Ivan Levkivskyi (levkivskyi) * Date: 2017-11-13 22:06
The string representation of a function signature with annotations is currently like this:

>>> def __init__(self, x: int = 1, y: int = 2) -> None: pass
... 
>>> import inspect
>>> str(inspect.signature(__init__))
'(self, x:str=1, y:int=2) -> None'

At the same time PEP 8 says:

When combining an argument annotation with a default value, use spaces around the = sign (but only for those arguments that have both an annotation and a default).

Yes:

def munge(sep: AnyStr = None): ...
def munge(input: AnyStr, sep: AnyStr = None, limit=1000): ...

No:

def munge(input: AnyStr=None): ...
def munge(input: AnyStr, limit = 1000): ...

I think there should be spaces in the signature repr.
msg306172 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-11-13 22:11
FWIW, I find the version without the spaces to be more readable (but I don't find annotations to be readable in general, so my opinion may not be worth much :)
msg306174 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2017-11-13 22:49
Those people who read and write annotations regularly are all using the convention that was added to PEP 8, so let's make inspect follow that lead rather than argue about it here. :-)
msg306175 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-11-13 22:53
Agreed.
msg306295 - (view) Author: Dong-hee Na (corona10) * Date: 2017-11-15 18:25
I've submitted a patch. :)
msg306296 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2017-11-15 18:31
New changeset 762b9571c9c8c6b036f1bf90140a1d030b3f9a01 by Yury Selivanov (Dong-hee Na) in branch 'master':
bpo-32018: Fix inspect.signature repr to follow PEP 8 (#4408)
https://github.com/python/cpython/commit/762b9571c9c8c6b036f1bf90140a1d030b3f9a01
History
Date User Action Args
2017-11-15 18:31:22yselivanovsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2017-11-15 18:31:06yselivanovsetmessages: + msg306296
2017-11-15 18:25:51corona10setmessages: + msg306295
2017-11-15 17:46:28corona10setnosy: + corona10
2017-11-15 17:36:41corona10setkeywords: + patch
stage: patch review
pull_requests: + pull_request4357
2017-11-13 22:53:52r.david.murraysetmessages: + msg306175
2017-11-13 22:49:59gvanrossumsetnosy: + gvanrossum
messages: + msg306174
2017-11-13 22:36:06eric.smithsetnosy: + eric.smith
2017-11-13 22:11:27r.david.murraysetnosy: + r.david.murray
messages: + msg306172
2017-11-13 22:06:50levkivskyicreate