Skip to content

Commit

Permalink
add support for typing.reveal_type (#12117)
Browse files Browse the repository at this point in the history
  • Loading branch information
JelleZijlstra authored Feb 3, 2022
1 parent c0e49ab commit 84b9778
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 2 deletions.
4 changes: 2 additions & 2 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
CallableType, Overloaded, Instance, Type, AnyType, LiteralType, LiteralValue,
TypeTranslator, TypeOfAny, TypeType, NoneType, PlaceholderType, TPDICT_NAMES, ProperType,
get_proper_type, get_proper_types, TypeAliasType, TypeVarLikeType,
PROTOCOL_NAMES, TYPE_ALIAS_NAMES, FINAL_TYPE_NAMES, FINAL_DECORATOR_NAMES,
PROTOCOL_NAMES, TYPE_ALIAS_NAMES, FINAL_TYPE_NAMES, FINAL_DECORATOR_NAMES, REVEAL_TYPE_NAMES,
is_named_instance,
)
from mypy.typeops import function_type, get_type_vars
Expand Down Expand Up @@ -3877,7 +3877,7 @@ def visit_call_expr(self, expr: CallExpr) -> None:
expr.analyzed.line = expr.line
expr.analyzed.column = expr.column
expr.analyzed.accept(self)
elif refers_to_fullname(expr.callee, 'builtins.reveal_type'):
elif refers_to_fullname(expr.callee, REVEAL_TYPE_NAMES):
if not self.check_fixed_args(expr, 1, 'reveal_type'):
return
expr.analyzed = RevealExpr(kind=REVEAL_TYPE, expr=expr.args[0])
Expand Down
6 changes: 6 additions & 0 deletions mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@
'typing.Reversible',
)

REVEAL_TYPE_NAMES: Final = (
'builtins.reveal_type',
'typing.reveal_type',
'typing_extensions.reveal_type',
)

# A placeholder used for Bogus[...] parameters
_dummy: Final[Any] = object()

Expand Down
16 changes: 16 additions & 0 deletions test-data/unit/check-expressions.test
Original file line number Diff line number Diff line change
Expand Up @@ -1688,6 +1688,22 @@ main:1: note: Revealed type is "Any"
def reveal_type(x: int) -> None: pass
reveal_type("foo") # E: Argument 1 to "reveal_type" has incompatible type "str"; expected "int"

[case testTypingRevealType]
from typing import reveal_type
from typing import reveal_type as show_me_the_type

reveal_type(1) # N: Revealed type is "Literal[1]?"
show_me_the_type(1) # N: Revealed type is "Literal[1]?"

[case testTypingExtensionsRevealType]
from typing_extensions import reveal_type
from typing_extensions import reveal_type as show_me_the_type

reveal_type(1) # N: Revealed type is "Literal[1]?"
show_me_the_type(1) # N: Revealed type is "Literal[1]?"

[builtins fixtures/tuple.pyi]

[case testRevealTypeVar]
reveal_type = 1
1 + "foo" # E: Unsupported operand types for + ("int" and "str")
Expand Down
2 changes: 2 additions & 0 deletions test-data/unit/lib-stub/typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,5 @@ class Sequence(Iterable[T_co]):
class Mapping(Iterable[T], Generic[T, T_co]): pass

def final(meth: T) -> T: pass

def reveal_type(__obj: T) -> T: pass
2 changes: 2 additions & 0 deletions test-data/unit/lib-stub/typing_extensions.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,5 @@ class _TypedDict(Mapping[str, object]):
def __delitem__(self, k: NoReturn) -> None: ...

def TypedDict(typename: str, fields: Dict[str, Type[_T]], *, total: Any = ...) -> Type[dict]: ...

def reveal_type(__obj: T) -> T: pass

0 comments on commit 84b9778

Please sign in to comment.