Skip to content

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

Closed

Description

@JukkaL

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.

Activity

  1. self-assigned this
    on Mar 27, 2017
  2. JukkaL commented on Mar 27, 2017

    @JukkaL
    CollaboratorAuthor

    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
  3. elazarg commented on Mar 27, 2017

    @elazarg
    Contributor

    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*'
  4. pkch commented on Mar 28, 2017

    @pkch
    Contributor

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

  5. removed their assignment
    on Aug 15, 2017
  6. JelleZijlstra commented on Aug 15, 2017

    @JelleZijlstra
    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.

  7. added a commit that references this issue on Aug 15, 2017
    f4dad9b
  8. added a commit that references this issue on Sep 28, 2017
    1a164b6
  9. gvanrossum commented on Feb 27, 2018

    @gvanrossum
    Member

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

  10. ilevkivskyi commented on Mar 7, 2018

    @ilevkivskyi
    Member

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

  11. gvanrossum commented on Mar 7, 2018

    @gvanrossum
    Member
  12. ilevkivskyi commented on Mar 7, 2018

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

  13. ilevkivskyi commented on Mar 9, 2018

    @ilevkivskyi
    Member

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

  14. mlhamel commented on Aug 16, 2018

    @mlhamel

    Any luck with this one?

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

  15. ilevkivskyi commented on Aug 17, 2018

    @ilevkivskyi
    Member

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

  16. mlhamel commented on Aug 17, 2018

    @mlhamel

    @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

  17. ilevkivskyi commented on Aug 18, 2018

    @ilevkivskyi
    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.

  18. self-assigned this
    on Sep 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions