Skip to content
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

Use generic self type rather than specifying "Con" #190

Open
Hubro opened this issue Dec 22, 2021 · 1 comment
Open

Use generic self type rather than specifying "Con" #190

Hubro opened this issue Dec 22, 2021 · 1 comment
Assignees
Labels

Comments

@Hubro
Copy link

Hubro commented Dec 22, 2021

The i3ipc.aio.connection.Con subclass overrides the command and command_children methods, but nothing else. All the find_* methods are untouched, and their type hints state: -> List['Con']. This means that even if you're in an AIO context, any type checker or language server will think the find_* methods return regular (non-AIO) containers. Any awaits on those containers will generate errors:

image

This issue is discussed in this PEP-0673: https://www.python.org/dev/peps/pep-0673/

It seems like Python 3.11 will include proper syntax for this: -> Self. However, for now, the "correct" workaround is to use a bound type variable:

TCon = TypeVar("TCon", bound="Con")

class Con:
    ...

    def find_focused(self) -> Optional[TCon]:
        ...

    def find_by_id(self, id: int) -> Optional[TCon]:
        ...

    def find_by_pid(self, id: int) -> List[TCon]:
        ...

    ...

This will cause the find_* methods on the AIO subclass to return instances of the AIO subclass rather than the parent class.

@acrisci
Copy link
Member

acrisci commented Dec 22, 2021

Oh ok I think that would be a good inclusion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants