-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix forward references * Fix forward references * Add comment; Remove unused imports * Remove unused imports Fix flake8 Fix deprecated usage of 'self' * Add usage inspect.stack to get locals from scope of class declaration * Fix proper passing of class definition frame to class_schema of nested fields * Change lambda to partial * Add params im test_class_schema * Fix type annotation in dataclass decorator. Add better doc * [README] Add documentation about Union types (#184) * [README] Update README.md to address issue #183 * Update README.md * fix typo in README.md Co-authored-by: Ophir LOJKINE <[email protected]> * Fix use of typing.NewType in Python 3.10 (#180) * Fix use of typing.NewType in Python 3.10 https://docs.python.org/3.10/library/typing.html#typing.NewType "Changed in version 3.10: NewType is now a class rather than a function." * Add Python 3.10 to GitHub workflow test matrix Note the use of strings: otherwise YAML thinks we want to test Python 3.1 * Pin types-dataclasses version for Python 3.6 It looks broken for Python 3.6 due to python/typeshed@a40d79a * fix ci (#185) * Update dev dependencies * mypy has gotten smarter, and it tries to do an automatic cast that breaks our code * drop support for pre-commit hooks on 3.6 * downgrade types-dataclass Co-authored-by: Adrian Dankiv <[email protected]> Co-authored-by: Adrian Dankiv <[email protected]> Co-authored-by: Aleksander Pawlak <[email protected]> Co-authored-by: John Bodley <[email protected]> Co-authored-by: Nicolas Noirbent <[email protected]>
- Loading branch information
1 parent
228f0e8
commit fac0b6d
Showing
4 changed files
with
311 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from typing import Any, Callable | ||
|
||
|
||
__all__ = ("lazy_class_attribute",) | ||
|
||
|
||
class LazyClassAttribute: | ||
"""Descriptor decorator implementing a class-level, read-only | ||
property, which caches its results on the class(es) on which it | ||
operates. | ||
""" | ||
|
||
__slots__ = ("func", "name", "called", "forward_value") | ||
|
||
def __init__( | ||
self, func: Callable[..., Any], name: str = None, forward_value: Any = None | ||
): | ||
self.func = func | ||
self.name = name | ||
self.called = False | ||
self.forward_value = forward_value | ||
|
||
def __get__(self, instance, cls=None): | ||
if not cls: | ||
cls = type(instance) | ||
|
||
# avoid recursion | ||
if self.called: | ||
return self.forward_value | ||
|
||
self.called = True | ||
|
||
setattr(cls, self.name, self.func()) | ||
|
||
# "getattr" is used to handle bounded methods | ||
return getattr(cls, self.name) | ||
|
||
def __set_name__(self, owner, name): | ||
self.name = self.name or name | ||
|
||
|
||
lazy_class_attribute = LazyClassAttribute |
Oops, something went wrong.