Skip to content

Self-type doesn't seem to work for properties #2402

Closed

Description

@gvanrossum

I found this when trying to use self-type for pathlib (see PR python/typeshed#658). There are a few properties that have type PurePath and are repeated in the Path subclass. The stub uses plain attributes for these, but the implementation has properties, so I want to replace them with properties. However this doesn't seem to work. In particular I'm trying to add this to class PurePath:

    @property
    def parents(self: _P) -> Sequence[_P]: ...
    @property
    def parent(self: _P) -> _P: ...

Now I have an example class:

class MyPath(pathlib.PosixPath):
    def parity(self):
        return len(str(self)) % 2

root = MyPath('/')
reveal_type(root.parent)

This reveals MyPath as the type but also gives an error:

__tmp__.py:14: error: Revealed type is '__tmp__.MyPath*'
__tmp__.py:14: error: Invalid method type

If I remove the self-type, e.g.

    @property
    def parents(self) -> Sequence[PurePath]: ...
    @property
    def parent(self) -> PurePath: ...

the error goes away but the revealed type reverts back to (of course) PurePath.

@elazarg thoughts?

Activity

  1. elazar commented on Nov 4, 2016

    @elazar

    @gvanrossum I'm thinking you were intending to @ mention someone else. 😉 It would be my luck that, the one time I'm contacted by the creator of Python, it's by mistake.

  2. gvanrossum commented on Nov 4, 2016

    @gvanrossum
    MemberAuthor

    Yup, sorry for that. :-/

  3. elazarg commented on Nov 5, 2016

    @elazarg
    Contributor

    I think it's just an overly-restrictive check. See the PR.

  4. added a commit that references this issue on Nov 5, 2016
    e4fd655
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions