Skip to content

Commit

Permalink
mesonlib: allow using lazy_property with "__"-named properties
Browse files Browse the repository at this point in the history
These are given a name that is different from __func.__name__, so
store the name when the descriptor is placed in the class.

Signed-off-by: Paolo Bonzini <[email protected]>
  • Loading branch information
bonzini committed Jan 10, 2025
1 parent d92769a commit abe89ef
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions mesonbuild/utils/universal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2392,10 +2392,17 @@ class lazy_property(T.Generic[_T]):
Due to Python's MRO that means that the calculated value will be found
before this property, speeding up subsequent lookups.
"""
def __init__(self, func: T.Callable[[T.Any], _T]):
def __init__(self, func: T.Callable[[T.Any], _T]) -> None:
self.__name: T.Optional[str] = None
self.__func = func

def __set_name__(self, owner: T.Any, name: str) -> None:
if self.__name is None:
self.__name = name
else:
assert name == self.__name

def __get__(self, instance: object, cls: T.Type) -> _T:
value = self.__func(instance)
setattr(instance, self.__func.__name__, value)
setattr(instance, self.__name, value)
return value

0 comments on commit abe89ef

Please sign in to comment.