Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
gvanrossum merged 7 commits intopython:masterfromilevkivskyi:abstract-type-ok
Mar 27, 2017
Merged
Show file tree
Hide file tree
Changes from1 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
PrevPrevious commit
NextNext commit
Apply review comments
  • Loading branch information
@ilevkivskyi
ilevkivskyi committedMar 21, 2017
commit65eab95fdb766c43b94f466be31e50ed909d5cb2
5 changes: 3 additions & 2 deletionsmypy/checker.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1151,14 +1151,15 @@ def check_assignment(self, lvalue: Lvalue, rvalue: Expression, infer_lvalue_type
else:
rvalue_type = self.check_simple_assignment(lvalue_type, rvalue, lvalue)

# Special case
# Special case: only non-abstract classes can be assigned to variables
# with explicit type Type[A].
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
):
self.fail("Cannot only assign non-abstract classes"
self.fail("Can only assign non-abstract classes"
" to a variable of type '{}'".format(lvalue_type), rvalue)
return
if rvalue_type and infer_lvalue_type:
Expand Down
6 changes: 4 additions & 2 deletionsmypy/checkexpr.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -845,10 +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 classcould be given where Type[...] is expected
# Only non-abstract classcan be given where Type[...] is expected...
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):
isinstance(callee_type.item, Instance) and callee_type.item.type.is_abstract and
# ...except for classmethod first argument
not caller_type.is_classmethod_class):
messages.fail("Only non-abstract class can be given where '{}' is expected"
.format(callee_type), context)
elif not is_subtype(caller_type, callee_type):
Expand Down
63 changes: 58 additions & 5 deletionstest-data/unit/check-abstract.test
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -157,8 +157,7 @@ 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
[case testInstantiationAbstractsInTypeForFunctions]
from typing import Type
from abc import abstractmethod

Expand All@@ -172,26 +171,80 @@ class C(B):

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
x: Type[B]
f(x) # OK
[out]

[case testInstantiationAbstractsInTypeForAliases]
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

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)
[out]

[case testInstantiationAbstractsInTypeForVariables]
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

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 = A # E:Can only assign non-abstract classes to a variable of type 'Type[__main__.A]'
var = B # E:Can only assign non-abstract classes to a variable of type 'Type[__main__.A]'
var = C # OK

var_old = None # type: Type[A] # Old syntax for variable annotations
var_old()
var_old = A # E: Can only assign non-abstract classes to a variable of type 'Type[__main__.A]'
var_old = B # E: Can only assign non-abstract classes to a variable of type 'Type[__main__.A]'
var_old = C # OK
[out]

[case testInstantiationAbstractsInTypeForClassMethods]
from typing import Type
from abc import abstractmethod

class Logger:
@staticmethod
def log(a: Type[C]):
pass
class C:
@classmethod
def action(cls) -> None:
cls() #OK for classmethods
Logger.log(cls) #OK for classmethods
@abstractmethod
def m(self) -> None:
pass
[out]

[case testInstantiatingClassWithInheritedAbstractMethodAndSuppression]
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp