Comments
This seems fairly ad hoc to me. If something is annotated as |
Obviously, this makes no difference to parameters of third-party functions, I don't see any improvements there. It just seems to me that these checks for Maybe function arguments should be excluded from this check, and still give an error if |
I just don't see any purpose to giving an error about an I don't have a lot of code that's checked with disallow Any, so I've only got this one example. I can't remember for sure, but I think each of the 6
The code is basically runtime checking type annotations against API responses, which is used to help reverse engineer the API (and alert me to changes). |
Going through my previous bug reports, I believe this would also be fixed by treating Any as object: The Any is within an await statement, so requires a messy cast to work around it. If it were object, the cast could come after the await without triggering an error. |
I think many libraries (and typeshed) also use
Causes: Due to the typeshed definition: This seems to be pretty common, where Basically, when I enable the |
If you think typeshed should change its usages of Any to object, please file a PR (or several) against that repo. If the Any warnings in mypy are not useful to you, don't use them. I don't think it's reasonable to expect mypy to be able to tell the difference between unsafe and safe uses of Any. I propose to close this issue. |
It's still useful, but it creates extra warnings that are simply unnecessary and require workarounds. I'm just not seeing a reason these extra warnings would be useful to developers. If there is a reason, then a new option that allows this strict typing without the extra warnings would be still be good. If strict typing (i.e. warn about |
I think van Rossum's argument is that, if this usage of So e.g. this type signature maybe should be considered buggy: def create_task(
coro: Union[Generator[Any, None, _T],
Awaitable[_T]],
*,
name: Optional[str] = None)
-> Task[_T]: and should be fixed by changing it to: def create_task(
coro: Union[Generator[object, None, _T],
Awaitable[_T]],
*,
name: Optional[str] = None)
-> Task[_T]: That said, I think maybe the intended meaning of |
I have in fact fixed that particular example already: However, there are still many cases where it doesn't make sense to change this, particularly with return types. See the 6 We can't assume a return type of |
Another example, this time from a more typical use case:
https://.com/aio-libs/aiohttp/blob/master/examples/web_ws.py#L30-L34 Gives:
That's 4 errors (ignoring the one from |
I may have said this before, but if this bothers you, you should not use the flags that cause warnings about "Any", or set them up so that they only apply to your own code (where you can write 'object' instead of 'Any' to make them go away). We're not going to change the meaning of Any. The typeshed people may or may not be interested in changing the stubs to contain fewer occurrences of Any. |
As I said before, the stricter options are useful, I just don't see why it needs to raise errors when they are irrelevant and have no effect on type safety. 90% of the additional errors are good and help eliminate undesired dynamic typing, but the other 10% just seem to be pointless noise. Also, my previous comment is not meant as a complaint, but a response to @JukkaL, who requested more examples from real code. I don't use |
In this case, I do have access to change the code, but using |
--disallow-any-expr
, silence complaints about Any where object would work I am haunted by the same problem. On one hand, In my own code, I am trying to educate myself to use |
It might be useful to mention it in the docs, but for third-party libraries, you'll likely still want The focus of this issue is that there are quite a few places where the error is triggered due to an |
I think the most obvious place where the docs can help improve this, would be in relation to |
Another case this would resolve is the regression in #17171. |
This implements a suggestion in python#9153 (comment), which I thought was a good idea.
Problem
Using
Any
is not type safe, therefore Mypy provides options to disallowAny
. However, there are many uses ofAny
from third-party and stdlib, so these options tend to result in a lot of errors even when they are not relevant.I'm finding many situations where I get an error about
Any
, but the code would be deemed safe if it wasobject
.Examples
This produces errors about
Any
on both lines even though the value is not used:This produces errors as well, but
object
would not produce errors, as Mypy can infer everything necessary here to be type safe.Solution
Add an option, or change the behaviour of the current disallow
Any
options, such thatAny
is treated likeobject
. Then, rather than producing errors whenAny
is present, just show normal errors withAny
(as if it wasobject
) in them (e.g. 'Any has no attribute foo').The text was updated successfully, but these errors were encountered: