Skip to content

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

Merged
merged 7 commits into from
Mar 1, 2018
Merged

Conversation

elazarg
Copy link
Contributor

@elazarg elazarg commented Sep 26, 2017

  • Remove old comments, cleanup bail-out logic
  • Use fallback to find Instance when possible
  • Remove cast; move formatting logic to messages.py
  • Rename variables to match check_overlapping_op_methods() terminology

Also:

  • Remove unused method from TypeInfo
  • Remove many unused imports

Was part of #3227
EDIT: may fix #3468, or at least avoid the crash (since it removes the unsafe cast).

@elazarg elazarg force-pushed the cleanup-reverseop branch 3 times, most recently from f475823 to 9865c6e Compare September 27, 2017 12:39
* 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
Copy link
Member

@emmatyping emmatyping left a 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
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Collaborator

@msullivan msullivan left a 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)):
Copy link
Collaborator

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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?

@elazarg
Copy link
Contributor Author

elazarg commented Feb 9, 2018

I'm not sure why the tests are failing - the errors mention code that's not there at all.

@ilevkivskyi
Copy link
Member

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.)

@ilevkivskyi
Copy link
Member

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?

@elazarg
Copy link
Contributor Author

elazarg commented Feb 10, 2018

Thanks! Yep, a merge problem - too many import changes - so it's my fault (obviously). Sorry.

@gvanrossum
Copy link
Member

@JukkaL and/or @ilevkivskyi Can you review this so it may make it into 0.570 (if you think it's solid)?

@JukkaL
Copy link
Collaborator

JukkaL commented Feb 26, 2018

I'll look at this tomorrow.

Copy link
Collaborator

@JukkaL JukkaL left a 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
Copy link
Collaborator

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

Copy link
Contributor Author

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
Copy link
Collaborator

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.

Copy link
Contributor Author

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)

Copy link
Collaborator

@JukkaL JukkaL left a 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.

@JukkaL JukkaL merged commit 574744e into python:master Mar 1, 2018
carljm added a commit to carljm/mypy that referenced this pull request Mar 6, 2018
* 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)
yedpodtrzitko pushed a commit to kiwicom/mypy that referenced this pull request Mar 15, 2018
* 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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AttributeError: 'UnionType' object has no attribute 'type'
6 participants