Fix the inspect bug by generating a __signature__ with a descriptor. #19
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
inspect.signature
appears to call the__get__
method of__init__
descriptors incorrectly.For:
Demo.__init__
will call the descriptor with(None, Demo)
.demo = Demo(...)
will call the descriptor with(demo, Demo)
inspect.signature(Demo)
will call the descriptor with(Demo, type(Demo))
which is incorrect in this case.If
__init__
is already generated through regular access then inspect.signature will work correctly.To work around this currently the descriptor checks if the second argument is a metaclass (subclass of 'type') and uses the first argument. This prevents the descriptor generator from being used for metaclasses. I don't have a specific use for it but I can see that potentially it could be used to generate metaclass methods.
The new fix instead provides a descriptor for
__signature__
. Inspect will look at this before trying to figure out the signature itself. This descriptor forces the generation of__init__
, removes itself from the class and then callsinspect.signature
on the class again to get the correct signature, which is then stored on__signature__
of the class and returned.