-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Fix 7190:__init_subclass__ is not type-checked #7452
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
Changes from 1 commit
a709e11
dd3df66
1dfae8c
081e19c
145d254
bd66bca
1d12de7
7dccdb7
f5cc79a
1c62e37
6729fe8
502aa3f
dfb6551
b860c3a
012b817
a8fcb3e
ef295b5
ddc1dce
2c0c20f
67a761b
79208fd
8eb7b8d
9a7ab31
2281a86
f8f330a
0a16c0c
0850dff
798ff33
6f5bda9
ab8a6c8
7da5d11
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1696,12 +1696,11 @@ def check_init_subclass(self, defn: ClassDef) -> None: | |
typ = defn.info | ||
# At runtime, only Base.__init_subclass__ will be called | ||
# we skip the current class itself. | ||
found = False | ||
for base in typ.mro[1:]: | ||
# 'object.__init_subclass__ is a dummy method with no arguments, always defined | ||
# there is no use to call it | ||
if base.name() != 'object' \ | ||
and base.defn.info: # there are "NOT_READY" instances | ||
# during the tests, so I filter them out... | ||
# there are "NOT_READY" instances | ||
# during the tests, so I filter them out... | ||
if base.defn.info: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I have an idea why this may be missing, but anyway this is probably not important since you don't need this anyway, everywhere where you use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. understood! |
||
for method_name, method_symbol_node in base.defn.info.names.items(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need this to be a cycle, just use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or even simpler (if you are not going to use if '__init_subclass__' not in base.names:
continue There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you are right, I have modified accordingly |
||
if method_name == '__init_subclass__': | ||
name_expr = NameExpr(defn.name) | ||
|
@@ -1723,9 +1722,13 @@ def check_init_subclass(self, defn: ClassDef) -> None: | |
self.expr_checker.accept(call_expr, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using
Please add a test for this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm sorry, I don't understand. I tried to produce a mypy runtime exception with 2 modules, one of which defines a Base, and the other one doesn't import that Base, and it only gave me a nice error I added a test for that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It is not what I wanted. I wanted something like this: # file bases.py
class Base:
def __init_subclass__(cls, **kwargs) -> None: ...
class MidBase(Base):
...
# file main.py
from bases import MidBase
class Main(MidBase, test=False):
... |
||
allow_none_return=True, | ||
always_allow_any=True) | ||
# We are only interested in the first Base having __init_subclass__ | ||
# all other (highest) bases have already been checked. | ||
# there is only one such method method | ||
found = True | ||
break | ||
# We are only interested in the first Base having __init_subclass__ | ||
# all other (highest) bases have already been checked. | ||
gantsevdenis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if found: | ||
break | ||
return | ||
|
||
def check_protocol_variance(self, defn: ClassDef) -> None: | ||
|
Uh oh!
There was an error while loading. Please reload this page.