Repository navigation
Allow instantiation of Type[A], if A is abstract #2853
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 5 commits
b0ff0cc
71ea62f
ec22e4d
80c82e7
f52a3bd
65eab95
4d2e6fd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -594,6 +594,8 @@ def is_implicit_any(t: Type) -> bool: | |
| self.fail(msg, defn) | ||
| if note: | ||
| self.note(note, defn) | ||
| if defn.is_class and isinstance(arg_type, CallableType): | ||
| arg_type.is_classmethod_class = True | ||
| elif isinstance(arg_type, TypeVarType): | ||
| # Refuse covariant parameter type variables | ||
| # TODO: check recursively for inner type variables | ||
|
|
@@ -1149,6 +1151,16 @@ def check_assignment(self, lvalue: Lvalue, rvalue: Expression, infer_lvalue_type | |
| else: | ||
| rvalue_type = self.check_simple_assignment(lvalue_type, rvalue, lvalue) | ||
|
|
||
| # Special case | ||
| if ( | ||
| isinstance(rvalue_type, CallableType) and rvalue_type.is_type_obj() and | ||
|
Member
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'm not keen on this formatting. |
||
| rvalue_type.type_object().is_abstract and | ||
| isinstance(lvalue_type, TypeType) and | ||
| isinstance(lvalue_type.item, Instance) and lvalue_type.item.type.is_abstract | ||
| ): | ||
| self.fail("Cannot only assign non-abstract classes" | ||
|
Member
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. "Cannot only" -> "Cannot" (I presume).
Member
Author
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. Oh, sorry, it should be "Can only" (this is probably a worst kind of mistakes). |
||
| " to a variable of type '{}'".format(lvalue_type), rvalue) | ||
| return | ||
| if rvalue_type and infer_lvalue_type: | ||
| self.binder.assign_type(lvalue, | ||
| rvalue_type, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -351,7 +351,9 @@ def check_call(self, callee: Type, args: List[Expression], | |
| """ | ||
| arg_messages = arg_messages or self.msg | ||
| if isinstance(callee, CallableType): | ||
| if callee.is_concrete_type_obj() and callee.type_object().is_abstract: | ||
| if (callee.is_type_obj() and callee.type_object().is_abstract | ||
| # Exceptions for Type[...] and classmethod first argument | ||
| and not callee.from_type_type and not callee.is_classmethod_class): | ||
|
Member
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. Maybe also except static methods? I found a case in our codebase that goes basically like this: class Logger:
@staticmethod
def log(a: Type[C]): ...
class C:
@classmethod
def action(cls) -> None:
Logger.log(cls) #E: Only non-abstract class can be given where 'Type[C]' is expected
@abstractmethod
...(I'm far from positive that it's because of this line though.)
Member
Author
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. This code looks reasonable, I added a corresponding exception to |
||
| type = callee.type_object() | ||
| self.msg.cannot_instantiate_abstract_class( | ||
| callee.type_object().name(), type.abstract_attributes, | ||
|
|
@@ -437,7 +439,10 @@ def analyze_type_type_callee(self, item: Type, context: Context) -> Type: | |
| if isinstance(item, AnyType): | ||
| return AnyType() | ||
| if isinstance(item, Instance): | ||
| return type_object_type(item.type, self.named_type) | ||
| res = type_object_type(item.type, self.named_type) | ||
| if isinstance(res, CallableType): | ||
| res = res.copy_modified(from_type_type=True) | ||
| return res | ||
| if isinstance(item, UnionType): | ||
| return UnionType([self.analyze_type_type_callee(item, context) | ||
| for item in item.items], item.line) | ||
|
|
@@ -840,6 +845,12 @@ def check_arg(self, caller_type: Type, original_caller_type: Type, | |
| messages.does_not_return_value(caller_type, context) | ||
| elif isinstance(caller_type, DeletedType): | ||
| messages.deleted_as_rvalue(caller_type, context) | ||
| # Only non-abstract class could be given where Type[...] is expected | ||
|
Member
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. could -> can (again :-) |
||
| elif (isinstance(caller_type, CallableType) and isinstance(callee_type, TypeType) and | ||
| caller_type.is_type_obj() and caller_type.type_object().is_abstract and | ||
| isinstance(callee_type.item, Instance) and callee_type.item.type.is_abstract): | ||
| messages.fail("Only non-abstract class can be given where '{}' is expected" | ||
| .format(callee_type), context) | ||
| elif not is_subtype(caller_type, callee_type): | ||
| if self.chk.should_suppress_optional_error([caller_type, callee_type]): | ||
| return | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -157,6 +157,43 @@ class B(A): pass | |
| B()# E: Cannot instantiate abstract class 'B' with abstract attributes 'f' and 'g' | ||
| [out] | ||
|
|
||
| [case testInstantiationAbstractsInType] | ||
| # flags: --python-version 3.6 | ||
|
Member
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 take it this is only because of the
Member
Author
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. Since recently |
||
| from typing import Type | ||
| from abc import abstractmethod | ||
|
|
||
| class A: | ||
| @abstractmethod | ||
| def m(self) -> None: pass | ||
| class B(A): pass | ||
| class C(B): | ||
| def m(self) -> None: | ||
| pass | ||
|
|
||
| def f(cls: Type[A]) -> A: | ||
| return cls() # OK | ||
|
|
||
| def g() -> A: | ||
| return A() # E: Cannot instantiate abstract class 'A' with abstract attribute 'm' | ||
|
|
||
| f(A) # E: Only non-abstract class can be given where 'Type[__main__.A]' is expected | ||
| f(B) # E: Only non-abstract class can be given where 'Type[__main__.A]' is expected | ||
| f(C) # OK | ||
|
|
||
| Alias = A | ||
| GoodAlias = C | ||
| Alias() # E: Cannot instantiate abstract class 'A' with abstract attribute 'm' | ||
| GoodAlias() | ||
| f(Alias) # E: Only non-abstract class can be given where 'Type[__main__.A]' is expected | ||
| f(GoodAlias) | ||
|
|
||
| var: Type[A] | ||
| var() | ||
| var = A # E: Cannot only assign non-abstract classes to a variable of type 'Type[__main__.A]' | ||
| var = B # E: Cannot only assign non-abstract classes to a variable of type 'Type[__main__.A]' | ||
| var = C # OK | ||
| [out] | ||
|
|
||
| [case testInstantiatingClassWithInheritedAbstractMethodAndSuppression] | ||
| from abc import abstractmethod, ABCMeta | ||
| import typing | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For what, exactly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a detailed comment here.