-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Handling de-facto positional-only arguments with overloads #5235
Comments
In stubs, you can just use the Perhaps that's also what we should have users do in implementation files. |
Huh, I didn't know that was even a thing. It looks like the I guess the only downside is that it leaves the signatures looking sort of ugly when you have a lot of those parameters -- maybe I just need to find a different example for the docs altogether... I'll close this in a little bit if nobody else chimes in. |
I think there is still an action item here. Using
|
Related failure case: Overloads have meaningful parameter names, but implementation uses arg1 and arg2=None. @overload
def add_row(right: Type[Right]) -> Any:
...
@overload
def add_row(left: Type[Left], right: Type[Right]) -> Any:
...
@overload
def add_row(label: str, right: Type[Right]) -> Any:
...
def add_row(arg1: Any, arg2: Any = None) -> Any:
impl |
mypy is behaving as expected, and with Python 3.7 dead it is much easier to mark positional-only args |
In my PR for updating the mypy docs, @gvanrossum pointed out that one of my examples didn't typecheck -- the simplified repro was this:
After debugging, I realized that the checks are actually technically correct: the overloads suggest that running
foo(a=1, b=2)
is type-safe; that call will crash at runtime.This feels a bit annoying though, so I'm thinking it'd be nice if we could relax these checks -- basically, allow the existence of "de-facto positional-only arguments".
I'm thinking one way we can do this is to:
So under this proposal,
foo
would continue to type-check, but we'd report an error if somebody tried doingfoo(a=1, a=2)
.The main disadvantage is that this would cause feature disparity with overloads in stubs since stubs don't allow you to include an implementation. We could either live with this or maybe allow users to add an implementations with empty bodies in stubs if we really want to maintain parity.
Thoughts? (I wanted to file an issue to get consensus/see if anybody is able to come up with a better solution before I start implementation.)
The text was updated successfully, but these errors were encountered: