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

Error occure when using classonlymethod decorator #1485

Closed
RobertM15 opened this issue May 10, 2023 · 2 comments · Fixed by #1494
Closed

Error occure when using classonlymethod decorator #1485

RobertM15 opened this issue May 10, 2023 · 2 comments · Fixed by #1494
Labels
bug Something isn't working

Comments

@RobertM15
Copy link

RobertM15 commented May 10, 2023

Bug report

from django.utils.decorators import classonlymethod


class Foo:
    @classonlymethod
    def bar(cls):
        pass

Error from mypy:

error: Argument 1 to "classonlymethod" has
incompatible type "Callable[[Foo], Any]"; expected "Callable[[Any, VarArg(Any), KwArg(Any)], Any]"  [arg-type]

What's wrong

Error occurred, but it seems to be more related to mypy itself. The problem is described here

I created this issue here, because I think the solution is very easy to fix it and what I understood from here there will not change how mypy treats subclasses of classmethod

Problem stub:
django-stubs/utils/decorators.pyi

class classonlymethod(classmethod): ...

How is that should be

Changing to alias solves the error

classonlymethod = classmethod

Let me know what do you think

System information

  • OS:
  • python version: 3.9.16
  • django version: 3.2.18
  • mypy version: 1.2.0
  • django-stubs version: 4.2.0
  • django-stubs-ext version: 4.2.0
@RobertM15 RobertM15 added the bug Something isn't working label May 10, 2023
@intgr
Copy link
Collaborator

intgr commented May 10, 2023

I've experienced this issue myself as well. I'm using # type: ignore[arg-type] right now.

I agree that aliasing the types (classonlymethod = classmethod) should fix it. The downside is that type checkers won't know that classonlymethod methods aren't accessible on instances. But that's already the case right now.

Not sure if there could be other downsides?

Ideally this would be solved by adding mypy plugin logic, to teach mypy about instance behavior. But not a high priority IMO.

@adamchainz
Copy link
Contributor

I've made #1494 to switch to an alias.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

Successfully merging a pull request may close this issue.

3 participants