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

Issue 2781 #9380

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/pyright-internal/src/analyzer/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4033,9 +4033,10 @@ export class Binder extends ParseTreeWalker {
// To determine whether the first parameter of the method
// refers to the class or the instance, we need to apply
// some heuristics.
if (methodNode.d.name.d.value === '__new__') {
// The __new__ method is special. It acts as a classmethod even
// though it doesn't have a @classmethod decorator.
const implicitClassMethods = ['__new__', '__init_subclass__', '__class_getitem__'];
if (implicitClassMethods.includes(methodNode.d.name.d.value)) {
// Several methods are special. They act as class methods even
// though they don't have a @classmethod decorator.
isInstanceMember = false;
} else {
// Assume that it's an instance member unless we find
Expand Down
11 changes: 11 additions & 0 deletions packages/pyright-internal/src/tests/samples/initsubclass1.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,14 @@ class ClassH(ClassG):
# in the object.__init_subclass__ method.
class ClassI(a=3):
a: int


class ClassJ:
def __init_subclass__(cls, **kwargs: Any) -> None:
super().__init_subclass__(**kwargs)
cls.custom_attribute = 9


class ClassJChild(ClassJ):
def __init__(self):
reveal_type(self.custom_attribute, expected_text="int")
Loading