You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
mypy not checking if calling passing a value of type Any to a function expecting some other type #16563
Mypy is doing the right thing here. This is how Any works in the Python static type system. It's literally compatible with any other type. When you use Any, you're effectively telling the type checker "don't emit any type violations here, I know what I'm doing". For more details, refer to this section in PEP 484.
That is unfortunate, because the Any comes from a sqlalchemy model. I wish we had a different kind of type similar to Any, like Unknown, to annotate when we don't know what is the type of something.
But OK, thanks for indulging my question anyway. I have to investigate how to deal with this. Maybe the nuclear option disallow_any_expr is the way to go :/
If you have a variable that is inferred asAny, but you'd like to enforce strict typechecking on that variable until the type of the variable has been manually narrowed, consider using typing.cast to convert the type of the variable to object, e.g.
importjsonimporttypingx=json.loads('{"foo": "bar"}') # oh no, inferred by type checker as having type 'Any'y=typing.cast(object, x) # y refers to the same object but has type 'object'
Activity
erictraut commentedon Nov 25, 2023
Mypy is doing the right thing here. This is how
Anyworks in the Python static type system. It's literally compatible with any other type. When you useAny, you're effectively telling the type checker "don't emit any type violations here, I know what I'm doing". For more details, refer to this section in PEP 484.gjcarneiro commentedon Nov 25, 2023
That is unfortunate, because the Any comes from a sqlalchemy model. I wish we had a different kind of type similar to Any, like Unknown, to annotate when we don't know what is the type of something.
But OK, thanks for indulging my question anyway. I have to investigate how to deal with this. Maybe the nuclear option disallow_any_expr is the way to go :/
AlexWaygood commentedon Nov 25, 2023
If you have a variable that is inferred as
Any, but you'd like to enforce strict typechecking on that variable until the type of the variable has been manually narrowed, consider usingtyping.castto convert the type of the variable toobject, e.g.