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

a classonlymethod decorator #108

Open
KotlinIsland opened this issue Jan 31, 2024 · 1 comment
Open

a classonlymethod decorator #108

KotlinIsland opened this issue Jan 31, 2024 · 1 comment
Labels
good first issue Good for newcomers

Comments

@KotlinIsland
Copy link
Owner

class A:
    @classmethod
    def a(cls): ...
    @classonlymethod
    def b(cls): ...
A.a()  # ok
A.b()  # ok
A().a()  # ok
A().b()  # not ok (😞)

The reasoning is that the classmethod decorator is often used for alternative constructors, so using it on instances would be a mistaken usage.

implementation

We could attempt a vanilla implementation using descriptors, otherwise a simple alias

if not TYPE_CHECKING:
    classonlymethod = classmethod
else:
    class classonlymethod(classmethod):  #  or something
        ...
@DetachHead
Copy link
Collaborator

DetachHead commented Mar 24, 2024

classonlymethod can be implemented entirely using descriptors, and it will work in basedpyright with no additional changes/special casing. for it to work in basedmypy tho, python/mypy#17050 will need to be fixed

i think staticonlymethod is kinda redundant because you can achieve the same effect by just making it a function outside of the class

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants