🚀 Summary
As a developer using TortoiseORM with VS Code (Pylance/Pyright), I've noticed that defining models can be a bit "blind" because Field classes use **kwargs: Any in their __init__.
I would like to propose using PEP 692 (Unpack with TypedDict) to provide explicit type hints for field arguments. This would significantly improve the autocompletion and static analysis experience for all users.
😟 Current Problem
Currently, most Field classes (e.g., IntField, CharField) have an __init__ signature like this:
def __init__(self, **kwargs: Any) -> None: ...
Because of Any, IDEs cannot suggest valid arguments like pk, null, default, or index. Developers have to refer to the documentation constantly, and simple typos in argument names are not caught by type checkers (Mypy/Pyright) in "Standard" or "Strict" modes.
💡 Proposed Solution
By leveraging PEP 692, we can define a TypedDict for common field arguments. This allows the IDE to provide real-time suggestions while maintaining the flexibility of **kwargs.
Example Implementation:
from typing import TypedDict, Optional, Any
from typing_extensions import Unpack # For compatibility with older Python versions
class FieldArgs(TypedDict, total=False):
pk: bool
null: bool
default: Any
index: bool
unique: bool
db_index: bool
description: Optional[str]
source_field: Optional[str]
generated: bool
# etc.
class Field:
def __init__(self, **kwargs: Unpack[FieldArgs]) -> None:
...
🎯 Benefits
- Full Autocompletion: IDEs like VS Code (Pylance) will show the list of valid arguments as soon as the user types
(.
- Early Bug Detection: Type checkers will flag invalid arguments or incorrect types (e.g., passing a string to
null).
- Self-Documenting Code: The code itself becomes the documentation for what a
Field can accept.
🚀 Summary
As a developer using TortoiseORM with VS Code (Pylance/Pyright), I've noticed that defining models can be a bit "blind" because
Fieldclasses use**kwargs: Anyin their__init__.I would like to propose using PEP 692 (
UnpackwithTypedDict) to provide explicit type hints for field arguments. This would significantly improve the autocompletion and static analysis experience for all users.😟 Current Problem
Currently, most
Fieldclasses (e.g.,IntField,CharField) have an__init__signature like this:Because of
Any, IDEs cannot suggest valid arguments likepk,null,default, orindex. Developers have to refer to the documentation constantly, and simple typos in argument names are not caught by type checkers (Mypy/Pyright) in "Standard" or "Strict" modes.💡 Proposed Solution
By leveraging PEP 692, we can define a
TypedDictfor common field arguments. This allows the IDE to provide real-time suggestions while maintaining the flexibility of**kwargs.Example Implementation:
🎯 Benefits
(.null).Fieldcan accept.