|
| 1 | +# Stub packages |
| 2 | + |
| 3 | +Stub packages are packages named `<package>-stubs` that provide typing stubs for `<package>`. See |
| 4 | +[specification](https://typing.python.org/en/latest/spec/distributing.html#stub-only-packages). |
| 5 | + |
| 6 | +## Simple stub |
| 7 | + |
| 8 | +```toml |
| 9 | +[environment] |
| 10 | +extra-paths = ["/packages"] |
| 11 | +``` |
| 12 | + |
| 13 | +`/packages/foo-stubs/__init__.pyi`: |
| 14 | + |
| 15 | +```pyi |
| 16 | +class Foo: |
| 17 | + name: str |
| 18 | + age: int |
| 19 | +``` |
| 20 | + |
| 21 | +`/packages/foo/__init__.py`: |
| 22 | + |
| 23 | +```py |
| 24 | +class Foo: ... |
| 25 | +``` |
| 26 | + |
| 27 | +`main.py`: |
| 28 | + |
| 29 | +```py |
| 30 | +from foo import Foo |
| 31 | + |
| 32 | +reveal_type(Foo().name) # revealed: str |
| 33 | +``` |
| 34 | + |
| 35 | +## Stubs only |
| 36 | + |
| 37 | +The regular package isn't required for type checking. |
| 38 | + |
| 39 | +```toml |
| 40 | +[environment] |
| 41 | +extra-paths = ["/packages"] |
| 42 | +``` |
| 43 | + |
| 44 | +`/packages/foo-stubs/__init__.pyi`: |
| 45 | + |
| 46 | +```pyi |
| 47 | +class Foo: |
| 48 | + name: str |
| 49 | + age: int |
| 50 | +``` |
| 51 | + |
| 52 | +`main.py`: |
| 53 | + |
| 54 | +```py |
| 55 | +from foo import Foo |
| 56 | + |
| 57 | +reveal_type(Foo().name) # revealed: str |
| 58 | +``` |
| 59 | + |
| 60 | +## Namespace package in different search paths |
| 61 | + |
| 62 | +A namespace package with multiple stub packages spread over multiple search paths. |
| 63 | + |
| 64 | +```toml |
| 65 | +[environment] |
| 66 | +extra-paths = ["/stubs1", "/stubs2", "/packages"] |
| 67 | +``` |
| 68 | + |
| 69 | +`/stubs1/shapes-stubs/polygons/pentagon.pyi`: |
| 70 | + |
| 71 | +```pyi |
| 72 | +class Pentagon: |
| 73 | + sides: int |
| 74 | + area: float |
| 75 | +``` |
| 76 | + |
| 77 | +`/stubs2/shapes-stubs/polygons/hexagon.pyi`: |
| 78 | + |
| 79 | +```pyi |
| 80 | +class Hexagon: |
| 81 | + sides: int |
| 82 | + area: float |
| 83 | +``` |
| 84 | + |
| 85 | +`/packages/shapes/polygons/pentagon.py`: |
| 86 | + |
| 87 | +```py |
| 88 | +class Pentagon: ... |
| 89 | +``` |
| 90 | + |
| 91 | +`/packages/shapes/polygons/hexagon.py`: |
| 92 | + |
| 93 | +```py |
| 94 | +class Hexagon: ... |
| 95 | +``` |
| 96 | + |
| 97 | +`main.py`: |
| 98 | + |
| 99 | +```py |
| 100 | +from shapes.polygons.hexagon import Hexagon |
| 101 | +from shapes.polygons.pentagon import Pentagon |
| 102 | + |
| 103 | +reveal_type(Pentagon().sides) # revealed: int |
| 104 | +reveal_type(Hexagon().area) # revealed: int | float |
| 105 | +``` |
| 106 | + |
| 107 | +## Inconsistent stub packages |
| 108 | + |
| 109 | +Stub packages where one is a namespae package and the other is a regular package. Module resolution |
| 110 | +should stop after the first non-namespace stub package. This matches pyrights behavior. |
| 111 | + |
| 112 | +```toml |
| 113 | +[environment] |
| 114 | +extra-paths = ["/stubs1", "/stubs2", "/packages"] |
| 115 | +``` |
| 116 | + |
| 117 | +`/stubs1/shapes-stubs/__init__.pyi`: |
| 118 | + |
| 119 | +```pyi |
| 120 | +``` |
| 121 | + |
| 122 | +`/stubs1/shapes-stubs/polygons/__init__.pyi`: |
| 123 | + |
| 124 | +```pyi |
| 125 | +``` |
| 126 | + |
| 127 | +`/stubs1/shapes-stubs/polygons/pentagon.pyi`: |
| 128 | + |
| 129 | +```pyi |
| 130 | +class Pentagon: |
| 131 | + sides: int |
| 132 | + area: float |
| 133 | +``` |
| 134 | + |
| 135 | +`/stubs2/shapes-stubs/polygons/hexagon.pyi`: |
| 136 | + |
| 137 | +```pyi |
| 138 | +class Hexagon: |
| 139 | + sides: int |
| 140 | + area: float |
| 141 | +``` |
| 142 | + |
| 143 | +`/packages/shapes/polygons/pentagon.py`: |
| 144 | + |
| 145 | +```py |
| 146 | +class Pentagon: ... |
| 147 | +``` |
| 148 | + |
| 149 | +`/packages/shapes/polygons/hexagon.py`: |
| 150 | + |
| 151 | +```py |
| 152 | +class Hexagon: ... |
| 153 | +``` |
| 154 | + |
| 155 | +`main.py`: |
| 156 | + |
| 157 | +```py |
| 158 | +from shapes.polygons.pentagon import Pentagon |
| 159 | +from shapes.polygons.hexagon import Hexagon # error: [unresolved-import] |
| 160 | + |
| 161 | +reveal_type(Pentagon().sides) # revealed: int |
| 162 | +reveal_type(Hexagon().area) # revealed: Unknown |
| 163 | +``` |
| 164 | + |
| 165 | +## Namespace stubs for non-namespace package |
| 166 | + |
| 167 | +The runtime package is a regular package but the stubs are namespace packages. Pyright |
| 168 | +skips the stub package if the "regular" package isn't a namespace package. I'm not aware |
| 169 | +that the behavior here is specificed, and using the stubs without probing the runtime package |
| 170 | +first requires slightly fewer lookups. |
| 171 | + |
| 172 | +```toml |
| 173 | +[environment] |
| 174 | +extra-paths = ["/packages"] |
| 175 | +``` |
| 176 | + |
| 177 | +`/packages/shapes-stubs/polygons/pentagon.pyi`: |
| 178 | + |
| 179 | +```pyi |
| 180 | +class Pentagon: |
| 181 | + sides: int |
| 182 | + area: float |
| 183 | +``` |
| 184 | + |
| 185 | +`/packages/shapes-stubs/polygons/hexagon.pyi`: |
| 186 | + |
| 187 | +```pyi |
| 188 | +class Hexagon: |
| 189 | + sides: int |
| 190 | + area: float |
| 191 | +``` |
| 192 | + |
| 193 | +`/packages/shapes/__init__.py`: |
| 194 | + |
| 195 | +```py |
| 196 | +``` |
| 197 | + |
| 198 | +`/packages/shapes/polygons/__init__.py`: |
| 199 | + |
| 200 | +```py |
| 201 | +``` |
| 202 | + |
| 203 | +`/packages/shapes/polygons/pentagon.py`: |
| 204 | + |
| 205 | +```py |
| 206 | +class Pentagon: ... |
| 207 | +``` |
| 208 | + |
| 209 | +`/packages/shapes/polygons/hexagon.py`: |
| 210 | + |
| 211 | +```py |
| 212 | +class Hexagon: ... |
| 213 | +``` |
| 214 | + |
| 215 | +`main.py`: |
| 216 | + |
| 217 | +```py |
| 218 | +from shapes.polygons.pentagon import Pentagon |
| 219 | +from shapes.polygons.hexagon import Hexagon |
| 220 | + |
| 221 | +reveal_type(Pentagon().sides) # revealed: int |
| 222 | +reveal_type(Hexagon().area) # revealed: int | float |
| 223 | +``` |
| 224 | + |
| 225 | +## Stub package using `__init__.py` over `.pyi` |
| 226 | + |
| 227 | +It's recommended that stub packages use `__init__.pyi` files over `__init__.py` but it doesn't seem to be an enforced convention. At least, Pyright is fine with the following. |
| 228 | + |
| 229 | +```toml |
| 230 | +[environment] |
| 231 | +extra-paths = ["/packages"] |
| 232 | +``` |
| 233 | + |
| 234 | +`/packages/shapes-stubs/__init__.py`: |
| 235 | + |
| 236 | +```py |
| 237 | +class Pentagon: |
| 238 | + sides: int |
| 239 | + area: float |
| 240 | + |
| 241 | +class Hexagon: |
| 242 | + sides: int |
| 243 | + area: float |
| 244 | +``` |
| 245 | + |
| 246 | +`/packages/shapes/__init__.py`: |
| 247 | + |
| 248 | +```py |
| 249 | +class Pentagon: ... |
| 250 | +class Hexagon: ... |
| 251 | +``` |
| 252 | + |
| 253 | +`main.py`: |
| 254 | + |
| 255 | +```py |
| 256 | +from shapes import Hexagon, Pentagon |
| 257 | + |
| 258 | +reveal_type(Pentagon().sides) # revealed: int |
| 259 | +reveal_type(Hexagon().area) # revealed: int | float |
| 260 | +``` |
0 commit comments