Skip to content

Commit 84b9778

Browse files
add support for typing.reveal_type (#12117)
1 parent c0e49ab commit 84b9778

File tree

5 files changed

+28
-2
lines changed

5 files changed

+28
-2
lines changed

mypy/semanal.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
CallableType, Overloaded, Instance, Type, AnyType, LiteralType, LiteralValue,
9999
TypeTranslator, TypeOfAny, TypeType, NoneType, PlaceholderType, TPDICT_NAMES, ProperType,
100100
get_proper_type, get_proper_types, TypeAliasType, TypeVarLikeType,
101-
PROTOCOL_NAMES, TYPE_ALIAS_NAMES, FINAL_TYPE_NAMES, FINAL_DECORATOR_NAMES,
101+
PROTOCOL_NAMES, TYPE_ALIAS_NAMES, FINAL_TYPE_NAMES, FINAL_DECORATOR_NAMES, REVEAL_TYPE_NAMES,
102102
is_named_instance,
103103
)
104104
from mypy.typeops import function_type, get_type_vars
@@ -3877,7 +3877,7 @@ def visit_call_expr(self, expr: CallExpr) -> None:
38773877
expr.analyzed.line = expr.line
38783878
expr.analyzed.column = expr.column
38793879
expr.analyzed.accept(self)
3880-
elif refers_to_fullname(expr.callee, 'builtins.reveal_type'):
3880+
elif refers_to_fullname(expr.callee, REVEAL_TYPE_NAMES):
38813881
if not self.check_fixed_args(expr, 1, 'reveal_type'):
38823882
return
38833883
expr.analyzed = RevealExpr(kind=REVEAL_TYPE, expr=expr.args[0])

mypy/types.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@
126126
'typing.Reversible',
127127
)
128128

129+
REVEAL_TYPE_NAMES: Final = (
130+
'builtins.reveal_type',
131+
'typing.reveal_type',
132+
'typing_extensions.reveal_type',
133+
)
134+
129135
# A placeholder used for Bogus[...] parameters
130136
_dummy: Final[Any] = object()
131137

test-data/unit/check-expressions.test

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1688,6 +1688,22 @@ main:1: note: Revealed type is "Any"
16881688
def reveal_type(x: int) -> None: pass
16891689
reveal_type("foo") # E: Argument 1 to "reveal_type" has incompatible type "str"; expected "int"
16901690

1691+
[case testTypingRevealType]
1692+
from typing import reveal_type
1693+
from typing import reveal_type as show_me_the_type
1694+
1695+
reveal_type(1) # N: Revealed type is "Literal[1]?"
1696+
show_me_the_type(1) # N: Revealed type is "Literal[1]?"
1697+
1698+
[case testTypingExtensionsRevealType]
1699+
from typing_extensions import reveal_type
1700+
from typing_extensions import reveal_type as show_me_the_type
1701+
1702+
reveal_type(1) # N: Revealed type is "Literal[1]?"
1703+
show_me_the_type(1) # N: Revealed type is "Literal[1]?"
1704+
1705+
[builtins fixtures/tuple.pyi]
1706+
16911707
[case testRevealTypeVar]
16921708
reveal_type = 1
16931709
1 + "foo" # E: Unsupported operand types for + ("int" and "str")

test-data/unit/lib-stub/typing.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,5 @@ class Sequence(Iterable[T_co]):
4747
class Mapping(Iterable[T], Generic[T, T_co]): pass
4848

4949
def final(meth: T) -> T: pass
50+
51+
def reveal_type(__obj: T) -> T: pass

test-data/unit/lib-stub/typing_extensions.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,5 @@ class _TypedDict(Mapping[str, object]):
4444
def __delitem__(self, k: NoReturn) -> None: ...
4545

4646
def TypedDict(typename: str, fields: Dict[str, Type[_T]], *, total: Any = ...) -> Type[dict]: ...
47+
48+
def reveal_type(__obj: T) -> T: pass

0 commit comments

Comments
 (0)