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

Fix checking of self when accessing a non-method callable attribute (take 2) #4016

Merged
merged 10 commits into from Oct 12, 2017

Conversation

Projects
None yet
3 participants
@elazarg
Contributor

elazarg commented Sep 26, 2017

Fix #3223. Minimal examples:

from typing import Callable
class X:
    f: Callable[[object], None]
X().f  # E: Invalid method type
class X:
    @property
    def g(self: object): pass
X().g  # E: Invalid method type

The problem is that for non-methods the check performed on access to self is whether object <: X instead of the other way around.

  1. Reverse subtyping check. This change alone fixes the issue described above.
  2. For classmethod, fallback on the argument instead of on the parameter
  3. Mimic dispatch better: meet original_type with static class. This handles the case were instead of X() above there was something of type Union[X, Y].
  4. Better error message

Take 2 of #3227, but without the reverse-operator part

Fix handling of non-method callable attribute (#3223)
1. Reverse subtyping check for self argument
2. For classmethod, fallback on the argument instead of on the parameter
3. Mimic dispatch better: meet original_type with static class
4. Better error message

@elazarg elazarg referenced this pull request Sep 26, 2017

Closed

Release 0.530 planning #4009

5 of 5 tasks complete
@JukkaL

Thanks for the PR! Note that this is not a full review, just some quick notes.

Can you update the PR message to describe the problem that this PR fixes, with an example? Currently I need to look up the issue to understand what is going on. Another nit is that the title of the PR doesn't communicate the purpose of this PR very well, but maybe it's hard to summarize it better.

I checked that this doesn't cause any issues with internal Dropbox codebases.

Show outdated Hide outdated mypy/checkmember.py Outdated

@elazarg elazarg changed the title from Fix handling of non-method callable attribute (take 2) to Fix checking of self when accessing a non-method callable attribute (take 2) Sep 28, 2017

@elazarg

This comment has been minimized.

Show comment
Hide comment
@elazarg

elazarg Sep 28, 2017

Contributor

Updated the PR description and the title. I hope it's an improvement.

Contributor

elazarg commented Sep 28, 2017

Updated the PR description and the title. I hope it's an improvement.

@gvanrossum

This comment has been minimized.

Show comment
Hide comment
@gvanrossum

gvanrossum Sep 29, 2017

Member

Sorry, this is too late to make it into the 0.530 release. It's a minor issue and it touches subtle parts of the codebase. Let's postpone it.

Member

gvanrossum commented Sep 29, 2017

Sorry, this is too late to make it into the 0.530 release. It's a minor issue and it touches subtle parts of the codebase. Let's postpone it.

@elazarg elazarg referenced this pull request Oct 10, 2017

Closed

Release 0.540 planning #4084

@JukkaL JukkaL self-assigned this Oct 10, 2017

@JukkaL

Thanks for fixing this -- it now looks cleaner (plus it also works better!). This concludes my review.

Some of the self type related code is still kind of hard to understand -- I added some ideas below for how to clarify things. If some of the suggested changes would require many additional changes (such as renaming original_type to something else) it's probably better to do that as a separate PR.

Show outdated Hide outdated mypy/checkmember.py Outdated
Show outdated Hide outdated mypy/checkmember.py Outdated
Show outdated Hide outdated test-data/unit/check-selftype.test Outdated
Show outdated Hide outdated mypy/checkmember.py Outdated
@elazarg

This comment has been minimized.

Show comment
Hide comment
@elazarg

elazarg Oct 11, 2017

Contributor

@JukkaL I will only be available for a couple of hours from now, so I won't be able to respond to further comments in time, unless ~30 hours from now will be good enough. As I understand it, the behavior is OK, and I hope it will be possible to resolve further issues in docstrings and comments even after the branch is cut.

Contributor

elazarg commented Oct 11, 2017

@JukkaL I will only be available for a couple of hours from now, so I won't be able to respond to further comments in time, unless ~30 hours from now will be good enough. As I understand it, the behavior is OK, and I hope it will be possible to resolve further issues in docstrings and comments even after the branch is cut.

@JukkaL

Just a few minor things. If you can't make the updates before I cut the release branch this Friday, I can merge this as such before cutting the branch.

Show outdated Hide outdated test-data/unit/check-selftype.test Outdated
Show outdated Hide outdated test-data/unit/check-selftype.test Outdated
a: A[int]
b: B[str]
reveal_type(a.g) # E: Revealed type is 'builtins.int'
--reveal_type(a.gt) # E: Revealed type is 'builtins.int'

This comment has been minimized.

@JukkaL

JukkaL Oct 11, 2017

Collaborator

Why are these commented out?

@JukkaL

JukkaL Oct 11, 2017

Collaborator

Why are these commented out?

This comment has been minimized.

@elazarg

elazarg Oct 11, 2017

Contributor

The instantiation of selftype fails in the commented tests. I think it is not a problem in this PR, and fixing these requires changes in bind_self.

I just forced an update that removed two of the comments; I hope it doesn't cause a mess.

@elazarg

elazarg Oct 11, 2017

Contributor

The instantiation of selftype fails in the commented tests. I think it is not a problem in this PR, and fixing these requires changes in bind_self.

I just forced an update that removed two of the comments; I hope it doesn't cause a mess.

This comment has been minimized.

@elazarg

elazarg Oct 11, 2017

Contributor

Sorry - I didn't force it; I will add as a new commit in a moment.

@elazarg

elazarg Oct 11, 2017

Contributor

Sorry - I didn't force it; I will add as a new commit in a moment.

This comment has been minimized.

@elazarg

elazarg Oct 11, 2017

Contributor

To elaborate, bind_self does not unify K[T] and A[T] even if it can. It just does not check for this case, and simply trims the first argument. It only performs such unification is for TypeType.

@elazarg

elazarg Oct 11, 2017

Contributor

To elaborate, bind_self does not unify K[T] and A[T] even if it can. It just does not check for this case, and simply trims the first argument. It only performs such unification is for TypeType.

elazarg added some commits Oct 11, 2017

@JukkaL

JukkaL approved these changes Oct 12, 2017 edited

Thanks, looks good.

@JukkaL JukkaL merged commit bf9dc4c into python:master Oct 12, 2017

2 checks passed

continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment