@@ -209,9 +209,6 @@ you should know about.
209
209
210
210
Style conventions for stub files are different from PEP 8. The general
211
211
rule is that they should be as concise as possible. Specifically:
212
- * lines can be up to 130 characters long;
213
- * functions and methods that don't fit in one line should be split up
214
- with one argument per line;
215
212
* all function bodies should be empty;
216
213
* prefer `` ... `` over `` pass `` ;
217
214
* prefer `` ... `` on the same line as the class/function signature;
@@ -221,8 +218,7 @@ rule is that they should be as concise as possible. Specifically:
221
218
if the classes are very small;
222
219
* do not use docstrings;
223
220
* use variable annotations instead of type comments, even for stubs
224
- that target older versions of Python;
225
- * for arguments with a type and a default, use spaces around the ` = ` .
221
+ that target older versions of Python.
226
222
227
223
Stubs should be reformatted with the formatters
228
224
[ black] ( https://github.com/psf/black ) and
@@ -241,7 +237,13 @@ checker, and leave out unnecessary detail:
241
237
* use ` float ` instead of ` Union[int, float] ` .
242
238
243
239
Some further tips for good type hints:
244
- * avoid invariant collection types (` List ` , ` Dict ` ) in argument
240
+ * use built-in generics (` list ` , ` dict ` , ` tuple ` , ` set ` ), instead
241
+ of importing them from ` typing ` , ** except** for arbitrary length tuples
242
+ (` Tuple[int, ...] ` ) (see
243
+ [ python/mypy #9980 ] ( https://github.com/python/mypy/issues/9980 ) );
244
+ * in Python 3 stubs, import collections (` Mapping ` , ` Iterable ` , etc.)
245
+ from ` collections.abc ` instead of ` typing ` ;
246
+ * avoid invariant collection types (` list ` , ` dict ` ) in argument
245
247
positions, in favor of covariant types like ` Mapping ` or ` Sequence ` ;
246
248
* avoid Union return types: https://github.com/python/mypy/issues/1693 ;
247
249
* in Python 2, whenever possible, use ` unicode ` if that's the only
@@ -265,14 +267,6 @@ Note that `Any` is not the correct type to use if you want to indicate
265
267
that some function can accept literally anything: in those cases use
266
268
` object ` instead.
267
269
268
- For arguments with type and a default value of ` None ` , PEP 484
269
- prescribes that the type automatically becomes ` Optional ` . However we
270
- prefer explicit over implicit in this case, and require the explicit
271
- ` Optional[] ` around the type. The mypy tests enforce this (through
272
- the use of --no-implicit-optional) and the error looks like
273
- `Incompatible types in assignment (expression has type None, variable
274
- has type "Blah") `.
275
-
276
270
Stub files support forward references natively. In other words, the
277
271
order of class declarations and type aliases does not matter in
278
272
a stub file. You can also use the name of the class within its own
0 commit comments