diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index c48f3da51f85..368a95c39238 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,8 +571,14 @@ 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 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. + 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 if isinstance(e.callee, LambdaExpr): @@ -773,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 95042d1393ab..625458a4295f 100644 --- a/test-data/unit/check-newtype.test +++ b/test-data/unit/check-newtype.test @@ -365,10 +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]