-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Overloaded function implementation does not accept all possible arguments of signature #13077
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
I know
|
Mypy's error here looks correct to me. The implementation is not consistent with the first overload signature. The first overload indicates that it's OK to pass two position-only arguments (corresponding to |
Yes, the error is correct here. I assume by |
@JelleZijlstra @erictraut What is the correct way to type hint this? |
@JelleZijlstra @erictraut For reference, I am trying to be consistent with PySide6 |
Ya, typo. I meant |
The implementation signature should be something like def addTab(
self,
__widget: QtWidgets.QWidget,
__icon: QtGui.QIcon | QtGui.QPixmap | str | None,
__label: str = ...,
) -> int: It may be difficult to type the body of the implementation correctly. |
@JelleZijlstra That does not work for me either: |
You're missing |
@JelleZijlstra Can I request that you reopen this issue because I'm not seeing how the type hinting can be made to work here. |
@JelleZijlstra FYI, this issue will occur anywhere a |
You still didn't use the code that I put in my previous comment. |
I guess I meant the fourth, not the third argument. |
@JelleZijlstra I've tried both ways...perhaps I'm misunderstanding. Could you please show me? |
@JelleZijlstra Oh you meant the fourth argument. Sorry. Still no dice though 😕 |
@JelleZijlstra In addition, seems to not work for either signature AFAICT |
You need to add it to the implementation, not the overloads. The overloads seem correct, but the implementation needs to be written so it can accept either two or three arguments. |
@JelleZijlstra Are you saying I have to do this, because |
Yes, though I don't see why you can't keep them positional-only. Also, the Also, you can use native positional-only arguments (from PEP 570) instead of the double underscore convention. |
Ya,
From the mypy Python 3 cheat sheet: # An argument can be declared positional-only by giving it a name
# starting with two underscores:
def quux(__x: int) -> None:
pass
quux(3) # Fine
quux(__x=3) # Error Was this just a "name mangling trick" until the
So is the idea with this: @typing.overload
def addTab(
self,
__widget: QtWidgets.QWidget,
__arg__2: str,
) -> int: # noqa: D102
...
@typing.overload
def addTab(
self,
__widget: QtWidgets.QWidget,
__icon: QtGui.QIcon | QtGui.QPixmap,
__label: str,
) -> int: # noqa: D102
...
def addTab(
self,
widget: QtWidgets.QWidget,
icon: QtGui.QIcon | QtGui.QPixmap | str,
label: str = '',
) -> int: that one still can't call widget = MyTabWidget(QtWidgets.QWidget(), QtGui.QIcon(), label='Tab') # This errors; `label` is positional only and then I would use if isinstance(icon, str):
label = icon
else:
# So I don't have to retype functions called with `icon` here that expect it to be:
# QtGui.QIcon | QtGui.QPixmap
# not
# QtGui.QIcon | QtGui.QPixmap | str
icon = typing.cast(QtGui.QIcon | QtGui.QPixmap, icon)
# rest of implementation This will be a bit confusing with docstring documentation (since we use
because the docstring signature will have to differ from the actual signature. |
@JelleZijlstra @erictraut I would love your opinions on the above. ❤️ |
Uh oh!
There was an error while loading. Please reload this page.
Bug Report
Most likely related to Issue #11004 , but adding here in case there is more going on. The python/typing discussion here helped me understand this is a
mypy
issue.To Reproduce
I am using
mypy==0.961
exclusively (not any other static type checker; e.g.pyright
,pyre
,pytyped
, etc.) inbasic
mode (notstrict
).I've imported
qtpy
withPySide6
inpython==3.8.10
on Windows 10 x64-bit in VSCode1.68.1
. I am writing a subclass that overwritesQTabWidget
.PySide6
is a binding of the C++Qt6
library usingshiboken6
. C++ does not natively support keyword arguments andPySide6
has chosen to support only exporting optional arguments fromQt6
as keyword arguments.The documentation for PySide6 seems to indicate my stubs below are correct
However,
mypy
complains that the overloaded method doesn't accept all possible arguments:The workaround of specifying the first signature as
does not work for me because positional arguments cannot come after keyword arguments.
I also tried:
but that didn't work either.
Expected Behavior
Overloads are properly understood.
Actual Behavior
Overloads error that not all possible arguments are accepted.
Your Environment
0.961
pyproject.toml
mypy.ini
(and other config files): See above3.8.10
x64-bitWindows 10 x64-bit, Verson 20H2
The text was updated successfully, but these errors were encountered: