-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Infer the 'cls' argument of a classmethod as the defining class, not Any #292
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
Comments
Partial support implemented in db9efde by Jeff Walden. |
For reference, here's an example of code that incorrectly typechecks because of this:
Replacing |
The type of Consider this example:
To catch the above error, we have at least two options:
We could also decide to not care about this and allow calling For 2, we could provide an annotation that enforces this. Here are some potential ways to do it:
The same issue also happens with There are other related things around this such as 'self types' but they aren't immediately relevant to this issue. |
Could we do this in two stages?
In stage 1 we change the type of cls to be A (in general, the class that
lexically contains the class method definition). We'd catch calls of cls()
with arguments that don't match the lexically current `__init__` or
`__new__` (which is Matthew's example). We wouldn't catch the subclass that
changes the signature for `__init__` or `__new__` but neither do we catch
that today.
In stage 2 we add code that restricts the signature of subclass
constructors to be compatible with the base class constructor, but only if
there are any calls to cls() in the base class.
Now, there might be cases where a mix-in class assumes a specific signature
for cls() that isn't obvious from the mix-in class's own definition, but
mix-in classes are already wildly problematic. A systematic approach to
mix-in classes requires the code to be refactored to use ABCs that declare
the interface assumed by the mix-in class.
|
Sounds pretty reasonable. We figure out the details of stage 2 later on; stage 1 is simple to implement. |
Looking for |
Honestly I've always found |
For the thread: we've heard this request several times now at Dropbox, where we have a lot of classmethods. One place we heard it was on our recent survey of internal users. |
Add support for classmethod. Currently we support staticmethod.
The text was updated successfully, but these errors were encountered: