From 23df29a323428456b889511d30393a59115f6a49 Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Thu, 14 Nov 2019 15:23:32 +0000 Subject: [PATCH] Add some info about get_proper_type() --- misc/proper_plugin.py | 2 ++ mypy/types.py | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/misc/proper_plugin.py b/misc/proper_plugin.py index 230f010e90df..c30999448387 100644 --- a/misc/proper_plugin.py +++ b/misc/proper_plugin.py @@ -42,6 +42,8 @@ def isinstance_proper_hook(ctx: FunctionContext) -> Type: return ctx.default_return_type ctx.api.fail('Never apply isinstance() to unexpanded types;' ' use mypy.types.get_proper_type() first', ctx.context) + ctx.api.note('If you pass on the original type' # type: ignore[attr-defined] + ' after the check, always use its unexpanded version', ctx.context) return ctx.default_return_type diff --git a/mypy/types.py b/mypy/types.py index 368cfe2f4b02..3eef81a4035f 100644 --- a/mypy/types.py +++ b/mypy/types.py @@ -1896,6 +1896,14 @@ def get_proper_type(typ: Type) -> ProperType: ... def get_proper_type(typ: Optional[Type]) -> Optional[ProperType]: + """Get the expansion of a type alias type. + + If the type is already a proper type, this is a no-op. Use this function + wherever a decision is made on a call like e.g. 'if isinstance(typ, UnionType): ...', + because 'typ' in this case may be an alias to union. Note: if after making the decision + on the isinstance() call you pass on the original type (and not one of its components) + it is recommended to *always* pass on the unexpanded alias. + """ if typ is None: return None while isinstance(typ, TypeAliasType):