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
2489bd5d
Commit
2489bd5d
authored
Jul 18, 2015
by
Serhiy Storchaka
Browse files
Options
Browse Files
Download
Plain Diff
Issue #24206: Fixed __eq__ and __ne__ methods of inspect classes.
parents
f12e3e2b
3018cc49
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
76 additions
and
45 deletions
+76
-45
Lib/inspect.py
Lib/inspect.py
+19
-22
Lib/test/test_inspect.py
Lib/test/test_inspect.py
+55
-23
Misc/NEWS
Misc/NEWS
+2
-0
No files found.
Lib/inspect.py
View file @
2489bd5d
...
...
@@ -2488,15 +2488,14 @@ class Parameter:
return
hash
((
self
.
name
,
self
.
kind
,
self
.
annotation
,
self
.
default
))
def
__eq__
(
self
,
other
):
return
(
self
is
other
or
(
issubclass
(
other
.
__class__
,
Parameter
)
and
self
.
_name
==
other
.
_name
and
self
.
_kind
==
other
.
_kind
and
self
.
_default
==
other
.
_default
and
self
.
_annotation
==
other
.
_annotation
))
def
__ne__
(
self
,
other
):
return
not
self
.
__eq__
(
other
)
if
self
is
other
:
return
True
if
not
isinstance
(
other
,
Parameter
):
return
NotImplemented
return
(
self
.
_name
==
other
.
_name
and
self
.
_kind
==
other
.
_kind
and
self
.
_default
==
other
.
_default
and
self
.
_annotation
==
other
.
_annotation
)
class
BoundArguments
:
...
...
@@ -2610,13 +2609,12 @@ class BoundArguments:
self
.
arguments
=
OrderedDict
(
new_arguments
)
def
__eq__
(
self
,
other
):
return
(
self
is
other
or
(
issubclass
(
other
.
__class__
,
BoundArguments
)
and
self
.
signature
==
other
.
signature
and
self
.
arguments
==
other
.
arguments
))
def
__ne__
(
self
,
other
):
return
not
self
.
__eq__
(
other
)
if
self
is
other
:
return
True
if
not
isinstance
(
other
,
BoundArguments
):
return
NotImplemented
return
(
self
.
signature
==
other
.
signature
and
self
.
arguments
==
other
.
arguments
)
def
__setstate__
(
self
,
state
):
self
.
_signature
=
state
[
'_signature'
]
...
...
@@ -2775,12 +2773,11 @@ class Signature:
return
hash
((
params
,
kwo_params
,
return_annotation
))
def
__eq__
(
self
,
other
):
return
(
self
is
other
or
(
isinstance
(
other
,
Signature
)
and
self
.
_hash_basis
()
==
other
.
_hash_basis
()))
def
__ne__
(
self
,
other
):
return
not
self
.
__eq__
(
other
)
if
self
is
other
:
return
True
if
not
isinstance
(
other
,
Signature
):
return
NotImplemented
return
self
.
_hash_basis
()
==
other
.
_hash_basis
()
def
_bind
(
self
,
args
,
kwargs
,
*
,
partial
=
False
):
"""Private method. Don't use directly."""
...
...
Lib/test/test_inspect.py
View file @
2489bd5d
...
...
@@ -89,6 +89,9 @@ def gen_coroutine_function_example(self):
yield
return
'spam'
class
EqualsToAll
:
def
__eq__
(
self
,
other
):
return
True
class
TestPredicates
(
IsTestBase
):
...
...
@@ -2672,69 +2675,84 @@ class TestSignatureObject(unittest.TestCase):
def
test_signature_equality
(
self
):
def
foo
(
a
,
*
,
b
:
int
)
->
float
:
pass
self
.
assertNotEqual
(
inspect
.
signature
(
foo
),
42
)
self
.
assertFalse
(
inspect
.
signature
(
foo
)
==
42
)
self
.
assertTrue
(
inspect
.
signature
(
foo
)
!=
42
)
self
.
assertTrue
(
inspect
.
signature
(
foo
)
==
EqualsToAll
())
self
.
assertFalse
(
inspect
.
signature
(
foo
)
!=
EqualsToAll
())
def
bar
(
a
,
*
,
b
:
int
)
->
float
:
pass
self
.
assertEqual
(
inspect
.
signature
(
foo
),
inspect
.
signature
(
bar
))
self
.
assertTrue
(
inspect
.
signature
(
foo
)
==
inspect
.
signature
(
bar
))
self
.
assertFalse
(
inspect
.
signature
(
foo
)
!=
inspect
.
signature
(
bar
))
self
.
assertEqual
(
hash
(
inspect
.
signature
(
foo
)),
hash
(
inspect
.
signature
(
bar
)))
def
bar
(
a
,
*
,
b
:
int
)
->
int
:
pass
self
.
assertNotEqual
(
inspect
.
signature
(
foo
),
inspect
.
signature
(
bar
))
self
.
assertFalse
(
inspect
.
signature
(
foo
)
==
inspect
.
signature
(
bar
))
self
.
assertTrue
(
inspect
.
signature
(
foo
)
!=
inspect
.
signature
(
bar
))
self
.
assertNotEqual
(
hash
(
inspect
.
signature
(
foo
)),
hash
(
inspect
.
signature
(
bar
)))
def
bar
(
a
,
*
,
b
:
int
):
pass
self
.
assertNotEqual
(
inspect
.
signature
(
foo
),
inspect
.
signature
(
bar
))
self
.
assertFalse
(
inspect
.
signature
(
foo
)
==
inspect
.
signature
(
bar
))
self
.
assertTrue
(
inspect
.
signature
(
foo
)
!=
inspect
.
signature
(
bar
))
self
.
assertNotEqual
(
hash
(
inspect
.
signature
(
foo
)),
hash
(
inspect
.
signature
(
bar
)))
def
bar
(
a
,
*
,
b
:
int
=
42
)
->
float
:
pass
self
.
assertNotEqual
(
inspect
.
signature
(
foo
),
inspect
.
signature
(
bar
))
self
.
assertFalse
(
inspect
.
signature
(
foo
)
==
inspect
.
signature
(
bar
))
self
.
assertTrue
(
inspect
.
signature
(
foo
)
!=
inspect
.
signature
(
bar
))
self
.
assertNotEqual
(
hash
(
inspect
.
signature
(
foo
)),
hash
(
inspect
.
signature
(
bar
)))
def
bar
(
a
,
*
,
c
)
->
float
:
pass
self
.
assertNotEqual
(
inspect
.
signature
(
foo
),
inspect
.
signature
(
bar
))
self
.
assertFalse
(
inspect
.
signature
(
foo
)
==
inspect
.
signature
(
bar
))
self
.
assertTrue
(
inspect
.
signature
(
foo
)
!=
inspect
.
signature
(
bar
))
self
.
assertNotEqual
(
hash
(
inspect
.
signature
(
foo
)),
hash
(
inspect
.
signature
(
bar
)))
def
bar
(
a
,
b
:
int
)
->
float
:
pass
self
.
assertNotEqual
(
inspect
.
signature
(
foo
),
inspect
.
signature
(
bar
))
self
.
assertFalse
(
inspect
.
signature
(
foo
)
==
inspect
.
signature
(
bar
))
self
.
assertTrue
(
inspect
.
signature
(
foo
)
!=
inspect
.
signature
(
bar
))
self
.
assertNotEqual
(
hash
(
inspect
.
signature
(
foo
)),
hash
(
inspect
.
signature
(
bar
)))
def
spam
(
b
:
int
,
a
)
->
float
:
pass
self
.
assertNotEqual
(
inspect
.
signature
(
spam
),
inspect
.
signature
(
bar
))
self
.
assertFalse
(
inspect
.
signature
(
spam
)
==
inspect
.
signature
(
bar
))
self
.
assertTrue
(
inspect
.
signature
(
spam
)
!=
inspect
.
signature
(
bar
))
self
.
assertNotEqual
(
hash
(
inspect
.
signature
(
spam
)),
hash
(
inspect
.
signature
(
bar
)))
def
foo
(
*
,
a
,
b
,
c
):
pass
def
bar
(
*
,
c
,
b
,
a
):
pass
self
.
assertEqual
(
inspect
.
signature
(
foo
),
inspect
.
signature
(
bar
))
self
.
assertTrue
(
inspect
.
signature
(
foo
)
==
inspect
.
signature
(
bar
))
self
.
assertFalse
(
inspect
.
signature
(
foo
)
!=
inspect
.
signature
(
bar
))
self
.
assertEqual
(
hash
(
inspect
.
signature
(
foo
)),
hash
(
inspect
.
signature
(
bar
)))
def
foo
(
*
,
a
=
1
,
b
,
c
):
pass
def
bar
(
*
,
c
,
b
,
a
=
1
):
pass
self
.
assertEqual
(
inspect
.
signature
(
foo
),
inspect
.
signature
(
bar
))
self
.
assertTrue
(
inspect
.
signature
(
foo
)
==
inspect
.
signature
(
bar
))
self
.
assertFalse
(
inspect
.
signature
(
foo
)
!=
inspect
.
signature
(
bar
))
self
.
assertEqual
(
hash
(
inspect
.
signature
(
foo
)),
hash
(
inspect
.
signature
(
bar
)))
def
foo
(
pos
,
*
,
a
=
1
,
b
,
c
):
pass
def
bar
(
pos
,
*
,
c
,
b
,
a
=
1
):
pass
self
.
assertEqual
(
inspect
.
signature
(
foo
),
inspect
.
signature
(
bar
))
self
.
assertTrue
(
inspect
.
signature
(
foo
)
==
inspect
.
signature
(
bar
))
self
.
assertFalse
(
inspect
.
signature
(
foo
)
!=
inspect
.
signature
(
bar
))
self
.
assertEqual
(
hash
(
inspect
.
signature
(
foo
)),
hash
(
inspect
.
signature
(
bar
)))
def
foo
(
pos
,
*
,
a
,
b
,
c
):
pass
def
bar
(
pos
,
*
,
c
,
b
,
a
=
1
):
pass
self
.
assertNotEqual
(
inspect
.
signature
(
foo
),
inspect
.
signature
(
bar
))
self
.
assertFalse
(
inspect
.
signature
(
foo
)
==
inspect
.
signature
(
bar
))
self
.
assertTrue
(
inspect
.
signature
(
foo
)
!=
inspect
.
signature
(
bar
))
self
.
assertNotEqual
(
hash
(
inspect
.
signature
(
foo
)),
hash
(
inspect
.
signature
(
bar
)))
def
foo
(
pos
,
*
args
,
a
=
42
,
b
,
c
,
**
kwargs
:
int
):
pass
def
bar
(
pos
,
*
args
,
c
,
b
,
a
=
42
,
**
kwargs
:
int
):
pass
self
.
assertEqual
(
inspect
.
signature
(
foo
),
inspect
.
signature
(
bar
))
self
.
assertTrue
(
inspect
.
signature
(
foo
)
==
inspect
.
signature
(
bar
))
self
.
assertFalse
(
inspect
.
signature
(
foo
)
!=
inspect
.
signature
(
bar
))
self
.
assertEqual
(
hash
(
inspect
.
signature
(
foo
)),
hash
(
inspect
.
signature
(
bar
)))
...
...
@@ -2907,11 +2925,17 @@ class TestParameterObject(unittest.TestCase):
P
=
inspect
.
Parameter
p
=
P
(
'foo'
,
default
=
42
,
kind
=
inspect
.
Parameter
.
KEYWORD_ONLY
)
self
.
assertEqual
(
p
,
p
)
self
.
assertNotEqual
(
p
,
42
)
self
.
assertTrue
(
p
==
p
)
self
.
assertFalse
(
p
!=
p
)
self
.
assertFalse
(
p
==
42
)
self
.
assertTrue
(
p
!=
42
)
self
.
assertTrue
(
p
==
EqualsToAll
())
self
.
assertFalse
(
p
!=
EqualsToAll
())
self
.
assertEqual
(
p
,
P
(
'foo'
,
default
=
42
,
kind
=
inspect
.
Parameter
.
KEYWORD_ONLY
))
self
.
assertTrue
(
p
==
P
(
'foo'
,
default
=
42
,
kind
=
inspect
.
Parameter
.
KEYWORD_ONLY
))
self
.
assertFalse
(
p
!=
P
(
'foo'
,
default
=
42
,
kind
=
inspect
.
Parameter
.
KEYWORD_ONLY
))
def
test_signature_parameter_replace
(
self
):
p
=
inspect
.
Parameter
(
'foo'
,
default
=
42
,
...
...
@@ -3215,25 +3239,33 @@ class TestBoundArguments(unittest.TestCase):
def
test_signature_bound_arguments_equality
(
self
):
def
foo
(
a
):
pass
ba
=
inspect
.
signature
(
foo
).
bind
(
1
)
self
.
assertEqual
(
ba
,
ba
)
self
.
assertTrue
(
ba
==
ba
)
self
.
assertFalse
(
ba
!=
ba
)
self
.
assertTrue
(
ba
==
EqualsToAll
())
self
.
assertFalse
(
ba
!=
EqualsToAll
())
ba2
=
inspect
.
signature
(
foo
).
bind
(
1
)
self
.
assertEqual
(
ba
,
ba2
)
self
.
assertTrue
(
ba
==
ba2
)
self
.
assertFalse
(
ba
!=
ba2
)
ba3
=
inspect
.
signature
(
foo
).
bind
(
2
)
self
.
assertNotEqual
(
ba
,
ba3
)
self
.
assertFalse
(
ba
==
ba3
)
self
.
assertTrue
(
ba
!=
ba3
)
ba3
.
arguments
[
'a'
]
=
1
self
.
assertEqual
(
ba
,
ba3
)
self
.
assertTrue
(
ba
==
ba3
)
self
.
assertFalse
(
ba
!=
ba3
)
def
bar
(
b
):
pass
ba4
=
inspect
.
signature
(
bar
).
bind
(
1
)
self
.
assertNotEqual
(
ba
,
ba4
)
self
.
assertFalse
(
ba
==
ba4
)
self
.
assertTrue
(
ba
!=
ba4
)
def
foo
(
*
,
a
,
b
):
pass
sig
=
inspect
.
signature
(
foo
)
ba1
=
sig
.
bind
(
a
=
1
,
b
=
2
)
ba2
=
sig
.
bind
(
b
=
2
,
a
=
1
)
self
.
assertEqual
(
ba1
,
ba2
)
self
.
assertTrue
(
ba1
==
ba2
)
self
.
assertFalse
(
ba1
!=
ba2
)
def
test_signature_bound_arguments_pickle
(
self
):
def
foo
(
a
,
b
,
*
,
c
:
1
=
{},
**
kw
)
->
{
42
:
'ham'
}:
pass
...
...
Misc/NEWS
View file @
2489bd5d
...
...
@@ -19,6 +19,8 @@ Core and Builtins
Library
-------
- Issue #24206: Fixed __eq__ and __ne__ methods of inspect classes.
- Issue #24631: Fixed regression in the timeit module with multiline setup.
- Issue #18622: unittest.mock.mock_open().reset_mock would recurse infinitely.
...
...
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