Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
67ae50ee
Commit
67ae50ee
authored
Apr 08, 2014
by
Yury Selivanov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
inspect: Make Signature and Parameter hashable. Issue #20334.
parent
3f73ca23
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
52 additions
and
13 deletions
+52
-13
Doc/library/inspect.rst
Doc/library/inspect.rst
+2
-2
Doc/whatsnew/3.5.rst
Doc/whatsnew/3.5.rst
+2
-1
Lib/inspect.py
Lib/inspect.py
+16
-0
Lib/test/test_inspect.py
Lib/test/test_inspect.py
+30
-10
Misc/NEWS
Misc/NEWS
+2
-0
No files found.
Doc/library/inspect.rst
View file @
67ae50ee
...
...
@@ -463,7 +463,7 @@ function.
modified copy.
.. versionchanged:: 3.5
Signature objects are picklable.
Signature objects are picklable
and hashable
.
.. attribute:: Signature.empty
...
...
@@ -530,7 +530,7 @@ function.
you can use :meth:`Parameter.replace` to create a modified copy.
.. versionchanged:: 3.5
Parameter objects are picklable.
Parameter objects are picklable
and hashable
.
.. attribute:: Parameter.empty
...
...
Doc/whatsnew/3.5.rst
View file @
67ae50ee
...
...
@@ -143,7 +143,8 @@ Improved Modules
(contributed by Claudiu Popa in :issue:`20627`).
* :class:`inspect.Signature` and :class:`inspect.Parameter` are now
picklable (contributed by Yury Selivanov in :issue:`20726`).
picklable and hashable (contributed by Yury Selivanov in :issue:`20726`
and :issue:`20334`).
* New class method :meth:`inspect.Signature.from_callable`, which makes
subclassing of :class:`~inspect.Signature` easier (contributed
...
...
Lib/inspect.py
View file @
67ae50ee
...
...
@@ -2231,6 +2231,16 @@ class Parameter:
return
'<{} at {:#x} "{}">'
.
format
(
self
.
__class__
.
__name__
,
id
(
self
),
self
)
def
__hash__
(
self
):
hash_tuple
=
(
self
.
name
,
int
(
self
.
kind
))
if
self
.
_annotation
is
not
_empty
:
hash_tuple
+=
(
self
.
_annotation
,)
if
self
.
_default
is
not
_empty
:
hash_tuple
+=
(
self
.
_default
,)
return
hash
(
hash_tuple
)
def
__eq__
(
self
,
other
):
return
(
issubclass
(
other
.
__class__
,
Parameter
)
and
self
.
_name
==
other
.
_name
and
...
...
@@ -2524,6 +2534,12 @@ class Signature:
return
type
(
self
)(
parameters
,
return_annotation
=
return_annotation
)
def
__hash__
(
self
):
hash_tuple
=
tuple
(
self
.
parameters
.
values
())
if
self
.
_return_annotation
is
not
_empty
:
hash_tuple
+=
(
self
.
_return_annotation
,)
return
hash
(
hash_tuple
)
def
__eq__
(
self
,
other
):
if
(
not
issubclass
(
type
(
other
),
Signature
)
or
self
.
return_annotation
!=
other
.
return_annotation
or
...
...
Lib/test/test_inspect.py
View file @
67ae50ee
...
...
@@ -2513,11 +2513,29 @@ class TestSignatureObject(unittest.TestCase):
def
bar
(
pos
,
*
args
,
c
,
b
,
a
=
42
,
**
kwargs
:
int
):
pass
self
.
assertEqual
(
inspect
.
signature
(
foo
),
inspect
.
signature
(
bar
))
def
test_signature_unhashable
(
self
):
def
test_signature_hashable
(
self
):
S
=
inspect
.
Signature
P
=
inspect
.
Parameter
def
foo
(
a
):
pass
sig
=
inspect
.
signature
(
foo
)
foo_sig
=
inspect
.
signature
(
foo
)
manual_sig
=
S
(
parameters
=
[
P
(
'a'
,
P
.
POSITIONAL_OR_KEYWORD
)])
self
.
assertEqual
(
hash
(
foo_sig
),
hash
(
manual_sig
))
self
.
assertNotEqual
(
hash
(
foo_sig
),
hash
(
manual_sig
.
replace
(
return_annotation
=
'spam'
)))
def
bar
(
a
)
->
1
:
pass
self
.
assertNotEqual
(
hash
(
foo_sig
),
hash
(
inspect
.
signature
(
bar
)))
def
foo
(
a
=
{}):
pass
with
self
.
assertRaisesRegex
(
TypeError
,
'unhashable type'
):
hash
(
sig
)
hash
(
inspect
.
signature
(
foo
))
def
foo
(
a
)
->
{}:
pass
with
self
.
assertRaisesRegex
(
TypeError
,
'unhashable type'
):
hash
(
inspect
.
signature
(
foo
))
def
test_signature_str
(
self
):
def
foo
(
a
:
int
=
1
,
*
,
b
,
c
=
None
,
**
kwargs
)
->
42
:
...
...
@@ -2651,6 +2669,15 @@ class TestParameterObject(unittest.TestCase):
self
.
assertTrue
(
repr
(
p
).
startswith
(
'<Parameter'
))
self
.
assertTrue
(
'"a=42"'
in
repr
(
p
))
def
test_signature_parameter_hashable
(
self
):
P
=
inspect
.
Parameter
foo
=
P
(
'foo'
,
kind
=
P
.
POSITIONAL_ONLY
)
self
.
assertEqual
(
hash
(
foo
),
hash
(
P
(
'foo'
,
kind
=
P
.
POSITIONAL_ONLY
)))
self
.
assertNotEqual
(
hash
(
foo
),
hash
(
P
(
'foo'
,
kind
=
P
.
POSITIONAL_ONLY
,
default
=
42
)))
self
.
assertNotEqual
(
hash
(
foo
),
hash
(
foo
.
replace
(
kind
=
P
.
VAR_POSITIONAL
)))
def
test_signature_parameter_equality
(
self
):
P
=
inspect
.
Parameter
p
=
P
(
'foo'
,
default
=
42
,
kind
=
inspect
.
Parameter
.
KEYWORD_ONLY
)
...
...
@@ -2661,13 +2688,6 @@ class TestParameterObject(unittest.TestCase):
self
.
assertEqual
(
p
,
P
(
'foo'
,
default
=
42
,
kind
=
inspect
.
Parameter
.
KEYWORD_ONLY
))
def
test_signature_parameter_unhashable
(
self
):
p
=
inspect
.
Parameter
(
'foo'
,
default
=
42
,
kind
=
inspect
.
Parameter
.
KEYWORD_ONLY
)
with
self
.
assertRaisesRegex
(
TypeError
,
'unhashable type'
):
hash
(
p
)
def
test_signature_parameter_replace
(
self
):
p
=
inspect
.
Parameter
(
'foo'
,
default
=
42
,
kind
=
inspect
.
Parameter
.
KEYWORD_ONLY
)
...
...
Misc/NEWS
View file @
67ae50ee
...
...
@@ -154,6 +154,8 @@ Library
positional
-
or
-
keyword
arguments
passed
as
keyword
arguments
become
keyword
-
only
.
-
Issue
#
20334
:
inspect
.
Signature
and
inspect
.
Parameter
are
now
hashable
.
IDLE
----
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment