Skip to content

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

Merged
merged 7 commits into from
Mar 27, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix minor issues and lint
  • Loading branch information
ilevkivskyi committed Feb 12, 2017
commit ec22e4d3759335a36fdf4f1f8baf4bffb5b0924e
8 changes: 6 additions & 2 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,8 @@ def is_implicit_any(t: Type) -> bool:
self.fail("The erased type of self '{}' "
"is not a supertype of its class '{}'"
.format(erased, ref_type), 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
Expand Down Expand Up @@ -1128,10 +1130,12 @@ def check_assignment(self, lvalue: Lvalue, rvalue: Expression, infer_lvalue_type
rvalue_type = self.check_simple_assignment(lvalue_type, rvalue, lvalue)

# Special case
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For what, exactly?

Copy link
Member Author

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.

if (isinstance(rvalue_type, CallableType) and rvalue_type.is_type_obj() and
if (
isinstance(rvalue_type, CallableType) and rvalue_type.is_type_obj() and
Copy link
Member

Choose a reason for hiding this comment

The 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):
isinstance(lvalue_type.item, Instance) and lvalue_type.item.type.is_abstract
):
self.fail("Cannot only assign non-abstract classes"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Cannot only" -> "Cannot" (I presume).

Copy link
Member Author

Choose a reason for hiding this comment

The 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
Expand Down
13 changes: 7 additions & 6 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,8 @@ def check_call(self, callee: Type, args: List[Expression],
arg_messages = arg_messages or self.msg
if isinstance(callee, CallableType):
if (callee.is_type_obj() and callee.type_object().is_abstract
and not callee.from_type_type):
# Exceptions for Type[...] and classmethod first argument
and not callee.from_type_type and not callee.is_classmethod_class):
Copy link
Member

Choose a reason for hiding this comment

The 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.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code looks reasonable, I added a corresponding exception to check_arg.

type = callee.type_object()
self.msg.cannot_instantiate_abstract_class(
callee.type_object().name(), type.abstract_attributes,
Expand Down Expand Up @@ -845,11 +846,11 @@ def check_arg(self, caller_type: Type, original_caller_type: Type,
elif isinstance(caller_type, DeletedType):
messages.deleted_as_rvalue(caller_type, context)
# Only non-abstract class could be given where Type[...] is expected
Copy link
Member

Choose a reason for hiding this comment

The 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):
if (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 (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
Expand Down
1 change: 0 additions & 1 deletion mypy/checkmember.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,6 @@ def class_callable(init_type: CallableType, info: TypeInfo, type_type: Instance,
ret_type=fill_typevars(info), fallback=type_type, name=None, variables=variables,
special_sig=special_sig)
c = callable_type.with_name('"{}"'.format(info.name()))
c.is_classmethod_class = True
return c


Expand Down
2 changes: 2 additions & 0 deletions test-data/unit/check-abstract.test
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ 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()
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy