Skip to content

Commit 764f926

Browse files
Use double quotes in "Revealed type is" error message (#10232)
Part of #7445 Co-authored-by: [email protected] <[email protected]>
1 parent 6ad3ea1 commit 764f926

File tree

74 files changed

+4395
-4395
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+4395
-4395
lines changed

docs/source/cheat_sheet.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ When you're puzzled or when things are complicated
129129
# To find out what type mypy infers for an expression anywhere in
130130
# your program, wrap it in reveal_type(). Mypy will print an error
131131
# message with the type; remove it again before running the code.
132-
reveal_type(1) # -> Revealed type is 'builtins.int'
132+
reveal_type(1) # -> Revealed type is "builtins.int"
133133
134134
# Use Union when something could be one of a few types
135135
x = [3, 5, "test", "fun"] # type: List[Union[int, str]]
@@ -160,7 +160,7 @@ When you're puzzled or when things are complicated
160160
a = [4]
161161
b = cast(List[int], a) # Passes fine
162162
c = cast(List[str], a) # Passes fine (no runtime check)
163-
reveal_type(c) # -> Revealed type is 'builtins.list[builtins.str]'
163+
reveal_type(c) # -> Revealed type is "builtins.list[builtins.str]"
164164
print c # -> [4]; the object is not cast
165165
166166
# If you want dynamic attributes on your class, have it override "__setattr__"

docs/source/cheat_sheet_py3.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ When you're puzzled or when things are complicated
148148
# To find out what type mypy infers for an expression anywhere in
149149
# your program, wrap it in reveal_type(). Mypy will print an error
150150
# message with the type; remove it again before running the code.
151-
reveal_type(1) # -> Revealed type is 'builtins.int'
151+
reveal_type(1) # -> Revealed type is "builtins.int"
152152
153153
# Use Union when something could be one of a few types
154154
x: List[Union[int, str]] = [3, 5, "test", "fun"]
@@ -178,7 +178,7 @@ When you're puzzled or when things are complicated
178178
a = [4]
179179
b = cast(List[int], a) # Passes fine
180180
c = cast(List[str], a) # Passes fine (no runtime check)
181-
reveal_type(c) # -> Revealed type is 'builtins.list[builtins.str]'
181+
reveal_type(c) # -> Revealed type is "builtins.list[builtins.str]"
182182
print(c) # -> [4]; the object is not cast
183183
184184
# If you want dynamic attributes on your class, have it override "__setattr__"

docs/source/common_issues.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ understand how mypy handles a particular piece of code. Example:
493493

494494
.. code-block:: python
495495
496-
reveal_type((1, 'hello')) # Revealed type is 'Tuple[builtins.int, builtins.str]'
496+
reveal_type((1, 'hello')) # Revealed type is "Tuple[builtins.int, builtins.str]"
497497
498498
You can also use ``reveal_locals()`` at any line in a file
499499
to see the types of all local variables at once. Example:

docs/source/literal_types.rst

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ precise type signature for this function using ``Literal[...]`` and overloads:
3636
# Implementation is omitted
3737
...
3838
39-
reveal_type(fetch_data(True)) # Revealed type is 'bytes'
40-
reveal_type(fetch_data(False)) # Revealed type is 'str'
39+
reveal_type(fetch_data(True)) # Revealed type is "bytes"
40+
reveal_type(fetch_data(False)) # Revealed type is "str"
4141
4242
# Variables declared without annotations will continue to have an
4343
# inferred type of 'bool'.
4444
4545
variable = True
46-
reveal_type(fetch_data(variable)) # Revealed type is 'Union[bytes, str]'
46+
reveal_type(fetch_data(variable)) # Revealed type is "Union[bytes, str]"
4747
4848
.. note::
4949

@@ -96,15 +96,15 @@ a literal type:
9696
.. code-block:: python
9797
9898
a: Literal[19] = 19
99-
reveal_type(a) # Revealed type is 'Literal[19]'
99+
reveal_type(a) # Revealed type is "Literal[19]"
100100
101101
In order to preserve backwards-compatibility, variables without this annotation
102102
are **not** assumed to be literals:
103103

104104
.. code-block:: python
105105
106106
b = 19
107-
reveal_type(b) # Revealed type is 'int'
107+
reveal_type(b) # Revealed type is "int"
108108
109109
If you find repeating the value of the variable in the type hint to be tedious,
110110
you can instead change the variable to be ``Final`` (see :ref:`final_attrs`):
@@ -117,7 +117,7 @@ you can instead change the variable to be ``Final`` (see :ref:`final_attrs`):
117117
118118
c: Final = 19
119119
120-
reveal_type(c) # Revealed type is 'Literal[19]?'
120+
reveal_type(c) # Revealed type is "Literal[19]?"
121121
expects_literal(c) # ...and this type checks!
122122
123123
If you do not provide an explicit type in the ``Final``, the type of ``c`` becomes
@@ -155,13 +155,13 @@ For example, compare and contrast what happens when you try appending these type
155155
# Mypy will chose to infer List[int] here.
156156
list_of_ints = []
157157
list_of_ints.append(a)
158-
reveal_type(list_of_ints) # Revealed type is 'List[int]'
158+
reveal_type(list_of_ints) # Revealed type is "List[int]"
159159
160160
# But if the variable you're appending is an explicit Literal, mypy
161161
# will infer List[Literal[19]].
162162
list_of_lits = []
163163
list_of_lits.append(b)
164-
reveal_type(list_of_lits) # Revealed type is 'List[Literal[19]]'
164+
reveal_type(list_of_lits) # Revealed type is "List[Literal[19]]"
165165
166166
167167
Intelligent indexing
@@ -182,19 +182,19 @@ corresponding to some particular index, we can use Literal types like so:
182182
tup = ("foo", 3.4)
183183
184184
# Indexing with an int literal gives us the exact type for that index
185-
reveal_type(tup[0]) # Revealed type is 'str'
185+
reveal_type(tup[0]) # Revealed type is "str"
186186
187187
# But what if we want the index to be a variable? Normally mypy won't
188188
# know exactly what the index is and so will return a less precise type:
189189
int_index = 1
190-
reveal_type(tup[int_index]) # Revealed type is 'Union[str, float]'
190+
reveal_type(tup[int_index]) # Revealed type is "Union[str, float]"
191191
192192
# But if we use either Literal types or a Final int, we can gain back
193193
# the precision we originally had:
194194
lit_index: Literal[1] = 1
195195
fin_index: Final = 1
196-
reveal_type(tup[lit_index]) # Revealed type is 'str'
197-
reveal_type(tup[fin_index]) # Revealed type is 'str'
196+
reveal_type(tup[lit_index]) # Revealed type is "str"
197+
reveal_type(tup[fin_index]) # Revealed type is "str"
198198
199199
# We can do the same thing with with TypedDict and str keys:
200200
class MyDict(TypedDict):
@@ -204,11 +204,11 @@ corresponding to some particular index, we can use Literal types like so:
204204
205205
d: MyDict = {"name": "Saanvi", "main_id": 111, "backup_id": 222}
206206
name_key: Final = "name"
207-
reveal_type(d[name_key]) # Revealed type is 'str'
207+
reveal_type(d[name_key]) # Revealed type is "str"
208208
209209
# You can also index using unions of literals
210210
id_key: Literal["main_id", "backup_id"]
211-
reveal_type(d[id_key]) # Revealed type is 'int'
211+
reveal_type(d[id_key]) # Revealed type is "int"
212212
213213
.. _tagged_unions:
214214

@@ -282,9 +282,9 @@ using ``isinstance()``:
282282
# However, we can side-step this by checking the type of `w.inner` to
283283
# narrow `w` itself:
284284
if isinstance(w.inner, int):
285-
reveal_type(w) # Revealed type is 'Wrapper[int]'
285+
reveal_type(w) # Revealed type is "Wrapper[int]"
286286
else:
287-
reveal_type(w) # Revealed type is 'Wrapper[str]'
287+
reveal_type(w) # Revealed type is "Wrapper[str]"
288288
289289
This feature is sometimes called "sum types" or "discriminated union types"
290290
in other programming languages.

docs/source/more_types.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,8 +1019,8 @@ Keys that aren't required are shown with a ``?`` in error messages:
10191019

10201020
.. code-block:: python
10211021
1022-
# Revealed type is 'TypedDict('GuiOptions', {'language'?: builtins.str,
1023-
# 'color'?: builtins.str})'
1022+
# Revealed type is "TypedDict('GuiOptions', {'language'?: builtins.str,
1023+
# 'color'?: builtins.str})"
10241024
reveal_type(options)
10251025
10261026
Totality also affects structural compatibility. You can't use a partial

mypy/messages.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,7 @@ def invalid_signature_for_special_method(
10651065
self.fail('Invalid signature "{}" for "{}"'.format(func_type, method_name), context)
10661066

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

10701070
def reveal_locals(self, type_map: Dict[str, Optional[Type]], context: Context) -> None:
10711071
# To ensure that the output is predictable on Python < 3.6,

scripts/find_type.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def run_mypy(mypy_and_args: List[str], filename: str, tmp_name: str) -> str:
4242
return proc.stdout.decode(encoding="utf-8")
4343

4444
def get_revealed_type(line: str, relevant_file: str, relevant_line: int) -> Optional[str]:
45-
m = re.match(r"(.+?):(\d+): note: Revealed type is '(.*)'$", line)
45+
m = re.match(r'(.+?):(\d+): note: Revealed type is "(.*)"$', line)
4646
if (m and
4747
int(m.group(2)) == relevant_line and
4848
os.path.samefile(relevant_file, m.group(1))):

test-data/unit/check-abstract.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,8 +1000,8 @@ my_abstract_types = {
10001000
'B': MyAbstractB,
10011001
}
10021002

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

10061006
a = my_concrete_types['A']()
10071007
a.do()

test-data/unit/check-annotated.test

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,93 @@
11
[case testAnnotated0]
22
from typing_extensions import Annotated
33
x: Annotated[int, ...]
4-
reveal_type(x) # N: Revealed type is 'builtins.int'
4+
reveal_type(x) # N: Revealed type is "builtins.int"
55
[builtins fixtures/tuple.pyi]
66

77
[case testAnnotated1]
88
from typing import Union
99
from typing_extensions import Annotated
1010
x: Annotated[Union[int, str], ...]
11-
reveal_type(x) # N: Revealed type is 'Union[builtins.int, builtins.str]'
11+
reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]"
1212
[builtins fixtures/tuple.pyi]
1313

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

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

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

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

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

4444
[case testAnnotatedNested0]
4545
from typing_extensions import Annotated
4646
x: Annotated[Annotated[int, ...], ...]
47-
reveal_type(x) # N: Revealed type is 'builtins.int'
47+
reveal_type(x) # N: Revealed type is "builtins.int"
4848
[builtins fixtures/tuple.pyi]
4949

5050
[case testAnnotatedNested1]
5151
from typing import Union
5252
from typing_extensions import Annotated
5353
x: Annotated[Annotated[Union[int, str], ...], ...]
54-
reveal_type(x) # N: Revealed type is 'Union[builtins.int, builtins.str]'
54+
reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]"
5555
[builtins fixtures/tuple.pyi]
5656

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

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

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

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

7979
[case testAnnotatedDifferentName]
8080
from typing_extensions import Annotated as An
8181
x: An[int, ...]
82-
reveal_type(x) # N: Revealed type is 'builtins.int'
82+
reveal_type(x) # N: Revealed type is "builtins.int"
8383
[builtins fixtures/tuple.pyi]
8484

8585
[case testAnnotatedAliasSimple]
8686
from typing import Tuple
8787
from typing_extensions import Annotated
8888
Alias = Annotated[Tuple[int, ...], ...]
8989
x: Alias
90-
reveal_type(x) # N: Revealed type is 'builtins.tuple[builtins.int]'
90+
reveal_type(x) # N: Revealed type is "builtins.tuple[builtins.int]"
9191
[builtins fixtures/tuple.pyi]
9292

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

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

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

120120
[case testAnnotatedSecondParamNonType]
@@ -124,5 +124,5 @@ class Meta:
124124
...
125125

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

0 commit comments

Comments
 (0)