From 7c0b58f3946995ebec3ae7b1e5a13d0851df63d5 Mon Sep 17 00:00:00 2001 From: zhengdaodeguL <1159957339@qq.com> Date: Tue, 17 Nov 2026 14:01:31 +0000 Subject: [PATCH] Report NewType used as the first argument of issubclass() issubclass() with a NewType as its first argument fails at runtime (TypeError: issubclass() arg 1 must be a class), but mypy only reported a NewType used as the second argument. Check the first argument the same way, including NewTypes inside a tuple. Fixes python/mypy#22504 --- mypy/checkexpr.py | 5 +++++ test-data/unit/check-newtype.test | 2 ++ 2 files changed, 7 insertions(+) diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index c48f3da51f85e..6492a5b717f09 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -573,6 +573,11 @@ def visit_call_expr_inner(self, e: CallExpr, allow_none_return: bool = False) -> self.msg.cannot_use_function_with_type(e.callee.name, "TypedDict", e) elif typ.node.is_newtype: self.msg.cannot_use_function_with_type(e.callee.name, "NewType", e) + if e.callee.name == "issubclass": + for typ in mypy.checker.flatten(e.args[0]): + if isinstance(typ, RefExpr) and isinstance(typ.node, TypeInfo): + if typ.node.is_newtype: + self.msg.cannot_use_function_with_type("issubclass", "NewType", e) self.try_infer_partial_type(e) type_context = None if isinstance(e.callee, LambdaExpr): diff --git a/test-data/unit/check-newtype.test b/test-data/unit/check-newtype.test index 95042d1393ab1..aa57440ffa1e7 100644 --- a/test-data/unit/check-newtype.test +++ b/test-data/unit/check-newtype.test @@ -369,6 +369,8 @@ d: object if isinstance(d, T): # E: Cannot use isinstance() with NewType type reveal_type(d) # N: Revealed type is "builtins.int" issubclass(object, T) # E: Cannot use issubclass() with NewType type +issubclass(T, object) # E: Cannot use issubclass() with NewType type +issubclass((T,), object) # E: Cannot use issubclass() with NewType type [builtins fixtures/isinstancelist.pyi] [case testInvalidNewTypeCrash]