-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Mypy forbids converting a method to a property in some cases #3913
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
Comments
It looks like a very similar error has been observed in #3625 (comment), so maybe this is a duplicate of #3223 modulo class -> metaclass? @elazarg are you working on this? It looks like your PR #3227 should help with this issue. |
I can't verify this right now, but looks like it should be resolved by #3227. |
Good news. Thanks! |
Apparently this is not a precise duplicate, or #3227 is not a complete fix. Trying to use this with any version from 0.521/master/#3227, you get:
The first two errors are technically correct, although possibly too strict in practice. This is already discussed elsewhere. I believe I should augment #3227 to fix this problem too. |
This would be great! |
The problem seems to be that Specifically, adding
which means the function is no longer generic at this point, its type variable are already instantiated by the statement I assume the solution must be to use a function (I write this here since I "reveal" it over and over again; also @ilevkivskyi I wonder if that makes sense to you) So, this seems to require a different fix than #3227. I think its better not to augment it. |
Or alternatively you could just generalize It is OK (and maybe even desirable) if you do this in a separate PR. |
Hmph. After another couple of hours debugging this, I've found the culprit: class Meta(type):
def method_to_access_a_special_instance(cls: Type[_Base]) -> _Base:
# ^Good.
return None # type: ignore
@property
def property_to_access_a_special_instance(cls: Type[_Base]) -> Base:
# ^oops, should be _Base.
return None # type: ignore After correcting this typo, the output is:
Which is as expected, up to the I believe this issue can be closed now. |
Oops indeed. My bad. Yes, when I replace I guess this should be closed as a duplicate, then. |
When type checking the following
example.py
:Then
mypy
produces the following errors:Expected results:
Lines 29 and 33 are accepted by
mypy
, as they should.Line 32 is properly rejected because
b
has the typeSub
and we assign aBase
to it.Unexpected results (bugs?):
Lines 35 and 36 are rejected with the message
Invalid method type
. This seems strange, as the only difference between them and the valid lines 29 and 33 is that the code is using a property instead of a method.Line 36 also triggers an additional rejection claiming that the property returns a different type
Base
than the expected typeSub
. Again this is strange sincemypy
correctly deduces the return type of the method, but gets confused and deduces a different type when using a property.Workaround:
For now, I am using the method syntax, since the property syntax is rejected by
mypy
. However, this causes ugliness in the code. For example, I need to write something likeMyClass.special_instance()[x]
, when I would much rather writeMyClass.special_instance[x]
instead.The text was updated successfully, but these errors were encountered: