@@ -215,6 +215,28 @@ You can use :py:class:`~collections.abc.Callable` as the type for callable objec
215215 for x in objs:
216216 f(x)
217217
218+ .. _code-nonetype-type :
219+
220+ Check that NoneType is not used as a type (annotation) [nonetype-type]
221+ ----------------------------------------------------------------------
222+
223+ The preferred way to annotate the type of `None ` is `None `.
224+ `NoneType ` is equivalent, but mypy won't allow it by default.
225+
226+ .. code-block :: python
227+
228+ from types import NoneType
229+ def f (x : None ) -> None :
230+ reveal_type(x) # note: Revealed type is "None"
231+
232+ # error: NoneType should not be used as a type, please use None instead [nonetype-type]
233+ def g (x : NoneType) -> None :
234+ reveal_type(x) # note: Revealed type is "None"
235+
236+ # error: NoneType should not be used as a type, please use None instead [nonetype-type]
237+ x1: NoneType = None
238+ x2: None = None # OK
239+
218240 .. _code-metaclass :
219241
220242Check the validity of a class's metaclass [metaclass]
@@ -1129,6 +1151,25 @@ Warn about cases where a bytes object may be converted to a string in an unexpec
11291151 print (f " The alphabet starts with { b!r } " ) # The alphabet starts with b'abc'
11301152 print (f " The alphabet starts with { b.decode(' utf-8' )} " ) # The alphabet starts with abc
11311153
1154+ .. _code-str-unpack :
1155+
1156+ Check that ``str `` is not unpacked [str-unpack]
1157+ ---------------------------------------------------------
1158+
1159+ It can sometimes be surprising that ``str `` is iterable, especially when unpacking
1160+ in an assignment.
1161+
1162+ Example:
1163+
1164+ .. code-block :: python
1165+
1166+ def print_dict (d : dict[str , str ]) -> int :
1167+ # We meant to do d.items(), but instead we're unpacking the str keys of d
1168+
1169+ # Error: Unpacking a string is disallowed
1170+ for k, v in d:
1171+ print (k, v)
1172+
11321173 .. _code-overload-overlap :
11331174
11341175Check that overloaded functions don't overlap [overload-overlap]
0 commit comments