Skip to content
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

🔧 MAINTAIN: Improve type annotations of @classproperty #5176

Merged
merged 3 commits into from
Oct 13, 2021

Conversation

chrisjsewell
Copy link
Member

@chrisjsewell chrisjsewell commented Oct 13, 2021

Taken from python/mypy#2266 (comment).

These annotations allow for static analysis to correctly return the correct type.

As an example:

example.py

from typing import Any, Callable, Generic, TypeVar

ReturnType = TypeVar('ReturnType')

class classproperty(Generic[ReturnType]):

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

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

class Example:

    @classproperty
    def class_property(cls) -> str:
        return 'class property'

reveal_type(Example.class_property)
$ mypy example.py
note: Revealed type is "builtins.str*"

as opposed to without annotations:

$ mypy example.py
note: Revealed type is "Any"

You can also get the correct type when using VS Code's Pylance, although pytest still seems to have problems with it (pylint-dev/pylint#1694 (comment)).

Taken from python/mypy#2266 (comment).

These annotations allow for static analysis to correctly return the correct type.
aiida/common/lang.py Outdated Show resolved Hide resolved
@codecov
Copy link

codecov bot commented Oct 13, 2021

Codecov Report

Merging #5176 (b657831) into develop (ddad09c) will increase coverage by 0.04%.
The diff coverage is 100.00%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #5176      +/-   ##
===========================================
+ Coverage    80.90%   80.93%   +0.04%     
===========================================
  Files          536      536              
  Lines        37054    37056       +2     
===========================================
+ Hits         29975    29988      +13     
+ Misses        7079     7068      -11     
Flag Coverage Δ
django 75.79% <100.00%> (+0.04%) ⬆️
sqlalchemy 74.93% <100.00%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
aiida/common/lang.py 80.00% <100.00%> (+1.06%) ⬆️
aiida/orm/nodes/process/calculation/calcjob.py 78.00% <0.00%> (+0.96%) ⬆️
aiida/engine/processes/calcjobs/tasks.py 65.78% <0.00%> (+1.01%) ⬆️
aiida/engine/daemon/client.py 76.27% <0.00%> (+1.02%) ⬆️
aiida/engine/processes/calcjobs/manager.py 86.73% <0.00%> (+3.54%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ddad09c...b657831. Read the comment docs.

@chrisjsewell chrisjsewell merged commit 5acf437 into develop Oct 13, 2021
@chrisjsewell chrisjsewell deleted the type/classproperty branch October 13, 2021 00:54
chrisjsewell added a commit that referenced this pull request Oct 19, 2021
As discussed in #5176,
pylint does not understand the `@classproperty` decorator,
and so mistakes the method as a function, rather than an attribute of the class.
This commit adds a pylint transform plugin, to remove false-positives.
(see: https://pylint.pycqa.org/en/latest/how_tos/transform_plugins.html)
chrisjsewell added a commit that referenced this pull request Oct 19, 2021
As discussed in #5176 and pylint-dev/pylint#1694,
pylint does not understand the `@classproperty` decorator,
and so mistakes the method as a function, rather than an attribute of the class.
This commit adds a pylint transform plugin, to remove false-positives.
(see: https://pylint.pycqa.org/en/latest/how_tos/transform_plugins.html)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant