Skip to content

add support for typing.reveal_type #12117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optional nit: Let's also try rt = reveal_type and rt(1)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That actually doesn't work, probably because at the place in semanal where we recognize reveal_type we can't see through assignments yet. Fixing it would probably be a lot more complicated than this PR, so if we think it's worth fixing, we should do it separately. It doesn't seem too important as no user has asked for it so far.


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