Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent _GufeTozenizableMeta.__call__ from overwriting signature of __init__ #216

Merged
merged 2 commits into from
Aug 10, 2023

Conversation

Yoshanuikabundi
Copy link
Contributor

When the inspect module of the standard library is used to get the signature of a GufeTokenizable class, it sees that the metaclass has an implementation of __call__ and takes the signature from that instead of from the __new__ or __init__ method of the child class:

>>> from gufe import SmallMoleculeComponent
>>> import inspect
>>> inspect.signature(SmallMoleculeComponent)
<Signature (*args, **kwargs)>
>>> inspect.signature(SmallMoleculeComponent.__init__)
<Signature (self, rdkit: rdkit.Chem.rdchem.Mol, name: str = '')>

This results in unhelpful API documentation:

SmallMoleculeComponent_signature_before

But also may affect the signature displayed by IDEs, the Jupyter help magic, and so on.

This PR restores the signature from __init__ or __new__ when a child class is created:

SmallMoleculeComponent_signature_after

(I've taken screenshots from the GUFE docs, but this problem affects the OpenFE docs as well, and this solution fixes them too)

See https://stackoverflow.com/questions/49740290/call-from-metaclass-shadows-signature-of-init. I originally wrote a solution defining a __signature__ property on the metaclass, but this solution seems more... canonical.

@codecov
Copy link

codecov bot commented Aug 10, 2023

Codecov Report

Patch coverage: 88.88% and project coverage change: -0.06% ⚠️

Comparison is base (799b5b2) 99.09% compared to head (2d5f27d) 99.04%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #216      +/-   ##
==========================================
- Coverage   99.09%   99.04%   -0.06%     
==========================================
  Files          36       36              
  Lines        1773     1782       +9     
==========================================
+ Hits         1757     1765       +8     
- Misses         16       17       +1     
Files Changed Coverage Δ
gufe/tokenization.py 98.10% <88.88%> (-0.42%) ⬇️

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@richardjgowers richardjgowers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @Yoshanuikabundi this had been bugging me

@richardjgowers richardjgowers merged commit fdcfe3b into main Aug 10, 2023
4 of 6 checks passed
@richardjgowers richardjgowers deleted the tokenizable_signature branch August 10, 2023 10:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants