|
255 | 255 | to optimize performance and memory usage. |
256 | 256 | - [ ] Recheck formatting with Ruff. |
257 | 257 |
|
| 258 | +## Python type completeness |
| 259 | + |
| 260 | +The following are best practice recommendations for how to |
| 261 | +define “type complete”: |
| 262 | + |
| 263 | +- [ ] Classes: |
| 264 | + - [ ] All class variables, instance variables, and methods that |
| 265 | + are “visible” (not overridden) are annotated and refer to |
| 266 | + known types |
| 267 | + - [ ] If a class is a subclass of a generic class, type arguments |
| 268 | + are provided for each generic type parameter, and these type |
| 269 | + arguments are known types |
| 270 | +- [ ] Functions and Methods: |
| 271 | + - [ ] All input parameters have type annotations that refer to |
| 272 | + known types |
| 273 | + - [ ] The return parameter is annotated and refers to a known type |
| 274 | + - [ ] The result of applying one or more decorators results in |
| 275 | + a known type |
| 276 | +- [ ] Type Aliases: |
| 277 | + - [ ] All of the types referenced by the type alias are known |
| 278 | +- [ ] Variables: |
| 279 | + - [ ] All variables have type annotations that refer to known types |
| 280 | + |
| 281 | +Type annotations can be omitted in a few specific cases |
| 282 | +where the type is obvious from the context: |
| 283 | + |
| 284 | +- Constants that are assigned simple literal values |
| 285 | + (e.g. `RED = '#F00'` or `MAX_TIMEOUT = 50` or |
| 286 | + `room_temperature: Final = 20`). |
| 287 | + A constant is a symbol that is assigned only once and is either |
| 288 | + annotated with `Final` or is named in all-caps. |
| 289 | + A constant that is not assigned a simple literal value requires |
| 290 | + explicit annotations, preferably with a Final annotation |
| 291 | + (e.g. `WOODWINDS: Final[list[str]] = ['Oboe', 'Bassoon']`). |
| 292 | +- Enum values within an `Enum` class do not require annotations |
| 293 | + because they take on the type of the `Enum` class. |
| 294 | +- Type aliases do not require annotations. |
| 295 | + A type alias is a symbol that is defined at a module level |
| 296 | + with a single assignment where the assigned value is an |
| 297 | + instantiable type, as opposed to a class instance |
| 298 | + (e.g. `Foo = Callable[[Literal["a", "b"]], int | str]` or |
| 299 | + `Bar = MyGenericClass[int] | None`). |
| 300 | +- The “self” parameter in an instance method and the “cls” |
| 301 | + parameter in a class method do not require an explicit annotation. |
| 302 | +- The return type for an `__init__` method does not need |
| 303 | + to be specified, since it is always `None`. |
| 304 | +- The following module-level symbols do not require type annotations: |
| 305 | + `__all__`, `__author__`, `__copyright__`, `__email__`, |
| 306 | + `__license__`, `__title__`, `__uri__`, `__version__`. |
| 307 | +- The following class-level symbols do not require type annotations: |
| 308 | + `__class__`, `__dict__`, `__doc__`, `__module__`, `__slots__`. |
| 309 | + |
258 | 310 | ## JSON |
259 | 311 |
|
260 | 312 | - [ ] When serialize to JSON, always enclose decimal values |
|
0 commit comments