Skip to content

Commit 1781b72

Browse files
authored
Add some info about get_proper_type() (#7950)
1 parent aba094f commit 1781b72

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

misc/proper_plugin.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ def isinstance_proper_hook(ctx: FunctionContext) -> Type:
4242
return ctx.default_return_type
4343
ctx.api.fail('Never apply isinstance() to unexpanded types;'
4444
' use mypy.types.get_proper_type() first', ctx.context)
45+
ctx.api.note('If you pass on the original type' # type: ignore[attr-defined]
46+
' after the check, always use its unexpanded version', ctx.context)
4547
return ctx.default_return_type
4648

4749

mypy/types.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1896,6 +1896,14 @@ def get_proper_type(typ: Type) -> ProperType: ...
18961896

18971897

18981898
def get_proper_type(typ: Optional[Type]) -> Optional[ProperType]:
1899+
"""Get the expansion of a type alias type.
1900+
1901+
If the type is already a proper type, this is a no-op. Use this function
1902+
wherever a decision is made on a call like e.g. 'if isinstance(typ, UnionType): ...',
1903+
because 'typ' in this case may be an alias to union. Note: if after making the decision
1904+
on the isinstance() call you pass on the original type (and not one of its components)
1905+
it is recommended to *always* pass on the unexpanded alias.
1906+
"""
18991907
if typ is None:
19001908
return None
19011909
while isinstance(typ, TypeAliasType):

0 commit comments

Comments
 (0)