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
e7dcc5e9
Commit
e7dcc5e9
authored
Jan 27, 2014
by
Yury Selivanov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
inspect.signature: Support classes without user-defined __init__/__new__ #20308
parent
7aedea40
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
28 additions
and
0 deletions
+28
-0
Lib/inspect.py
Lib/inspect.py
+11
-0
Lib/test/test_inspect.py
Lib/test/test_inspect.py
+14
-0
Misc/NEWS
Misc/NEWS
+3
-0
No files found.
Lib/inspect.py
View file @
e7dcc5e9
...
...
@@ -1561,6 +1561,17 @@ def signature(obj):
init
=
_get_user_defined_method
(
obj
,
'__init__'
)
if
init
is
not
None
:
sig
=
signature
(
init
)
if
sig
is
None
:
if
type
in
obj
.
__mro__
:
# 'obj' is a metaclass without user-defined __init__
# or __new__. Return a signature of 'type' builtin.
return
signature
(
type
)
else
:
# We have a class (not metaclass), but no user-defined
# __init__ or __new__ for it
return
signature
(
object
)
elif
not
isinstance
(
obj
,
_NonUserDefinedCallables
):
# An object with __call__
# We also check that the 'obj' is not an instance of
...
...
Lib/test/test_inspect.py
View file @
e7dcc5e9
...
...
@@ -2045,6 +2045,20 @@ class TestSignatureObject(unittest.TestCase):
(
'bar'
,
2
,
...,
"keyword_only"
)),
...))
# Test classes without user-defined __init__ or __new__
class
C
:
pass
self
.
assertEqual
(
str
(
inspect
.
signature
(
C
)),
'()'
)
class
D
(
C
):
pass
self
.
assertEqual
(
str
(
inspect
.
signature
(
D
)),
'()'
)
# Test meta-classes without user-defined __init__ or __new__
class
C
(
type
):
pass
self
.
assertEqual
(
str
(
inspect
.
signature
(
C
)),
'(object_or_name, bases, dict)'
)
class
D
(
C
):
pass
self
.
assertEqual
(
str
(
inspect
.
signature
(
D
)),
'(object_or_name, bases, dict)'
)
def
test_signature_on_callable_objects
(
self
):
class
Foo
:
def
__call__
(
self
,
a
):
...
...
Misc/NEWS
View file @
e7dcc5e9
...
...
@@ -13,6 +13,9 @@ Core and Builtins
Library
-------
- Issue #20308: inspect.signature now works on classes without user-defined
__init__ or __new__ methods.
What'
s
New
in
Python
3.4.0
Beta
3
?
==================================
...
...
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