Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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))
Expand Down
21 changes: 21 additions & 0 deletions test-data/unit/check-newtype.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down