Spurious error when overriding property with constant value in base class #2717
Replies: 10 comments
-
I assume that you've enabled type checking along with the "reportIncompatibleMethodOverride" rule. This is one of the strictest checks. It is off by default because it may require you to add more type information for it to be useful. In this case, you are relying on pylance to infer the return type of In the case of attributes, pylance's inference rules do not infer a literal type unless the attribute is constant. This is consistent with the guidance set forth in PEP 586. So this is working as intended. You should either disable "reportIncompatibleMethodOverride" if you don't feel that it's providing sufficient value, or you should add an explicit return type annotation in this case. |
Beta Was this translation helpful? Give feedback.
-
Thanks for your reply. My use case is that I'm overriding a function from a base class that is in a third-party library, I do not control the annotations of that library. I do want reportIncompatibleMethodOverride on because it's useful for me in other cases. I still think inferring the literal here is wrong. Regarding the guidance you referred to in PEP 586:
I think this is what you were referring to. This seems to support my suggestion of not inferring literal, in the interest of compatibility with valid unannotated code. Also I notice that TypeScript does not infer literal - the following gives no error:
|
Beta Was this translation helpful? Give feedback.
-
Which third-party library are you using? Is it properly typed? If not, I wouldn't recommend using the strictest diagnostic checks. These checks rely on complete and accurate types, and you're likely to see many false positives if you attempt to use them. Type inference heuristics involve many tradeoffs. We've found that in most cases, it's best to infer the narrowest type. This results in the most accurate type checking, and it eliminates a class of false positive errors. But in some cases, inferring the narrowest type can result in false positives. We've tuned the inference heuristics in pylance to try to achieve the best balance in most Python sources. If we were to change one of these heuristics at this point, it would be a breaking change — not something we'd do without evidence that it would provide significant value. |
Beta Was this translation helpful? Give feedback.
-
The library is untyped. I don't think disabling strict diagnostic checks is a good recommendation. The checks are useful to me in other parts of my code. They are only problematic when combined with wrong type inference. A better workaround for me would be to patch the library or add an ignore comment in my code.
Can you give an example of a false positive error eliminated in this case by inferring Literal? I cannot think of a use case where inferring
OK, you might agree with my points but choose not to implement them due to this difficulty. But you seem to not agree with my points anyway?
Eliminating false positives |
Beta Was this translation helpful? Give feedback.
-
You can find similar behavior in libraries like pika. For example: import pika
# Boilerplate
p_rabbit = pika.credentials.PlainCredentials(
"YOUR RABBIT LOGIN",
"YOUR RABBIT PASSWORD",
)
params = pika.ConnectionParameters(
host="YOUR RABBITMQ HOST",
port="YOUR RABBITMQ PORT",
virtual_host="YOUR RABBITMQ VIRTUAL PORT",
credentials=p_rabbit,
)
#######################
# Here is the example #
#######################
params.socket_timeout = 10
#################
# Error message #
#################
# Cannot assign member "socket_timeout" for type
# "ConnectionParameters"PylancereportGeneralTypeIssues
# (property) socket_timeout: Literal[10] I agree with @erictraut:
The library is not properly typed and it's not possible(maybe?) to suggest a single answer to the typing problem. |
Beta Was this translation helpful? Give feedback.
-
If you've enabled type checking, we also recommend disabling the |
Beta Was this translation helpful? Give feedback.
-
I totally agree that |
Beta Was this translation helpful? Give feedback.
-
@jakebailey is this closed as fixed or wontfix? |
Beta Was this translation helpful? Give feedback.
-
My impression was that this discussion had hit a terminus, where changing this behavior was undesired/potentially incompatible. I'm willing to be wrong. |
Beta Was this translation helpful? Give feedback.
-
I summarized the discussion in my last post, we haven't reached an agreement... If you want to close it as wontfix that's your choice but I personally was not convinced it is the right one. |
Beta Was this translation helpful? Give feedback.
-
Environment data
Expected behaviour
No warning when overriding property with constant value.
Actual behaviour
Error:
Code Snippet / Additional information
Discussion
This happens for method return types as well:
But does not happen for attributes. E.g.:
It feels inconsistent that
int
is inferred forBase3.foo
but not for forBase.foo
andBase2.foo
. You could say thatBase3.foo
is mutable but then it seemsBase.foo
is also "mutable" in a sense (when overridden).You might suggest to annotate
foo -> int
but in my opinion pyright could be smart enough to not warn, at least for the property, when types are inferred.Beta Was this translation helpful? Give feedback.
All reactions