OpenFreeEnergy / gufe

grand unified free energy by OpenFE
https://gufe.readthedocs.io
MIT License
28 stars 8 forks source link

Prevent `_GufeTozenizableMeta.__call__` from overwriting signature of `__init__` #216

Closed Yoshanuikabundi closed 1 year ago

Yoshanuikabundi commented 1 year ago

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[bot] commented 1 year ago

Codecov Report

Patch coverage: 88.88% and project coverage change: -0.06% :warning:

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

Additional details and impacted files ```diff @@ 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](https://app.codecov.io/gh/OpenFreeEnergy/gufe/pull/216?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=OpenFreeEnergy) | Coverage Δ | | |---|---|---| | [gufe/tokenization.py](https://app.codecov.io/gh/OpenFreeEnergy/gufe/pull/216?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=OpenFreeEnergy#diff-Z3VmZS90b2tlbml6YXRpb24ucHk=) | `98.10% <88.88%> (-0.42%)` | :arrow_down: |

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.