Inconsistent behavior: MyPy rejects isinstance with Any, but accepts cast from Any for ignored imports
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 20.6k
- 派生
- 3.3k
- 平均合并
- 1 天 18 小时
- 30 天内合并 PR
- 54
描述
Context
When working with the cassandra.cluster module in a Python project, i had to use type: ignore on the import because there are no compatible MyPy stubs available for the module.
Below is a simplified version of how the code is set up.
from cassandra.cluster import Session as CassandraConnection # type: ignore (mypy will treat it just like Any)
from typing import TypeAlias, cast, Any
TransactionalConnection: TypeAlias = CassandraConnection
This approach generally works, but it causes issues with type checking in MyPy.
Initial Problem
When trying to use isinstance() to check if a variable is an instance of TransactionalConnection, Mypy raises the following error:
Cannot use isinstance() with Any type [misc]
This happens because MyPy is treating CassandraConnection as Any due to the #type: ignore. Here's the code that triggers the error:
if isinstance(db_client, TransactionalConnection):
pass
-> error occurs here: Cannot use isinstance() with Any type.
Paradox
Interestingly, when using cast(Any, CassandraConnection), Mypy accepts the typing without issues. In other words, when i cast Any to CassandraConnection, Mypy does not raise any errors, even though the underlying type is still Any. Here's the example:
TransactionalConnection: TypeAlias = cast(Any, CassandraConnection) -> This works with no MyPy errors.
This behavior is paradoxical because, in both cases: (isinstance and cast), the underlying type is Any, but Mypy handles them differently.
Full Scenario Example
# Core
import json
import os
from typing import TypedDict, TypeAlias, cast, Any
# Libraries
from psycopg2.extensions import connection as PostgreSQLConnection
import psycopg2
from psycopg2 import sql as psycopg2_sql
from cassandra.cluster import Session as CassandraConnection # type: ignore
from cassandra.auth import PlainTextAuthProvider # type: ignore
from cassandra.cluster import Cluster
# Core functionality
sql = psycopg2_sql
SqlCursor = psycopg2.extensions.cursor
# Defining type aliases
RelationalConnection: TypeAlias = PostgreSQLConnection
TransactionalConnection: TypeAlias = cast(Any, CassandraConnection)
class DbConnections(TypedDict):
transactional: TransactionalConnection
relational: RelationalConnection
Here’s the part where the isinstance check causes the error:
if isinstance(db_client, TransactionalConnection):
pass
error: Cannot use isinstance() with Any type
But when using cast, MyPy doesn't raise any errors:
TransactionalConnection: TypeAlias = cast(Any, CassandraConnection)
Conclusion
The inconsistent behavior between using isinstance() and cast when dealing with the Any type seems to be a bug or, at the very least, an inconsistency in Mypy. If cast() accepts Any without issue, isinstance() should behave similarly, as both deal with the Any type. However, Mypy rejects isinstance, but accepts cast.
This inconsistent behavior can confuse developers working with modules that lack proper typing, requiring the use of # type: ignore. I hope this issue can be reviewed, and Mypy can offer consistent behavior between isinstance and cast.
Environment
- Python version 3.12.3
- Flags:
--strict - mypy version: 1.11.0
- mypy-extensions version: 1.0.0
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先使用 mypy 1.11.0 和 --strict 重现最小的 Python 3.12 示例,然后追踪 isinstance() 和 cast() 如何处理来自被忽略 import 的 Any 类型。确认预期的一致性,并添加一个涵盖所报告行为的回归测试。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- devtools
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100