Skip to content

Commit

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

These annotations allow for static analysers to correctly return the type
of a decorated method.
  • Loading branch information
chrisjsewell authored Oct 13, 2021
1 parent ddad09c commit 5acf437
Show file tree
Hide file tree
Showing 2 changed files with 7 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]): # pylint: disable=invalid-name
"""
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)
1 change: 1 addition & 0 deletions docs/source/nitpick-exceptions
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ py:class builtins.dict
# typing
py:class asyncio.events.AbstractEventLoop
py:class EntityType
py:class ReturnType
py:class function
py:class IO
py:class traceback
Expand Down

0 comments on commit 5acf437

Please sign in to comment.