-
Notifications
You must be signed in to change notification settings - Fork 8
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
Question about using zope with PyCharm and typehints? #14
Comments
Kevin Mu wrote at 2020-5-13 21:28 -0700:
Consider the following python 3.6 script that utilizes both zope and the builtin property decorator:
```
from zope.cachedescriptors import property as zproperty
import math
class Point:
def __init__(self, x, y):
self.x, self.y = x, y
@Property
def builtin_radius(self) -> int:
print('computing builtin radius')
return math.sqrt(self.x**2 + self.y**2)
@zproperty.readproperty
def zope_radius(self) -> int:
print('computing zope radius')
return math.sqrt(self.x**2 + self.y**2)
if __name__ == "__main__":
point = Point(1, 2);
print(point.builtin_radius)
print(point.zope_radius)
```
It seems that PyCharm is able to detect that `point.buildin_radius` has type int, but when writing `point.zope_radius`, PyCharm stll thinks that the type is `Callable[[], int]`. The same is true for `property.cachedIn` and `property.CachedProperty`.
I'm wondering if there is a way to work around this? Would something like the following snippet work, or would that change the behavior of zope?
```
@Property
@zproperty.readproperty
def zope_radius(self) -> int:
print('computing zope radius')
return math.sqrt(self.x**2 + self.y**2)
```
Appreciate your help!
Most decorators wrap a function and return typically another function
with the same signature; this means, that usually the type hint for
a function can be reused for the wrapped function.
`readproperty` is an exception: it turns a function into a
descriptor with a different signature. Apparently, `zope.cachedescriptors`
does not yet take care of this.
I think the best way would be a new version of `zope.cachedescriptor`s
which handles type hints correctly - emulating logic from `property`.
I do not think that `@property @readproperty` will work:
it may get the type hint right but it will likely fail when
executed: `@property` expects a function but `@readproperty` will
result in an atom.
Of course, you can define a (new) decorator which applies
the type hint related logic of `property` to fix up the wrong
type hint produced by `readproperty`.
|
I see, thanks @d-maurer ; appreciate the quick response! Yeah, I kind of quickly realized the
Thanks again for your help! :) |
What logic would that be? I was under the impression that the builtin All of the property-like descriptors defined here already use |
Functionally, more-or-less. That's the distinction between the two.
Close. The distinction is that the code in
No, not even close. Some of the above should probably more clearly be spelled out in the documentation.
Maybe? I haven't looked into how it works. But it's unlikely to use |
Jason Madden wrote at 2020-5-14 03:26 -0700:
> I think the best way would be a new version of `zope.cachedescriptor`s which handles type hints correctly - emulating logic from `property`….you can define a (new) decorator which applies
the type hint related logic of `property`
What logic would that be? I was under the impression that the builtin ***@***.***` decorator was simply special cased by IDEs. There's nothing in [the standard descriptor protocol](https://docs.python.org/3/reference/datamodel.html#implementing-descriptors) that has anything to do with type hints.
I do not know. I have just interpreted the reported observations
that "pyCharm" reports the correct type hint for `@property`.
If the logic is in the IDE rather than `property`, then this logic
should be emulated by the `property` like decorators of
`zope.cachedecriptors`.
All of the property-like descriptors defined here already use `functools.update_wrapper` to copy as much information from the underlying function as the standard library allows.
(As I wrote), this is approriate for decorators which do not change
the signature; it is not appropriate for `property` and friends
(which change the signature).
|
Based on this (very long) thread, mypy has special cases for
I continue to imagine that other type-checkers and static analysis tools are the same. I don't think there's anything that this package can do, the individual type-checkers will have to get full-featured descriptor protocol support. |
As said by @jamadden: This cannot be fixed in this package, so closing the issue. |
FWIW, this is the configuration snippet I use to get pylint to treat the cachedescriptors as properties: [MASTER]
# Fix zope.cachedescriptors.property.Lazy; the property-classes doesn't seem to
# do anything.
# https://stackoverflow.com/questions/51160955/pylint-how-to-specify-a-self-defined-property-decorator-with-property-classes
# For releases prior to 2.14.2, this needs to be a one-line, quoted string. After that,
# a multi-line string.
# - Make zope.cachedescriptors.property.Lazy look like a property;
# fixes pylint thinking it is a method.
# - Run in Pure Python mode (ignore C extensions that respect this);
# fixes some issues with zope.interface, like IFoo.providedby(ob)
# claiming not to have the right number of parameters...except no, it does not.
init-hook =
import astroid.bases
astroid.bases.POSSIBLE_PROPERTIES.add('Lazy')
astroid.bases.POSSIBLE_PROPERTIES.add('readproperty')
astroid.bases.POSSIBLE_PROPERTIES.add('non_overridable')
import os
os.environ['PURE_PYTHON'] = ("1")
# Ending on a quoted string
# breaks pylint 2.14.5 (it strips the trailing quote. This is
# probably because it tries to handle one-line quoted strings as well as multi-blocks).
# The parens around it fix the issue. |
Consider the following python 3.6 script that utilizes both zope and the builtin property decorator:
It seems that PyCharm is able to detect that
point.buildin_radius
has type int, but when writingpoint.zope_radius
, PyCharm stll thinks that the type isCallable[[], int]
. The same is true forproperty.cachedIn
andproperty.CachedProperty
.I'm wondering if there is a way to work around this and get the correct typehints in Pycharm? I'm asking here because I don't /think/ there are any settings in Pycharm that can be changed to get the desired behavior; I believe it the change would have to come from the code. Thanks, really appreciate your help!
The text was updated successfully, but these errors were encountered: