Skip to content

Reject isinstance() with TypedDict and NewType #3654

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 4, 2017
Merged
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
11 changes: 8 additions & 3 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,16 @@ def visit_call_expr(self, e: CallExpr, allow_none_return: bool = False) -> Type:
except KeyError:
# Undefined names should already be reported in semantic analysis.
node = None
if (isinstance(typ, IndexExpr)
and isinstance(typ.analyzed, (TypeApplication, TypeAliasExpr))
if ((isinstance(typ, IndexExpr)
and isinstance(typ.analyzed, (TypeApplication, TypeAliasExpr)))
# node.kind == TYPE_ALIAS only for aliases like It = Iterable[int].
or isinstance(typ, NameExpr) and node and node.kind == nodes.TYPE_ALIAS):
or (isinstance(typ, NameExpr) and node and node.kind == nodes.TYPE_ALIAS)):
self.msg.type_arguments_not_allowed(e)
if isinstance(typ, RefExpr) and isinstance(typ.node, TypeInfo):
if typ.node.typeddict_type:
self.msg.fail(messages.CANNOT_ISINSTANCE_TYPEDDICT, e)
elif typ.node.is_newtype:
self.msg.fail(messages.CANNOT_ISINSTANCE_NEWTYPE, e)
self.try_infer_partial_type(e)
callee_type = self.accept(e.callee, always_allow_any=True)
if (self.chk.options.disallow_untyped_calls and
Expand Down
2 changes: 2 additions & 0 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@
NON_BOOLEAN_IN_CONDITIONAL = 'Condition must be a boolean'
DUPLICATE_TYPE_SIGNATURES = 'Function has duplicate type signatures'
GENERIC_INSTANCE_VAR_CLASS_ACCESS = 'Access to generic instance variables via class is ambiguous'
CANNOT_ISINSTANCE_TYPEDDICT = 'Cannot use isinstance() with a TypedDict type'
CANNOT_ISINSTANCE_NEWTYPE = 'Cannot use isinstance() with a NewType type'

ARG_CONSTRUCTOR_NAMES = {
ARG_POS: "Arg",
Expand Down
6 changes: 3 additions & 3 deletions mypy/test/testextensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ def test_typeddict_errors(self):
self.assertEqual(TypedDict.__module__, 'mypy_extensions')
jim = Emp(name='Jim', id=1)
with self.assertRaises(TypeError):
isinstance({}, Emp)
isinstance({}, Emp) # type: ignore
with self.assertRaises(TypeError):
isinstance(jim, Emp)
isinstance(jim, Emp) # type: ignore
with self.assertRaises(TypeError):
issubclass(dict, Emp)
issubclass(dict, Emp) # type: ignore
with self.assertRaises(TypeError):
TypedDict('Hi', x=1)
with self.assertRaises(TypeError):
Expand Down
8 changes: 8 additions & 0 deletions test-data/unit/check-newtype.test
Original file line number Diff line number Diff line change
Expand Up @@ -336,3 +336,11 @@ class C(B): pass # E: Cannot subclass NewType
from typing import NewType
Any = NewType('Any', int)
Any(5)

[case testNewTypeAndIsInstance]
from typing import NewType
T = NewType('T', int)
d: object
if isinstance(d, T): # E: Cannot use isinstance() with a NewType type
reveal_type(d) # E: Revealed type is '__main__.T'
[builtins fixtures/isinstancelist.pyi]
10 changes: 8 additions & 2 deletions test-data/unit/check-typeddict.test
Original file line number Diff line number Diff line change
Expand Up @@ -717,8 +717,14 @@ def set_coordinate(p: TaggedPoint, key: str, value: int) -> None:

-- isinstance

-- TODO: Implement support for this case.
--[case testCannotIsInstanceTypedDictType]
[case testTypedDictAndInstance]
from mypy_extensions import TypedDict
D = TypedDict('D', {'x': int})
d: object
if isinstance(d, D): # E: Cannot use isinstance() with a TypedDict type
reveal_type(d) # E: Revealed type is '__main__.D'
[builtins fixtures/isinstancelist.pyi]


-- scoping
[case testTypedDictInClassNamespace]
Expand Down
1 change: 1 addition & 0 deletions test-data/unit/fixtures/isinstancelist.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class type:

class tuple: pass
class function: pass
class ellipsis: pass
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary? It looks like you don't use ellipses in the added tests.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The stub for mypy_extensions uses ....


def isinstance(x: object, t: Union[type, Tuple]) -> bool: pass
def issubclass(x: object, t: Union[type, Tuple]) -> bool: pass
Expand Down