-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
General support for properties #220
Comments
Generic descriptor support would also let us support properties, but the distinction between read-only and settable properties could be lost. A property would have type In order to properly model settable properties, we should have something like |
Read-only properties using |
Any progress on this? I am using mypy to check some new production (yes i know :)) python daemon and it worked fine until I started using properties. Do you know of any workaround? Even using the old properties notation i get issues with setters. Thanks for this great tool. |
I'm guessing you're talking about an example like this:
Indeed mypy goes crazy over this:
|
Yeah, this hasn't been implemented yet. It's been on my to-do list for a long time, but since the code I've worked with haven't used setters much I've never gotten around to actually implementing this. The implementation should not be very difficult, though. I'll try to get this working reasonably soon (though probably only after PyCon, i.e. after April 15th). |
I just pushed experimental setter support. The following code now works as expected:
The implementation still needs refinement. For example, Let me know if/when you encounter additional issues. |
Thanks, the file containing the properties now typechecks. class C:
@property
def x(self) -> Dict[str, Any]:
return self._x
@x.setter
def x(self, value: Dict[str, Any]) -> None:
self._x = value b.py def foo(c: a.C):
x = c.x
|
Do you have cyclic imports? Mypy has trouble with them (#481) in general. However, it should be pretty easy to fix mypy to support your example -- assuming that this actually is due to cyclic imports. |
This still doesn't work (reported by Tim Abbott):
|
The last example seems to be only valid in Python 2, not Python 3. |
Here's some more examples of strange property-related behavior: This code produces a mypy error as it should (the class User:
@property
def password(self) -> str:
raise AttributeError('Password is write-only')
@property.setter
def password(self, value: str) -> None:
# TODO: add hashing
self.password_hash = value
def __init__(self, username: str, password: int) -> None:
self.username = username
self.password = password Running mypy gives However, if I just rearrange the order of the methods so that the constructor is first and the property methods come after it, there's no error produced any more: class User:
def __init__(self, username: str, password: int) -> None:
self.username = username
self.password = password
@property
def password(self) -> str:
raise AttributeError('Password is write-only')
@property.setter
def password(self, value: str) -> None:
# TODO: add hashing
self.password_hash = value Running mypy on that code has no output. In addition, even the "more correct" behavior from the original code was only because the property was annotated incorrectly. Because attempting to read from mypy_extensions import NoReturn
class User:
@property
def password(self) -> NoReturn:
raise AttributeError('Password is write-only')
@property.setter
def password(self, value: str) -> None:
# TODO: add hashing
self.password_hash = value
def __init__(self, username: str, password: str) -> None:
self.username = username
self.password = password However, even though this code should now be correct, mypy produces So it seems like mypy is using the return value of the "getter" to determine the value the "setter" can accept, but that won't necessarily always match up. |
Hm, I think we ran into a similar issue (about potential discrepancies between getter and setter) for descriptors (#2266). |
edit: previously contained #3011 |
Should already be supported, see #263. |
@funkyfuture Please file a separate issue. |
So I've seen this commit a103f25 that closed #263. It seems to check for exactly "property" or "abc.abstractproperty" for property support. Is there a way to support non-stdlib property decorators such as SqlAlchemy's hybrid_property, which has getter, setter, deleter, as well as expression and comparator? I'd rather not need to litter my code with # type: ignore. |
Also, I'd be happy to fix this myself if someone @JukkaL can give me enough context to do so in less time than it'd take just to fix it themselves. |
Another thing that doesn't work came in #6045: accessing attributes of a property object in the class body (for example |
mypy cannot currently cope with the attributes of properties, incl. fget: python/mypy#220
In my comment above from March 9, 2017, I noted that putting I don't know if it's useful information at all, but I just wanted to point out that the new semantic analyzer no longer has that particular issue, and it will report the same error (which isn't actually one) regardless of whether Specifically, here's the code: from mypy_extensions import NoReturn
class User:
def __init__(self, username: str, password: str) -> None:
self.username = username
self.password = password
@property
def password(self) -> NoReturn:
raise AttributeError('Password is write-only')
@property.setter
def password(self, value: str) -> None:
# TODO: add hashing
self.password_hash = value And the output from both analyzers:
|
This comment has been minimized.
This comment has been minimized.
I have this file
And when I run mypy, I have the following errors.
If I comment out the decorators under the setters, mypy check runs OK. No errors |
@gonzaloamadio Thanks for the report. I can't help you with a quick fix. The problem is that property (both get and set functionality) is special-cased in mypy, and currently doesn't support additional decorators at all. Sorry! |
@gvanrossum I have the same issue. In my project I use additional decorators for properties very often. Thus mypy sends me hundreds of error with "error: Decorated property not supported". First of all, I don't understand the error message. Does it mean mypy does not support decorated properties, or is decorating a property related to any code quality issue in general? Because if just mypy is not supporting decorated properties, but the pattern in general is not considered as bad practice, I don't know, why mypy is giving me an error here. It should be at maximum a warning right? Second, if it is just a mypy issue, I really want to ignore it. Is there any way to ignore exactly this error on a global level? At the moment, I'm only aware of ignoring type errors for a single line, but since this pattern is extremely common in my code, I don't want to add the "ignore line" comment on hundreds of lines in my code. Thanks in advance for any suggestions and assistance. |
Decorated property has a separate issue: #1362 |
I'm closing as this covers several related issues, some of which have already been fixed, and it's hard to keep track of what's still open. I created separate issues for the remaining problems. Feel free to create additional issues if I missed something. |
Add support for type checking properties.
We should initially support at least the decorator syntax for read-only properties and the assignment syntax for both read-only and read-write properties, as these seem to be the most frequently used variants:
Also support defining a deleter and a docstring, at least when using the call syntax.
The text was updated successfully, but these errors were encountered: