Skip to content

Use double quotes in "Revealed type is" error message #10232

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 3 commits into from
Mar 21, 2021
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 docs/source/cheat_sheet.rst
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ When you're puzzled or when things are complicated
# To find out what type mypy infers for an expression anywhere in
# your program, wrap it in reveal_type(). Mypy will print an error
# message with the type; remove it again before running the code.
reveal_type(1) # -> Revealed type is 'builtins.int'
reveal_type(1) # -> Revealed type is "builtins.int"

# Use Union when something could be one of a few types
x = [3, 5, "test", "fun"] # type: List[Union[int, str]]
Expand Down Expand Up @@ -160,7 +160,7 @@ When you're puzzled or when things are complicated
a = [4]
b = cast(List[int], a) # Passes fine
c = cast(List[str], a) # Passes fine (no runtime check)
reveal_type(c) # -> Revealed type is 'builtins.list[builtins.str]'
reveal_type(c) # -> Revealed type is "builtins.list[builtins.str]"
print c # -> [4]; the object is not cast

# If you want dynamic attributes on your class, have it override "__setattr__"
Expand Down
4 changes: 2 additions & 2 deletions docs/source/cheat_sheet_py3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ When you're puzzled or when things are complicated
# To find out what type mypy infers for an expression anywhere in
# your program, wrap it in reveal_type(). Mypy will print an error
# message with the type; remove it again before running the code.
reveal_type(1) # -> Revealed type is 'builtins.int'
reveal_type(1) # -> Revealed type is "builtins.int"

# Use Union when something could be one of a few types
x: List[Union[int, str]] = [3, 5, "test", "fun"]
Expand Down Expand Up @@ -178,7 +178,7 @@ When you're puzzled or when things are complicated
a = [4]
b = cast(List[int], a) # Passes fine
c = cast(List[str], a) # Passes fine (no runtime check)
reveal_type(c) # -> Revealed type is 'builtins.list[builtins.str]'
reveal_type(c) # -> Revealed type is "builtins.list[builtins.str]"
print(c) # -> [4]; the object is not cast

# If you want dynamic attributes on your class, have it override "__setattr__"
Expand Down
2 changes: 1 addition & 1 deletion docs/source/common_issues.rst
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ understand how mypy handles a particular piece of code. Example:

.. code-block:: python

reveal_type((1, 'hello')) # Revealed type is 'Tuple[builtins.int, builtins.str]'
reveal_type((1, 'hello')) # Revealed type is "Tuple[builtins.int, builtins.str]"

You can also use ``reveal_locals()`` at any line in a file
to see the types of all local variables at once. Example:
Expand Down
32 changes: 16 additions & 16 deletions docs/source/literal_types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ precise type signature for this function using ``Literal[...]`` and overloads:
# Implementation is omitted
...

reveal_type(fetch_data(True)) # Revealed type is 'bytes'
reveal_type(fetch_data(False)) # Revealed type is 'str'
reveal_type(fetch_data(True)) # Revealed type is "bytes"
reveal_type(fetch_data(False)) # Revealed type is "str"

# Variables declared without annotations will continue to have an
# inferred type of 'bool'.

variable = True
reveal_type(fetch_data(variable)) # Revealed type is 'Union[bytes, str]'
reveal_type(fetch_data(variable)) # Revealed type is "Union[bytes, str]"

.. note::

Expand Down Expand Up @@ -96,15 +96,15 @@ a literal type:
.. code-block:: python

a: Literal[19] = 19
reveal_type(a) # Revealed type is 'Literal[19]'
reveal_type(a) # Revealed type is "Literal[19]"

In order to preserve backwards-compatibility, variables without this annotation
are **not** assumed to be literals:

.. code-block:: python

b = 19
reveal_type(b) # Revealed type is 'int'
reveal_type(b) # Revealed type is "int"

If you find repeating the value of the variable in the type hint to be tedious,
you can instead change the variable to be ``Final`` (see :ref:`final_attrs`):
Expand All @@ -117,7 +117,7 @@ you can instead change the variable to be ``Final`` (see :ref:`final_attrs`):

c: Final = 19

reveal_type(c) # Revealed type is 'Literal[19]?'
reveal_type(c) # Revealed type is "Literal[19]?"
expects_literal(c) # ...and this type checks!

If you do not provide an explicit type in the ``Final``, the type of ``c`` becomes
Expand Down Expand Up @@ -155,13 +155,13 @@ For example, compare and contrast what happens when you try appending these type
# Mypy will chose to infer List[int] here.
list_of_ints = []
list_of_ints.append(a)
reveal_type(list_of_ints) # Revealed type is 'List[int]'
reveal_type(list_of_ints) # Revealed type is "List[int]"

# But if the variable you're appending is an explicit Literal, mypy
# will infer List[Literal[19]].
list_of_lits = []
list_of_lits.append(b)
reveal_type(list_of_lits) # Revealed type is 'List[Literal[19]]'
reveal_type(list_of_lits) # Revealed type is "List[Literal[19]]"


Intelligent indexing
Expand All @@ -182,19 +182,19 @@ corresponding to some particular index, we can use Literal types like so:
tup = ("foo", 3.4)

# Indexing with an int literal gives us the exact type for that index
reveal_type(tup[0]) # Revealed type is 'str'
reveal_type(tup[0]) # Revealed type is "str"

# But what if we want the index to be a variable? Normally mypy won't
# know exactly what the index is and so will return a less precise type:
int_index = 1
reveal_type(tup[int_index]) # Revealed type is 'Union[str, float]'
reveal_type(tup[int_index]) # Revealed type is "Union[str, float]"

# But if we use either Literal types or a Final int, we can gain back
# the precision we originally had:
lit_index: Literal[1] = 1
fin_index: Final = 1
reveal_type(tup[lit_index]) # Revealed type is 'str'
reveal_type(tup[fin_index]) # Revealed type is 'str'
reveal_type(tup[lit_index]) # Revealed type is "str"
reveal_type(tup[fin_index]) # Revealed type is "str"

# We can do the same thing with with TypedDict and str keys:
class MyDict(TypedDict):
Expand All @@ -204,11 +204,11 @@ corresponding to some particular index, we can use Literal types like so:

d: MyDict = {"name": "Saanvi", "main_id": 111, "backup_id": 222}
name_key: Final = "name"
reveal_type(d[name_key]) # Revealed type is 'str'
reveal_type(d[name_key]) # Revealed type is "str"

# You can also index using unions of literals
id_key: Literal["main_id", "backup_id"]
reveal_type(d[id_key]) # Revealed type is 'int'
reveal_type(d[id_key]) # Revealed type is "int"

.. _tagged_unions:

Expand Down Expand Up @@ -282,9 +282,9 @@ using ``isinstance()``:
# However, we can side-step this by checking the type of `w.inner` to
# narrow `w` itself:
if isinstance(w.inner, int):
reveal_type(w) # Revealed type is 'Wrapper[int]'
reveal_type(w) # Revealed type is "Wrapper[int]"
else:
reveal_type(w) # Revealed type is 'Wrapper[str]'
reveal_type(w) # Revealed type is "Wrapper[str]"

This feature is sometimes called "sum types" or "discriminated union types"
in other programming languages.
Expand Down
4 changes: 2 additions & 2 deletions docs/source/more_types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1019,8 +1019,8 @@ Keys that aren't required are shown with a ``?`` in error messages:

.. code-block:: python

# Revealed type is 'TypedDict('GuiOptions', {'language'?: builtins.str,
# 'color'?: builtins.str})'
# Revealed type is "TypedDict('GuiOptions', {'language'?: builtins.str,
# 'color'?: builtins.str})"
reveal_type(options)

