MCPcopy Index your code
hub / github.com/python/mypy / isinstance_proper_hook

Function isinstance_proper_hook

mypy/plugins/proper_plugin.py:55–76  ·  view source on GitHub ↗
(ctx: FunctionContext)

Source from the content-addressed store, hash-verified

53
54
55def isinstance_proper_hook(ctx: FunctionContext) -> Type:
56 if len(ctx.arg_types) != 2 or not ctx.arg_types[1]:
57 return ctx.default_return_type
58
59 right = get_proper_type(ctx.arg_types[1][0])
60 for arg in ctx.arg_types[0]:
61 if (
62 is_improper_type(arg) or isinstance(get_proper_type(arg), AnyType)
63 ) and is_dangerous_target(right):
64 if is_special_target(right):
65 return ctx.default_return_type
66 ctx.api.fail(
67 "Never apply isinstance() to unexpanded types;"
68 " use mypy.types.get_proper_type() first",
69 ctx.context,
70 )
71 ctx.api.note( # type: ignore[attr-defined]
72 "If you pass on the original type"
73 " after the check, always use its unexpanded version",
74 ctx.context,
75 )
76 return ctx.default_return_type
77
78
79def is_special_target(right: ProperType) -> bool:

Callers

nothing calls this directly

Calls 8

get_proper_typeFunction · 0.90
lenFunction · 0.85
is_improper_typeFunction · 0.85
isinstanceFunction · 0.85
is_dangerous_targetFunction · 0.85
is_special_targetFunction · 0.85
failMethod · 0.45
noteMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…