Reputation: 433
I'm still new to type hints. Here's the minimal code example of the error I'm getting:
import sqlalchemy as sa
t = sa.Table("a", sa.MetaData(), sa.Column("id_", sa.Integer))
cols = t.columns
This raises the following error when I run mypy:
error: Expression type contains "Any" (has type "ReadOnlyColumnCollection[str, Column[Any]]") [misc]
I'm running mypy with the following configuration turned on (link):
disallow_any_expr = true
I've looked at the sql alchemy source code and the .colums
method of the Table
class does indeed have the return type that mypy states.
I don't know however how could I go about altering that to remove the Any. Would that be even the correct approach?
Upvotes: 0
Views: 1213
Reputation: 46
Are you sure you are using the latest sqlalchemy version? for me, when I run mypy to test your code, I find no issues:
f.py:5: note: Revealed type is "sqlalchemy.sql.base.ReadOnlyColumnCollection[builtins.str, sqlalchemy.sql.schema.Column[Any]]"
Success: no issues found in 1 source file
when running at:
import sqlalchemy as sa
t = sa.Table("a", sa.MetaData(), sa.Column("id_", sa.Integer))
cols = t.columns
reveal_type(cols)
Upvotes: 0
Reputation: 882336
If it's not source code that you control(a), the easiest option is usually to drop a # type: ignore [xxx]
at the end of the offending line.
I usually also place a comment stating why it's needed so that anyone looking at the code later understands since, during our PR process, we have to justify any disabling of checks for the compliance tools mypy/pylint/bandit/isort/black
.
(a) We also follow this guideline if the effort to fix our legacy code is more than a certain threshold. For example, we don't want to have to refactor 10,000 lines of code to make a one-line bug fix :-) All new code (that we control) has to comply however.
Upvotes: 1