Skip to content

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

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

Closed
JukkaL opened this issue Mar 17, 2016 · 4 comments
Closed

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

JukkaL opened this issue Mar 17, 2016 · 4 comments

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Mar 17, 2016

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.
@JukkaL
Copy link
Collaborator Author

JukkaL commented Apr 14, 2016

I propose moving this to 0.3.3.

@gvanrossum gvanrossum modified the milestones: 0.3.3, 0.3.2 Apr 14, 2016
@gvanrossum
Copy link
Member

Moved to 0.3.3.

@gvanrossum gvanrossum modified the milestones: 0.5, 0.4.x Jul 14, 2016
@gvanrossum
Copy link
Member

Anything needing a PEP change probably shouldn't be labeled 0.4.x.

Really, the individual issues should be given proper milestones.

@gvanrossum
Copy link
Member

Closing, as only #1212 is still open.

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

No branches or pull requests

2 participants