Skip to content

Better story around class methods, self types and type object types #1298

@JukkaL

Description

@JukkaL

There are several issues that are pretty closely related to each other. This issue tracks them together. Here are the issues:

Summary of how the above are related:

  • If we support Type[T] we could use that as the type of the cls argument in class methods.
  • The result of cls(...) could have type SelfType.
  • The type of self.__class__ could be Type[T] (similarly for type(self)).

Another thing to discuss is determining/specifying when overrides of __init__ should be compatible with a base class, which is usually not required. For example, we want to restrict __init__ to accept a single argument here:

class A:
    def __init__(self, x: int) -> None: ...

    @classmethod
    def f(cls) -> None:
        a = cls(1)
        ...

class B(A):
    def __init__(self) -> None: ...   # should be rejected!

There are at least are a few different ways of fixing the above issue:

  • Detect any calls to cls and if such things exist, enforce __init__ compatibility implicitly. This wouldn't work well in a stub as function bodies are empty.
  • Require a magic __init__ decorator or class decorator that causes __init__ compatibility to be enforced. Don't support cls(...) unless the decorator exists.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions