Closed stefanhoelzl closed 6 years ago
Why do you think the warning is false? It looks like a totally reasonable warning, since --warn-return-any
is part of --strict
. If you don't like it, don't use this flag.
because {} == other
should evaluate to a bool
.
Which means the method returns a bool
.
Then getting a warning Returning Any
seems false to me.
Everything applied to Any
evaluates to Any
. A dynamic class can define __eq__
that returns a string, there is no guarantee it is a bool
.
Everything applied to
Any
evaluates toAny
. A dynamic class can define__eq__
that returns a string, there is no guarantee it is abool
.
This was helpful. Thank you!
I get the same error when using:
def verify_password(plain_password :str, hashed_password :str) -> bool:
return pwd_context.verify(plain_password, hashed_password)
Which is taken from the FastAPI docs. In this case .verify()
is a verification function that to the best of my knowledge can only return True
or False
but is not typed: https://foss.heptapod.net/python-libs/passlib/-/blob/01dfe753e4f4590307611ef5c6304279a7364305/passlib/context.py?page=3#L2272-2313
I think I understand the rules of the Any
evaluation, but perhaps I've misunderstood how mypy evaluates these cases.
Shouldn't the return value matter in mypy's checks? That is that it's a static value of False
or True
that gets returned?
In any case, the fix is simple enough:
def verify_password(plain_password :str, hashed_password :str) -> bool:
return bool(pwd_context.verify(plain_password, hashed_password))
But it feels excessive to enforce one additional function call per call stack to please mypy, even in --strict
mode.
It's the first time trying to use linters and type checkers, and this thread came up. I assumed it would deal with these cases "smartly", is that not the case?
@Torxed the fix seems simple here: mypy needs to be told what the return type of .verify()
is. If there's no annotation, it doesn't know.
I would not recommend turning on the option that generates this error. It's too strict to be useful for most code.
--no-warn-return-any
:-)
same issue here 👋
My little hack for this is to set a new variable, and type the variable. (Similar to @Torxed, but not explicitly casting)
from typing import Any
class Structure():
def __eq__(self, other: Any) -> bool:
outcome: bool = {} == other
return outcome
@Torxed the fix seems simple here: mypy needs to be told what the return type of
.verify()
is. If there's no annotation, it doesn't know.
The same problem affects me. It is evident that .verify()
returns a boolean. Why is it invisible to Mypy?
@Torxed the fix seems simple here: mypy needs to be told what the return type of
.verify()
is. If there's no annotation, it doesn't know.The same problem affects me. It is evident that
.verify()
returns a boolean. Why is it invisible to Mypy?
It's actually not completely evident. Because the .verify()
function actually lacks a typed annotation that mypy can interpret.
Your IDE however, might be able to parse the sphinx docstring since the verify docstring has a :returns:
or, if exist, :rtype:
.
But to the best of my understanding, passlib simply lacks native annotation in all possible .verify()
functions.
And to the best of my understanding, mypy
does not read or understand docstrings, and why would it, as it's a sphinx invented thing (as I understand it).
Would be nice if the project moved to https://pypi.org/project/sphinx-autodoc-typehints/ or something similar.
@Torxed Thank you for your thoughtful explanations, as I had not given this topic much thought.
class Structure(): def eq(self, other: Any) -> bool: return {} == other
mypy==0.630
--strict