-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
apply_generic_arguments: replace runtime checks with assertions #2346
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
Conversation
| msg = messages.temp_message_builder() | ||
| applied = mypy.applytype.apply_generic_arguments(type, inferred_vars, msg, context=target) | ||
| if msg.is_errors() or not isinstance(applied, CallableType): | ||
| if msg.is_errors(): |
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.
Perhaps msg.is_errors() should be an assertion too?
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 doubt it, unless you can prove that apply_generic_arguments() cannot produce any errors.
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.
Should it?
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.
It definitely can produce errors, since it checks type variable bounds/allowed values.
mypy/applytype.py
Outdated
| 'def (int) -> int'. | ||
| Note that each type can be None; in this case, it will not be applied. | ||
| Can only be None when len(tvars) != len(types) |
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 comment looks wrong.
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.
Yes it's outdated. Thanks.
mypy/applytype.py
Outdated
| Note that each type can be None; in this case, it will not be applied. | ||
| Can only be None when len(tvars) != len(types) | ||
| """ | ||
| assert len(callable.variables) == len(types) |
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'd move the assert after the assignment.
mypy/applytype.py
Outdated
| Note that each type can be None; in this case, it will not be applied. | ||
| Can only be None when len(tvars) != len(types) | ||
| """ | ||
| assert len(callable.variables) == len(types) |
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'd move the assert after the assignment.
|
Thanks! |
Part of #2272.
The only place where the number of type arguments in type application might not match is when the user does that; this is explicitly checked in
visit_type_application. Other places involve type inference, which must be correct "by construction" and if it isn't, that's a bug we want to catch, instead of returning AnyType.It also allows us to remove casts.
cc @ddfisher