-
-
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
Flexible callable #2426
Flexible callable #2426
Conversation
arg_type, | ||
verbosity = max(verbosity-1, 0)))) | ||
else: | ||
constructor = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: Move this constant dict thing out to a constant
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prelim comments on my own diff.
ARG_STAR: "StarArg", | ||
ARG_STAR2: "KwArg", | ||
}[arg_kind] | ||
if arg_kind in ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move tuple constant out to a constant.
def __init__(name=None, typ=Any, keyword_only=False): | ||
self.name = name | ||
self.typ = typ | ||
self.named_only = named_only |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add the rest of the kinds of arguments.
@@ -821,7 +839,12 @@ def construct_function_type(self, args: List[Argument], ret_type: Type, | |||
if ret_type is None: | |||
ret_type = AnyType(implicit=True) | |||
arg_kinds = [arg.kind for arg in args] | |||
arg_names = [arg.variable.name() for arg in args] | |||
if include_names: | |||
arg_names = [arg.variable.name() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This implements "names starting with __ are unnamed arguments". It's probably necessary to actually get the typeshed files passing.
if right_name is not None: | ||
left_by_name = left.argument_by_name(right_name) | ||
else: | ||
left_by_name = None | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Blank lines totally match each other! For sure!
@@ -22,220 +22,220 @@ class NodeVisitor(Generic[T]): | |||
|
|||
# Module structure | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are all overloaded later with different arg names. See also needing the positional-only syntax.
@@ -1230,9 +1230,9 @@ class D: | |||
def __getattr__(self, x: str) -> None: pass | |||
[out] | |||
main: note: In member "__getattr__" of class "B": | |||
main:4: error: Invalid signature "def (self: __main__.B, x: __main__.A) -> __main__.B" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed argument names from special functions so these error messages changed.
int_named_str_fun = isf # E: Incompatible types in assignment (expression has type Callable[[Arg('ii', int, False), Arg('ss', str, False)], str], variable has type Callable[[int, Arg('s', str, False)], str]) | ||
int_named_str_fun = iosf | ||
|
||
[builtins fixtures/dict.pyi] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So many more to come.
Mypy doesn't check argument name compatibility in method overrides by design -- it would generate a huge number of errors for a lot of real-world code. Generally code like this implicitly expects callers not to use keyword arguments. Clearly this is not sound, which is unfortunate. On the other hand, users have generally not been complaining about this. Also, many methods implemented in C only accept positional arguments. For them, we'd need a way of specifying in stub files that the arguments don't actually have names, and tracking this down for every single C method would be a pretty big task. When we previously discussed this, one idea was to use I think that argument name compatibility checking for overrides should be left out of this PR and discussed separately, as it's a much bigger change than making |
@JukkaL How about I split this into a few different diffs:
And submit them each as separate pull requests? They might have a dependency tree among them. |
Sounds like a good plan! |
I have started the series of smaller pull requests that this turned into. |
This is a draft implementation of the work discussed in python/typing#264
Most of the subtlety was in getting the function subtyping rules (what I believe to be) right.
Unfortunately basically all the typeshed stub files violate these rules all over the place -- specifically, arguments in a function overriding a superclass function must be named the same thing, otherwise calling that function by keyword is going to fail. So I still need to resolve that stuff.
Also I need to write a pile more tests.
Also also apparently I haven't implemented StarArg and KwArg yet.
Actually there's a decent amount left to do.