-
Notifications
You must be signed in to change notification settings - Fork 0
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
CachedClassProps and CachedProp really don't play well with any tooling. #86
Comments
Seems that the 'official' way to achieve class properties is now wrapping methods with @Property and @classmethod: https://docs.python.org/3.9/library/functions.html#classmethod (python/cpython#63272) And I believe this is also supported by Sphinx specifically: But I'm not completely sure: |
It may be possible to re-write e.g. class _ClsCached:
def __init__(self, func: Callable[[Type[T]], V]) -> None:
self.func = func
self.val_set = False
self.val = None
def __call__(self, cls: Type[T]) -> V:
if self.val is not None:
return self.val
self.val = self.func(cls)
#self.val_set = True
return self.val
def _cached_class_prop(wrapped: Callable[[Type[T]], V]):
prop = property(_ClsCached(wrapped))
cls_meth = classmethod(prop)
return cls_meth The only thing is this is all 3.9 and above, so would have to bump Python version dependency. |
Eugh, ok this was added in 3.9 and deprecated in 3.11: And mypy is responding accordingly: |
Then goal of these methods is a way to set default values for certain attributes if they're not defined by subclasses... Maybe there is another way? |
For the class ones I can just do this!
|
See #131, I went with the option above. I don't like stacking decorators, it's so confusing. I think this implementation makes sense. If users subclass, then they sever the connection with the default setter. In theory we could probably simplify the implementation of Will close when merged. |
The accessors continue to cause chaos.
Sphinx autodoc doesn't manage to generate docstrings for any CachedClassProps, presumably because it just gets those attributes from the class, which get returned as the cached value.
This means things like
TierBase.name_part_template
are missing from the docs - they're important!A solution may be found add some sort of preprocessor or whatever to Sphinx:
https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html#event-autodoc-process-docstring
The CachedClassProps can be fetch from the classes using the syntax (which bypasses the accessor protocol):
and it seems the wraps is passing on the correct docstring. So it should be possible to generate the docstring I think, provided we can find a way to get the right object to Sphinx.
The text was updated successfully, but these errors were encountered: