-
-
Notifications
You must be signed in to change notification settings - Fork 3k
TypingDict Default Arguments #12980
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Could you give some examples of what you want to work? (Also, mutable defaults seem like an unrelated issue, so let's discuss that elsewhere.) |
Of course. Assume I am tasked with type hinting a 3rd-party library that wraps a function using @staticmethod
def keyClick(*args, **kwargs):
qt_api.QtTest.QTest.keyClick(*args, **kwargs) Here The actual signatures of void keyClick(QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1)
void keyClick(QWidget *widget, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1)
void keyClick(QWindow *window, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1)
void keyClick(QWindow *window, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1) As you can see, two arguments here have default values. I think currently default arguments might be possible using the # python 3.8.10
from __future__ import annotations
from typing import Annotated, TypedDict
from qt_api import QtCore, QtGui, QtWidgets
from typing_extensions import Unpack
class KeyClickArgs(TypedDict):
widget: QtWidgets.QWidget | QtGui.QWindow
key: QtCore.Qt.Key | str
modifier: Annotated[QtCore.Qt.KeyboardModifiers, QtCore.Qt.NoModifier]
delay: Annotated[int, -1] |
DefaultArg = Annotated
Actually, I can't use a variable unless it's a type alias, so I would have to specify the indices to |
See #12981 |
From
and the
It would be great if ```python
class DefaultArgs(TypedDict):
arg1: int = 1
arg2: bool = True and under the hood wrap them in a mechanism like |
That's fine. That was just a thought really. It would be up to a linting tool to decide whether or not to warn for that anyway. |
As a compromise, perhaps rather than specifying a literal as the default value, we could adopt the ellipses instead to be used within the class KeyClickArgs(TypedDict):
widget: QtWidgets.QWidget | QtGui.QWindow
key: QtCore.Qt.Key | str
modifier: QtCore.Qt.KeyboardModifiers = ...
delay: int = ... |
Or perhaps instead, this is the reason why class KeyClickArgs(TypedDict):
widget: Required[QtWidgets.QWidget | QtGui.QWindow]
key: Required[QtCore.Qt.Key | str]
modifier: NotRequired[QtCore.Qt.KeyboardModifiers]
delay: NotRequired[int] or perhaps even simply using the class KeyClickArgs(TypedDict, total=False):
widget: QtWidgets.QWidget | QtGui.QWindow
key: QtCore.Qt.Key | str
modifier: QtCore.Qt.KeyboardModifiers # Default argument
delay: int # Default argument |
I think we established in this discussion that default arguments don't make sense for |
Uh oh!
There was an error while loading. Please reload this page.
Feature
Issue #6131 request default types be added to
TypedDict
. To separate concerns, I am interested in being able to specify default arguments toTypedDict
(as suggested in one of the comments).Pitch
Default arguments are a feature of python. Currently, there's no support for default arguments in
TypedDict
. This would be helpful both for new and existing codebases.Bonus: A common beginner blunder is to use mutable default arguments in functions and methods. AFAIK, linters (
flake8
,pylint
) do not yet warn about this. It would be interesting ifmypy
(and other static type checkers) could detect this and provide a useful warning.Syntax Idea
Borrowing from the comment suggestion, the following is intuitively what is trying to be achieved (though I don't know how this would be implemented):
The closest I am able to achieve currently is by using
Annotated
types, but it doesn't seem the intended use case forAnnotated
and I would prefer a more intuitive name like "Default":The text was updated successfully, but these errors were encountered: