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

Incorporate typing_extensions into type checkers #19

Closed
jakub-toptal opened this issue Oct 6, 2022 · 3 comments
Closed

Incorporate typing_extensions into type checkers #19

jakub-toptal opened this issue Oct 6, 2022 · 3 comments
Assignees
Labels
type:enhancement New feature or enhancement visitble to users
Milestone

Comments

@jakub-toptal
Copy link
Contributor

jakub-toptal commented Oct 6, 2022

What's needed?

Type annotations in some cases are limited and thus it would be good to enhance them with what the typing-extensions package has to offer.

One example of this would be @classmethod, which always uses cls argument as the first argument. Ideally, we would annotate it with the Self type from the typing-extensions package.

Proposed solution

Use typing-extensions package to enhance type annotations.

Use cases

This can be applied to @classmethods and perhaps other functions used as decorators. One apparent case where we could use better type checking is the @actor decorator, which decorates classes at runtime making it impossible for pylint to verify if attributes / methods are defined on the decorated class.

Alternatives and workarounds

In case of @classmethod, we an omit the type annotation for the cls argument and it still passes type checking.

Otherwise, we have to explicitly tell type checkers pylint and/or mypy that a particular line should be ignored, because there is no correct type annotation available.

Additional context

No response

@jakub-toptal jakub-toptal added part:❓ We need to figure out which part is affected priority:❓ We need to figure out how soon this should be addressed type:enhancement New feature or enhancement visitble to users labels Oct 6, 2022
@leandro-lucarella-frequenz
Copy link
Contributor

One important clarification:

Using Self is particularly useful for @classmethods acting as alternative constructors, because the cls argument should a special case in type checkers, as it is known that it always have to be the type of the class defining the @classmethod.

But for @classmethods acting as alternative constructors, you also need to return the type of the class defining the @classmethod, and this has to work too for subclasses not re-defining the alternative constructor.

As a workaround we can use the class name explicitly.

For example:

class S:
    @classmethod
    def make(cls) -> S:
        return cls()

class T(S):
    pass

def print_t(t: T) -> None:
    print(t)

print_t(T.make())

This should fail a type checking, because T.make() is marked to return a S, not a T. But this should work:

from typing_extensions import Self

class S:
    @classmethod
    def make(cls) -> Self:  # <------
        return cls()

class T(S):
    pass

def print_t(t: T) -> None:
    print(t)

print_t(T.make())

Note: this is untested because mypy doesn't support Self yet, but is what I assume it should happen :)

@leandro-lucarella-frequenz
Copy link
Contributor

Some other important clarifications:

  • Self is introduced in Python 3.11.
  • typing-extensions is not a random package, is an official Python package maintained by the Python organization and only includes features backported from newer Python versions so they are available for older versions. Thus the use of this package is by definition temporary (until we can depend on a new enough Python minimal version)
  • mypy doesn't support Self yet, this is why we didn't go for it. There is a PR, so support should come fairly soon. Implementation of PEP 673 (typing.Self) python/mypy#11666
  • This comes from another PR's discussion: Change component data wrappers to be data classes #4 (comment)

@jakub-toptal jakub-toptal removed part:❓ We need to figure out which part is affected priority:❓ We need to figure out how soon this should be addressed labels Oct 7, 2022
@leandro-lucarella-frequenz
Copy link
Contributor

We already have typing_extensions as a dependency and mypy already supports Self, we just need to start using it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:enhancement New feature or enhancement visitble to users
Projects
Development

No branches or pull requests

2 participants