Skip to content

Commit 76cae61

Browse files
committed
Document the [maybe-unrecognized-str-typeform] error code
1 parent 1d9620d commit 76cae61

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

docs/source/error_code_list.rst

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,6 +1257,65 @@ type must be a subtype of the original type::
12571257
def g(x: object) -> TypeIs[str]: # OK
12581258
...
12591259

1260+
.. _code-maybe-unrecognized-str-typeform:
1261+
1262+
String appears in a context which expects a TypeForm [maybe-unrecognized-str-typeform]
1263+
-------------------------------------------------------------------------------------
1264+
1265+
TypeForm literals may contain string annotations:
1266+
1267+
.. code-block:: python
1268+
1269+
typx1: TypeForm = str | None
1270+
typx2: TypeForm = 'str | None' # OK
1271+
typx3: TypeForm = 'str' | None # OK
1272+
1273+
However TypeForm literals containing a string annotation can only be recognized
1274+
by mypy in the following locations:
1275+
1276+
.. code-block:: python
1277+
1278+
typx_var: TypeForm = 'str | None' # assignment r-value
1279+
1280+
def func(typx_param: TypeForm) -> TypeForm:
1281+
return 'str | None' # returned expression
1282+
1283+
func('str | None') # callable's argument
1284+
1285+
If you try to use a string annotation in some other location
1286+
which expects a TypeForm, the string value will always be treated as a ``str``
1287+
even if a ``TypeForm`` would be more appropriate and this note code
1288+
will be generated:
1289+
1290+
.. code-block:: python
1291+
1292+
# Note: TypeForm containing a string annotation cannot be recognized here. Surround with TypeForm(...) to recognize. [maybe-unrecognized-str-typeform]
1293+
# Error: List item 0 has incompatible type "str"; expected "TypeForm[Any]" [list-item]
1294+
list_of_typx: list[TypeForm] = ['str | None', float]
1295+
1296+
Fix the note by surrounding the entire type with ``TypeForm(...)``:
1297+
1298+
.. code-block:: python
1299+
1300+
list_of_typx: list[TypeForm] = [TypeForm('str | None'), float] # OK
1301+
1302+
Similarly, if you try to use a string literal in a location which expects a
1303+
TypeForm, this note code will be generated:
1304+
1305+
.. code-block:: python
1306+
1307+
dict_of_typx = {'str_or_none': TypeForm(str | None)}
1308+
# Note: TypeForm containing a string annotation cannot be recognized here. Surround with TypeForm(...) to recognize. [maybe-unrecognized-str-typeform]
1309+
list_of_typx: list[TypeForm] = [dict_of_typx['str_or_none']]
1310+
1311+
Fix the note by adding ``# type: ignore[maybe-unrecognized-str-typeform]``
1312+
to the line with the string literal:
1313+
1314+
.. code-block:: python
1315+
1316+
dict_of_typx = {'str_or_none': TypeForm(str | None)}
1317+
list_of_typx: list[TypeForm] = [dict_of_typx['str_or_none']] # type: ignore[maybe-unrecognized-str-typeform]
1318+
12601319
.. _code-misc:
12611320

12621321
Miscellaneous checks [misc]

0 commit comments

Comments
 (0)