Totality also affects structural compatibility. You can't use a partial
Expand Down
2 changes: 1 addition & 1 deletion mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -1065,7 +1065,7 @@ def invalid_signature_for_special_method(
self.fail('Invalid signature "{}" for "{}"'.format(func_type, method_name), context)

def reveal_type(self, typ: Type, context: Context) -> None:
self.note('Revealed type is \'{}\''.format(typ), context)
self.note('Revealed type is "{}"'.format(typ), context)

def reveal_locals(self, type_map: Dict[str, Optional[Type]], context: Context) -> None:
# To ensure that the output is predictable on Python < 3.6,
Expand Down
2 changes: 1 addition & 1 deletion scripts/find_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def run_mypy(mypy_and_args: List[str], filename: str, tmp_name: str) -> str:
return proc.stdout.decode(encoding="utf-8")

def get_revealed_type(line: str, relevant_file: str, relevant_line: int) -> Optional[str]:
m = re.match(r"(.+?):(\d+): note: Revealed type is '(.*)'$", line)
m = re.match(r'(.+?):(\d+): note: Revealed type is "(.*)"$', line)
if (m and
int(m.group(2)) == relevant_line and
os.path.samefile(relevant_file, m.group(1))):
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-abstract.test
Original file line number Diff line number Diff line change
Expand Up @@ -1000,8 +1000,8 @@ my_abstract_types = {
'B': MyAbstractB,
}

reveal_type(my_concrete_types) # N: Revealed type is 'builtins.dict[builtins.str*, def () -> __main__.MyAbstractType]'
reveal_type(my_abstract_types) # N: Revealed type is 'builtins.dict[builtins.str*, def () -> __main__.MyAbstractType]'
reveal_type(my_concrete_types) # N: Revealed type is "builtins.dict[builtins.str*, def () -> __main__.MyAbstractType]"
reveal_type(my_abstract_types) # N: Revealed type is "builtins.dict[builtins.str*, def () -> __main__.MyAbstractType]"

a = my_concrete_types['A']()
a.do()
Expand Down
38 changes: 19 additions & 19 deletions test-data/unit/check-annotated.test
Original file line number Diff line number Diff line change
@@ -1,93 +1,93 @@
[case testAnnotated0]
from typing_extensions import Annotated
x: Annotated[int, ...]
reveal_type(x) # N: Revealed type is 'builtins.int'
reveal_type(x) # N: Revealed type is "builtins.int"
[builtins fixtures/tuple.pyi]

[case testAnnotated1]
from typing import Union
from typing_extensions import Annotated
x: Annotated[Union[int, str], ...]
reveal_type(x) # N: Revealed type is 'Union[builtins.int, builtins.str]'
reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]"
[builtins fixtures/tuple.pyi]

[case testAnnotated2]
from typing_extensions import Annotated
x: Annotated[int, THESE, ARE, IGNORED, FOR, NOW]
reveal_type(x) # N: Revealed type is 'builtins.int'
reveal_type(x) # N: Revealed type is "builtins.int"
[builtins fixtures/tuple.pyi]

[case testAnnotated3]
from typing_extensions import Annotated
x: Annotated[int, -+~12.3, "som"[e], more(anno+a+ions, that=[are]), (b"ignored",), 4, N.O.W, ...]
reveal_type(x) # N: Revealed type is 'builtins.int'
reveal_type(x) # N: Revealed type is "builtins.int"
[builtins fixtures/tuple.pyi]

[case testAnnotatedBadType]
from typing_extensions import Annotated
x: Annotated[XXX, ...] # E: Name 'XXX' is not defined
reveal_type(x) # N: Revealed type is 'Any'
reveal_type(x) # N: Revealed type is "Any"
[builtins fixtures/tuple.pyi]

[case testAnnotatedBadNoArgs]
from typing_extensions import Annotated
x: Annotated # E: Annotated[...] must have exactly one type argument and at least one annotation
reveal_type(x) # N: Revealed type is 'Any'
reveal_type(x) # N: Revealed type is "Any"
[builtins fixtures/tuple.pyi]

[case testAnnotatedBadOneArg]
from typing_extensions import Annotated
x: Annotated[int] # E: Annotated[...] must have exactly one type argument and at least one annotation
reveal_type(x) # N: Revealed type is 'Any'
reveal_type(x) # N: Revealed type is "Any"
[builtins fixtures/tuple.pyi]

