From 221a076f787e5092c99d59617f273a909ac9927e Mon Sep 17 00:00:00 2001 From: StrikeRougeAgent <359208080+StrikeRougeAgent@users.noreply.github.com> Date: Tue, 24 Nov 2026 19:13:33 +0000 Subject: [PATCH 1/2] Report NewType used as the first argument of issubclass() issubclass() raises TypeError when the class under test is a NewType, but mypy only diagnosed a NewType in the second argument. The argument check does not catch it because a NewType's constructor is accepted where type is expected, so the call type-checked cleanly. Check the first argument of issubclass() for a NewType alongside the existing second-argument checks, and extend the existing isinstance()/ issubclass() NewType test case. Fixes #22504 --- mypy/checkexpr.py | 11 +++++++++++ test-data/unit/check-newtype.test | 2 ++ 2 files changed, 13 insertions(+) diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index c48f3da51f85e..7fe1d1ad260dd 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -573,6 +573,17 @@ 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": + # issubclass() tests the class given as its first argument, so a NewType + # there fails at runtime just as it does in second position. The subtype + # check lets it through, as a NewType's constructor looks like a type object. + cls_arg = e.args[0] + if ( + isinstance(cls_arg, RefExpr) + and isinstance(cls_arg.node, TypeInfo) + and cls_arg.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..45d98af33849d 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(int, object) [builtins fixtures/isinstancelist.pyi] [case testInvalidNewTypeCrash] From 334961a1412f97e385bc83bf4298fbae43585d2a Mon Sep 17 00:00:00 2001 From: StrikeRougeAgent <359208080+StrikeRougeAgent@users.noreply.github.com> Date: Wed, 25 Nov 2026 19:27:40 +0000 Subject: [PATCH 2/2] fix: resolve the callee and NewType aliases in isinstance checks Key the isinstance()/issubclass() argument checks on the resolved callee (builtins.isinstance / builtins.issubclass) instead of the spelled name, so a user-defined function that shadows either builtin no longer triggers the NewType/TypedDict diagnostics. Detect NewType through type aliases as well: a plain `Alias = T` is a TypeAlias whose target is an Instance of the NewType, which the old RefExpr/TypeInfo test missed in both argument positions. PEP 695 aliases are left to the regular argument check, which already reports them. Extend testNewTypeWithIsInstanceAndIsSubclass with alias cases and add testIsInstanceAndIsSubclassShadowed for the shadowing regression. --- mypy/checkexpr.py | 30 ++++++++++++++++++++---------- test-data/unit/check-newtype.test | 19 +++++++++++++++++++ 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index 7fe1d1ad260dd..368a95c392387 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -536,7 +536,7 @@ def visit_call_expr_inner(self, e: CallExpr, allow_none_return: bool = False) -> ) if ( isinstance(e.callee, NameExpr) - and e.callee.name in ("isinstance", "issubclass") + and e.callee.fullname in ("builtins.isinstance", "builtins.issubclass") and len(e.args) == 2 ): for typ in mypy.checker.flatten(e.args[1]): @@ -571,18 +571,13 @@ def visit_call_expr_inner(self, e: CallExpr, allow_none_return: bool = False) -> if isinstance(typ, RefExpr) and isinstance(typ.node, TypeInfo): if typ.node.typeddict_type: 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": + if self.is_newtype_type_expr(typ): + self.msg.cannot_use_function_with_type(e.callee.name, "NewType", e) + if e.callee.fullname == "builtins.issubclass": # issubclass() tests the class given as its first argument, so a NewType # there fails at runtime just as it does in second position. The subtype # check lets it through, as a NewType's constructor looks like a type object. - cls_arg = e.args[0] - if ( - isinstance(cls_arg, RefExpr) - and isinstance(cls_arg.node, TypeInfo) - and cls_arg.node.is_newtype - ): + if self.is_newtype_type_expr(e.args[0]): self.msg.cannot_use_function_with_type("issubclass", "NewType", e) self.try_infer_partial_type(e) type_context = None @@ -784,6 +779,21 @@ def check_runtime_protocol_test(self, e: CallExpr) -> None: ): self.chk.fail(message_registry.RUNTIME_PROTOCOL_EXPECTED, e) + def is_newtype_type_expr(self, expr: Expression) -> bool: + """Does this expression evaluate to a NewType, directly or through an alias?""" + if isinstance(expr, RefExpr) and isinstance(expr.node, TypeInfo) and expr.node.is_newtype: + return True + if isinstance(expr, NameExpr): + try: + node = self.chk.lookup_qualified(expr.name).node + except KeyError: + return False + # PEP 695 aliases are already rejected by the regular argument check. + if isinstance(node, TypeAlias) and not node.python_3_12_type_alias: + target = get_proper_type(node.target) + return isinstance(target, Instance) and target.type.is_newtype + return False + def check_protocol_issubclass(self, e: CallExpr) -> None: for expr in mypy.checker.flatten(e.args[1]): tp = get_proper_type(self.chk.lookup_type(expr)) diff --git a/test-data/unit/check-newtype.test b/test-data/unit/check-newtype.test index 45d98af33849d..625458a4295f8 100644 --- a/test-data/unit/check-newtype.test +++ b/test-data/unit/check-newtype.test @@ -365,12 +365,31 @@ Any(5) [case testNewTypeWithIsInstanceAndIsSubclass] from typing import NewType T = NewType('T', int) +Alias = T d: object if isinstance(d, T): # E: Cannot use isinstance() with NewType type reveal_type(d) # N: Revealed type is "builtins.int" +isinstance(d, Alias) # E: Cannot use isinstance() with NewType type issubclass(object, T) # E: Cannot use issubclass() with NewType type issubclass(T, object) # E: Cannot use issubclass() with NewType type issubclass(int, object) +issubclass(Alias, object) # E: Cannot use issubclass() with NewType type +issubclass(object, Alias) # E: Cannot use issubclass() with NewType type +[builtins fixtures/isinstancelist.pyi] + +[case testIsInstanceAndIsSubclassShadowed] +from typing import NewType + +T = NewType('T', int) + +def isinstance(obj: object, typ: object) -> bool: + return True + +def issubclass(cls: object, typ: object) -> bool: + return True + +isinstance(T, int) +issubclass(T, int) [builtins fixtures/isinstancelist.pyi] [case testInvalidNewTypeCrash]