Movatterモバイル変換


[0]ホーム

URL:


homepage

Issue20334

This issue trackerhas been migrated toGitHub, and is currentlyread-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title:make inspect Signature hashable
Type:enhancementStage:
Components:Library (Lib)Versions:Python 3.5
process
Status:closedResolution:fixed
Dependencies:Superseder:
Assigned To:Nosy List: Antony.Lee, brett.cannon, larry, ncoghlan, python-dev, yselivanov
Priority:normalKeywords:patch

Created on2014-01-21 16:50 byyselivanov, last changed2022-04-11 14:57 byadmin. This issue is nowclosed.

Files
File nameUploadedDescriptionEdit
hashable_signature_01.patchyselivanov,2014-01-21 16:50review
issue20334-2.01.patchyselivanov,2014-09-12 14:26review
signature-hash-and-equality.patchAntony.Lee,2014-09-12 18:02review
Messages (12)
msg208671 -(view)Author: Yury Selivanov (yselivanov)*(Python committer)Date: 2014-01-21 16:50
inspect.Signature and inspect.Parameter are immutable structures, and it makes sense to make them hashable too.Patch is attached.
msg208673 -(view)Author: Brett Cannon (brett.cannon)*(Python committer)Date: 2014-01-21 16:59
This is a new feature so it can't go into Python 3.4.
msg208674 -(view)Author: Yury Selivanov (yselivanov)*(Python committer)Date: 2014-01-21 17:00
Fair enough.
msg214962 -(view)Author: Yury Selivanov (yselivanov)*(Python committer)Date: 2014-03-27 16:14
If nobody has any objections on this, I'm going to commit this in 3.5 soon.
msg215768 -(view)Author: Roundup Robot (python-dev)(Python triager)Date: 2014-04-08 15:47
New changeset932d69ef0c63 by Yury Selivanov in branch 'default':inspect: Make Signature and Parameter hashable. Issue#20334.http://hg.python.org/cpython/rev/932d69ef0c63
msg225343 -(view)Author: Antony Lee (Antony.Lee)*Date: 2014-08-15 06:01
The hash function of the Signature class is actually incompatible with the definition of Signature equality, which doesn't consider the order of keyword-only arguments:>>> from inspect import signature>>> s1 = signature(lambda *, x, y: None); s2 = signature(lambda *, y, x: None)>>> s1 == s2True>>> hash(s1) == hash(s2)FalseActually the implementation of Signature.__eq__ seems way too complicated; I would suggest making a helper method returning (return_annotation, tuple(non-kw-only-params), frozenset(kw-only-params)) so that __eq__ can compare these values while __hash__ can hash that tuple.
msg225361 -(view)Author: Yury Selivanov (yselivanov)*(Python committer)Date: 2014-08-15 19:20
Thanks, Antony, this is a good catch. Your suggestion seems like a good idea. I'll look into this more closely soon.
msg225364 -(view)Author: Antony Lee (Antony.Lee)*Date: 2014-08-15 20:37
Actually, that specific solution (using a helper method) will fail because there may be unhashable params (due to unhashable default values or annotations) among the keyword-only arguments, so it may not be possible to build a frozenset (rather, one should compare the {param.name: param if param.kind == KEYWORD_ONLY} dict).  The frozenset approach still works for computing the hash as this requires all params to be hashable anyways.
msg226820 -(view)Author: Yury Selivanov (yselivanov)*(Python committer)Date: 2014-09-12 14:26
Antonie, I'm attaching a patch (issue20334-2.01.patch) to this issue which should fix the problem. Please review.
msg226831 -(view)Author: Antony Lee (Antony.Lee)*Date: 2014-09-12 18:02
While your patch works, I think it is a good opportunity to simplify the implementation of Signature.__eq__, which is *much* more complicated than what it should be.Please comment on the attached patch, which uses the helper method approach I suggested.
msg226840 -(view)Author: Roundup Robot (python-dev)(Python triager)Date: 2014-09-12 19:48
New changeset3b974b61e74d by Yury Selivanov in branch 'default':inspect.Signature: Fix discrepancy between __eq__ and __hash__.http://hg.python.org/cpython/rev/3b974b61e74d
msg226841 -(view)Author: Yury Selivanov (yselivanov)*(Python committer)Date: 2014-09-12 19:50
Antony, I've tweaked the patch a bit and it's now in default branch. Thank you!
History
DateUserActionArgs
2022-04-11 14:57:57adminsetgithub: 64533
2014-09-12 19:50:37yselivanovsetmessages: +msg226841
2014-09-12 19:48:11python-devsetmessages: +msg226840
2014-09-12 18:02:14Antony.Leesetfiles: +signature-hash-and-equality.patch

messages: +msg226831
2014-09-12 14:26:03yselivanovsetfiles: +issue20334-2.01.patch

messages: +msg226820
2014-08-15 20:37:59Antony.Leesetmessages: +msg225364
2014-08-15 19:20:38yselivanovsetmessages: +msg225361
2014-08-15 06:01:40Antony.Leesetnosy: +Antony.Lee
messages: +msg225343
2014-04-08 15:47:22yselivanovsetstatus: open -> closed
resolution: fixed
2014-04-08 15:47:11python-devsetnosy: +python-dev
messages: +msg215768
2014-03-27 16:14:08yselivanovsetmessages: +msg214962
2014-01-21 17:00:41yselivanovsetmessages: +msg208674
2014-01-21 16:59:10brett.cannonsetmessages: +msg208673
versions: + Python 3.5, - Python 3.4
2014-01-21 16:50:24yselivanovcreate
Supported byThe Python Software Foundation,
Powered byRoundup
Copyright © 1990-2022,Python Software Foundation
Legal Statements

[8]ページ先頭

©2009-2026 Movatter.jp