-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Cleanup check_reverse_op_method #4017
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
f475823
to
9865c6e
Compare
* Remove old comments, clean bail-out logic * Use fallback to find Instance when possible * Remove cast; move formatting logic to messages.py * remove unused method * rename variables to match check_overlapping_op_methods terminology * remove many unused imports
9865c6e
to
49a4670
Compare
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.
Just one suggestion, otherwise looks good.
mypy/checker.py
Outdated
if len(typ.arg_types) == 2: | ||
# TODO check self argument kind | ||
if len(reverse_type.arg_types) != 2: | ||
return |
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 actually think this should raise an internal error instead of returning, since we should never reach there, now that the signatures are checked.
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.
Makes sense to me. Fixed.
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.
Looks good, modulo my one test request
mypy/checker.py
Outdated
return | ||
forward_name = nodes.normal_from_reverse_op[reverse_name] | ||
forward_base = reverse_type.arg_types[1] | ||
if isinstance(forward_base, (FunctionLike, TupleType, TypedDictType)): |
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.
Could you add a test for one or all of these cases, which look new?
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.
Added a test for TupleType. I'm not sure how to do it for the other two, if at all possible.
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.
@msullivan what do you think?
I'm not sure why the tests are failing - the errors mention code that's not there at all. |
It's on master, you should merge to see it. (Btw it is my fault, missed this in my review, but I will be grateful if you add this import.) |
Hm, I double-checked, the import is there. @elazarg Could you please merge master, maybe it is some kind of subtle merge problem? |
Thanks! Yep, a merge problem - too many import changes - so it's my fault (obviously). Sorry. |
6816839
to
26f58bd
Compare
@JukkaL and/or @ilevkivskyi Can you review this so it may make it into 0.570 (if you think it's solid)? |
I'll look at this tomorrow. |
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.
Looks mostly good, but there's one crash issue that should be fixed before this can be merged.
mypy/checker.py
Outdated
if isinstance(ret_type, AnyType): | ||
return | ||
if isinstance(ret_type, Instance): | ||
if ret_type.type.fullname() == 'builtins.object': | ||
return | ||
|
||
if len(typ.arg_types) == 2: | ||
# TODO check self argument kind | ||
assert len(reverse_type.arg_types) == 2 |
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 assertion isn't safe. Type checking this program will cause a crash:
class B:
def __radd__(*self) -> int: pass
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.
Handled by duplicating self
mypy/checker.py
Outdated
forward_inst = forward_inst.fallback | ||
if not (isinstance(forward_inst, (Instance, UnionType)) | ||
and forward_inst.has_readable_member(forward_name)): | ||
return |
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.
Are there any other types that we could plausibly handle here instead of just returning, such as type variables? If yes, it may be worth adding a TODO comment.
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.
Instead of a TODO, added handling of TypeType and TypeVar (+tests)
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.
Thanks for the updates! LGTM now.
* master: New files shouldn't trigger a coarse-grained rebuild in fg cache mode (python#4669) Bump version to 0.580-dev Update revision history for 0.570 (python#4662) Fine-grained: Fix crashes when refreshing synthetic types (python#4667) Fine-grained: Support NewType and reset subtype caches (python#4656) Fine-grained: Detect changes in additional TypeInfo attributes (python#4659) Fine-grained: Apply semantic analyzer patch callbacks (python#4658) Optimize fine-grained update by using Graph as the cache (python#4622) Cleanup check_reverse_op_method (python#4017) Fine-grained: Fix AST merge issues (python#4652) Optionally check that we don't have duplicate nodes after AST merge (python#4647)
* Remove old comments, clean bail-out logic * Use fallback to find Instance when possible * Remove cast; move formatting logic to messages.py * Remove unused method * Rename variables to match check_overlapping_op_methods terminology * Remove many unused imports Potential fix to python#3468, or at least avoids the crash (since it removes the unsafe cast).
messages.py
check_overlapping_op_methods()
terminologyAlso:
Was part of #3227
EDIT: may fix #3468, or at least avoid the crash (since it removes the unsafe cast).