Skip to content

Commit

Permalink
🔧 MAINTAIN: Improve type annotations of @classproperty
Browse files Browse the repository at this point in the history
Taken from python/mypy#2266 (comment).

These annotations allow for static analysis to correctly return the correct type.
  • Loading branch information
chrisjsewell committed Oct 13, 2021
1 parent ddad09c commit 9ad1029
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions aiida/common/lang.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import functools
import inspect
import keyword
from typing import Any, Callable, Generic, TypeVar


def isidentifier(identifier):
Expand Down Expand Up @@ -75,8 +76,10 @@ def wrapped_fn(self, *args, **kwargs): # pylint: disable=missing-docstring

override = override_decorator(check=False) # pylint: disable=invalid-name

ReturnType = TypeVar('ReturnType')

class classproperty: # pylint: disable=invalid-name

class classproperty(Generic[ReturnType]):
"""
A class that, when used as a decorator, works as if the
two decorators @property and @classmethod where applied together
Expand All @@ -85,8 +88,8 @@ class classproperty: # pylint: disable=invalid-name
instance as its first argument).
"""

def __init__(self, getter):
def __init__(self, getter: Callable[[Any], ReturnType]) -> None:
self.getter = getter

def __get__(self, instance, owner):
def __get__(self, instance: Any, owner: Any) -> ReturnType:
return self.getter(owner)

0 comments on commit 9ad1029

Please sign in to comment.