[case testAnnotatedNested0]
from typing_extensions import Annotated
x: Annotated[Annotated[int, ...], ...]
reveal_type(x) # N: Revealed type is 'builtins.int'
reveal_type(x) # N: Revealed type is "builtins.int"
[builtins fixtures/tuple.pyi]

[case testAnnotatedNested1]
from typing import Union
from typing_extensions import Annotated
x: Annotated[Annotated[Union[int, str], ...], ...]
reveal_type(x) # N: Revealed type is 'Union[builtins.int, builtins.str]'
reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]"
[builtins fixtures/tuple.pyi]

[case testAnnotatedNestedBadType]
from typing_extensions import Annotated
x: Annotated[Annotated[XXX, ...], ...] # E: Name 'XXX' is not defined
reveal_type(x) # N: Revealed type is 'Any'
reveal_type(x) # N: Revealed type is "Any"
[builtins fixtures/tuple.pyi]

[case testAnnotatedNestedBadNoArgs]
from typing_extensions import Annotated
x: Annotated[Annotated, ...] # E: Annotated[...] must have exactly one type argument and at least one annotation
reveal_type(x) # N: Revealed type is 'Any'
reveal_type(x) # N: Revealed type is "Any"
[builtins fixtures/tuple.pyi]

[case testAnnotatedNestedBadOneArg]
from typing_extensions import Annotated
x: Annotated[Annotated[int], ...] # E: Annotated[...] must have exactly one type argument and at least one annotation
reveal_type(x) # N: Revealed type is 'Any'
reveal_type(x) # N: Revealed type is "Any"
[builtins fixtures/tuple.pyi]

[case testAnnotatedNoImport]
x: Annotated[int, ...] # E: Name 'Annotated' is not defined
reveal_type(x) # N: Revealed type is 'Any'
reveal_type(x) # N: Revealed type is "Any"

[case testAnnotatedDifferentName]
from typing_extensions import Annotated as An
x: An[int, ...]
reveal_type(x) # N: Revealed type is 'builtins.int'
reveal_type(x) # N: Revealed type is "builtins.int"
[builtins fixtures/tuple.pyi]

[case testAnnotatedAliasSimple]
from typing import Tuple
from typing_extensions import Annotated
Alias = Annotated[Tuple[int, ...], ...]
x: Alias
reveal_type(x) # N: Revealed type is 'builtins.tuple[builtins.int]'
reveal_type(x) # N: Revealed type is "builtins.tuple[builtins.int]"
[builtins fixtures/tuple.pyi]

[case testAnnotatedAliasTypeVar]
Expand All @@ -96,7 +96,7 @@ from typing_extensions import Annotated
T = TypeVar('T')
Alias = Annotated[T, ...]
x: Alias[int]
reveal_type(x) # N: Revealed type is 'builtins.int'
reveal_type(x) # N: Revealed type is "builtins.int"
[builtins fixtures/tuple.pyi]

[case testAnnotatedAliasGenericTuple]
Expand All @@ -105,7 +105,7 @@ from typing_extensions import Annotated
T = TypeVar('T')
Alias = Annotated[Tuple[T, T], ...]
x: Alias[int]
reveal_type(x) # N: Revealed type is 'Tuple[builtins.int, builtins.int]'
reveal_type(x) # N: Revealed type is "Tuple[builtins.int, builtins.int]"
[builtins fixtures/tuple.pyi]

[case testAnnotatedAliasGenericUnion]
Expand All @@ -114,7 +114,7 @@ from typing_extensions import Annotated
T = TypeVar('T')
Alias = Annotated[Union[T, str], ...]
x: Alias[int]
reveal_type(x) # N: Revealed type is 'Union[builtins.int, builtins.str]'
reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]"
[builtins fixtures/tuple.pyi]

[case testAnnotatedSecondParamNonType]
Expand All @@ -124,5 +124,5 @@ class Meta:
...

x = Annotated[int, Meta()]
reveal_type(x) # N: Revealed type is 'def () -> builtins.int'
reveal_type(x) # N: Revealed type is "def () -> builtins.int"
[builtins fixtures/tuple.pyi]
Loading