Skip to content

self.__class__() not supported #1195

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
timabbott opened this issue Jan 29, 2016 · 3 comments
Closed

self.__class__() not supported #1195

timabbott opened this issue Jan 29, 2016 · 3 comments

Comments

@timabbott
Copy link

Mypy is OK with accessing self.__class__ but not self.__class__(...)

Reproducer:

class S(object):
    def foo(self):
        return self.__class__()

Error:

/home/tabbott/foo.py: note: In member "foo" of class "S":
/home/tabbott/foo.py:4: error: "type" not callable
@gvanrossum
Copy link
Member

Well, maybe there should be a better error message, but calling
self.class() to construct an instance is very problematic. A
subclass constructor might have a different signature that is
unknowable when you are checking the base class. So unless in your
world you have a strong rule that subclass constructors must conform
to a certain signature, this should be flagged IMO. (OTOH a way to
declare that you do have such a rule might be useful -- maybe as an
annotation on init()?)

@JukkaL
Copy link
Collaborator

JukkaL commented Jan 29, 2016

Yeah, that's the reason why this doesn't work right now. Having an __init__ annotation would be a reasonable way to getting to work. Maybe provide a decorator in typing that could be used for this:

from typing import whatever

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

class B(A):
    def __init__(self) -> None: ...  # error!

Not sure what we would call it, though.

@gvanrossum
Copy link
Member

This no longer produces an error.

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

4 participants