Thanks to visit codestin.com
Credit goes to github.com

Skip to content

isinstance with self.__class__ infers 'object' as the type #3061

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

Closed
JukkaL opened this issue Mar 27, 2017 · 13 comments
Closed

isinstance with self.__class__ infers 'object' as the type #3061

JukkaL opened this issue Mar 27, 2017 · 13 comments
Assignees
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Mar 27, 2017

An unexpected type is revealed by this program:

from typing import Any

class A:
    def f(self, o: Any) -> None:
        isinstance(o, self.__class__) and reveal_type(o)  # Revealed type is 'builtins.object'

The issue was introduced by #2997. The most precise type would be A. Mypy used to infer Any as the type before #2997.

@JukkaL JukkaL added the bug mypy got something wrong label Mar 27, 2017
@JukkaL JukkaL self-assigned this Mar 27, 2017
@JukkaL
Copy link
Collaborator Author

JukkaL commented Mar 27, 2017

A slightly more straightforward way to repro:

from typing import Any

class A:
    def f(self, o: Any) -> None:
        if isinstance(o, self.__class__):
            reveal_type(o)  # builtins.object

@elazarg
Copy link
Contributor

elazarg commented Mar 27, 2017

It should be fixable by changing typeshed:

class object:
    @property
    def __class__(self: _T) -> Type[_T]: ...

Seems to work (at least for this particular case).

    reveal_type(o)  # Revealed type is 'tmp.A*'

@pkch
Copy link
Contributor

pkch commented Mar 28, 2017

@elazarg I think that's a good fix. It should probably be combined with #3056.

@JukkaL JukkaL removed their assignment Aug 15, 2017
@JelleZijlstra
Copy link
Member

@elazarg's proposed typeshed change isn't quite right because __class__ is writable. Though I suppose if you assign to __class__ there's little hope for typechecking your code. I'll try adding a setter.

@gvanrossum
Copy link
Member

This should be reopened since python/typeshed#1549 was reverted in python/typeshed#1632.

@gvanrossum gvanrossum reopened this Feb 27, 2018
@ilevkivskyi
Copy link
Member

@gvanrossum I think the original PR can be simply re-applied, since the issue that caused reverting it should be fixed now.

@gvanrossum
Copy link
Member

gvanrossum commented Mar 7, 2018 via email

@ilevkivskyi
Copy link
Member

Here is the PR python/typeshed#1951. I checked manually that it doesn't cause problems you mentioned in python/typeshed#1549 (comment) (will run testpr tomorrow).

@ilevkivskyi
Copy link
Member

@gvanrossum Unfortunately, there are still some problems with python/typeshed#1951, I will investigate next week.

@JukkaL JukkaL added the false-positive mypy gave an error on correct code label May 19, 2018
@mlhamel
Copy link

mlhamel commented Aug 16, 2018

Any luck with this one?

It would be great to have the insurance that __class__(self: _T) -> Type[_T]: ...

@ilevkivskyi
Copy link
Member

@mlhamel Unfortunately core mypy team is overloaded with other tasks plus two team members are on vacation now.

@mlhamel
Copy link

mlhamel commented Aug 17, 2018

@ilevkivskyi no worries i was mostly just wondering if there was anything new but if i can help in any way I would be pleased to give a try

@ilevkivskyi
Copy link
Member

@mlhamel TBH I don't remember now what exactly was the problem with python/typeshed#1951 that should fix this. I just remember that there was some weird interaction with classes that have an Any base class. One thing you can help with is if you have some large code bases could you try locally applying python/typeshed#1951 and checking if there are any problems with it.

@msullivan msullivan self-assigned this Sep 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal
Projects
None yet
Development

No branches or pull requests

8 participants