Skip to content

Commit b54a711

Browse files
JelleZijlstragvanrossum
authored andcommitted
Recommend against creating incomplete stubs (#1659)
I disagree with the recommendation that users create incomplete stubs, because such stubs lead to false positive type checker errors (see for example #525). I would like to instead set a norm that all stubs should contain all public objects in the library they cover.
1 parent feac49a commit b54a711

1 file changed

Lines changed: 19 additions & 4 deletions

File tree

CONTRIBUTING.md

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,31 @@ message where you received permission**.
112112
Make sure your changes pass the tests (the [README](README.md#running-the-tests)
113113
has more information).
114114

115+
### What to include
116+
117+
Stubs should include all public objects (classes, functions, constants,
118+
etc.) in the module they cover. Omitting objects can confuse users,
119+
because users who see an error like "module X has no attribute Y" will
120+
not know whether the error appeared because their code had a bug or
121+
because the stub is wrong. If you are submitting stubs to typeshed and
122+
you are unable to provide fully typed stubs for some of the objects in
123+
the library, you can use stubgen (see below) to generate untyped stubs.
124+
Although we prefer having exact types for all stubs, such stubs are
125+
better than nothing.
126+
127+
What counts as a "public object" is not always clear. Use your judgment,
128+
but objects that are listed in the module's documentation, that are
129+
included in ``__all__`` (if present), and whose names do not start with an
130+
underscore are more likely to merit inclusion in a stub. If in doubt, err
131+
on the side of including more objects.
132+
115133
### Using stubgen
116134

117135
Mypy includes a tool called [stubgen](https://github.com/python/mypy/blob/master/mypy/stubgen.py)
118136
that you can use as a starting point for your stubs. Note that this
119137
generator is currently unable to determine most argument and return
120138
types and omits them or uses ``Any`` in their place. Fill out the types
121-
that you know. Leave out modules that you are not using at all. It's
122-
strictly better to provide partial stubs that have detailed type
123-
information than to submit unmodified ``stubgen`` output for an entire
124-
library.
139+
that you know.
125140

126141
### Stub file coding style
127142

0 commit comments

Comments
 (0)