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

Conversation

@ilevkivskyi
Copy link
Member

@ilevkivskyiilevkivskyi commentedFeb 12, 2017
edited
Loading

Fixes#1843 (It was also necessary to fix few minor things to make this work correctly)

The rules are simple, assuming we have:

classA:@abstractmethoddefm(self)->None:passclassC(A):defm(self)->None:        ...

then

deffun(cls:Type[A]):cls()# OKfun(A)# Errorfun(C)# OK

The same applies to variables:

var:Type[A]var()# OKvar=A# Errorvar=C# OK

Also there is an option for people who want to pass abstract classes around: type aliases, they work as before. For non-abstractA,Type[A] also works as before.


@gvanrossum You could be interested in this.
My intuition why you opened#1843 is when someone writes annotationType[A] with an abstractA, then most probably one wants a class object thatimplements a certain protocol, not just inherits fromA.

EDIT: as discussed inpython/peps#224 this behaviour is good for both protocols and usual ABCs.

@elazarg
Copy link
Contributor

#2430 depends on this PR (although the test should be removed there)

Copy link
Member

@gvanrossumgvanrossum left a comment

Choose a reason for hiding this comment

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

Thanks! I finally got to looking at this. I like the general idea.

[out]

[case testInstantiationAbstractsInType]
# flags: --python-version 3.6
Copy link
Member

Choose a reason for hiding this comment

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

I take it this is only because of thevar: Type[A] below? If this behavior is supposed to work for all Python versions maybe you can rewrite that part of the test using a function? (Also it's a rather long test; maybe it would be better split up.)

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Since recently--python-version flag is not needed. Also I split the test into three smaller and added a test case for@classmethod.

else:
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
MemberAuthor

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.


# Special case
if (
isinstance(rvalue_type,CallableType)andrvalue_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.

isinstance(lvalue_type,TypeType)and
isinstance(lvalue_type.item,Instance)andlvalue_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
MemberAuthor

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

ifcallee.is_concrete_type_obj()andcallee.type_object().is_abstract:
if (callee.is_type_obj()andcallee.type_object().is_abstract
# Exceptions for Type[...] and classmethod first argument
andnotcallee.from_type_typeandnotcallee.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:

classLogger:@staticmethoddeflog(a:Type[C]): ...classC:@classmethoddefaction(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
MemberAuthor

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 tocheck_arg.

messages.does_not_return_value(caller_type,context)
elifisinstance(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 :-)

@ilevkivskyi
Copy link
MemberAuthor

@gvanrossum
Thank you very much for review!
I applied your comments in new commits.

@gvanrossum
Copy link
Member

LGTM. We're holding off merging until we've sorted out some issues with recent mypy and typeshed commits.

@ilevkivskyi
Copy link
MemberAuthor

@gvanrossum Could this be merged soon?

Sorry for bothering you again, this will be helpful to avoid merge conflicts with my work on protocols.

@gvanrossumgvanrossum merged commit72967fd intopython:masterMar 27, 2017
@gvanrossum
Copy link
Member

NP. We're finally at a point where we can merge PRs with confidence again.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@gvanrossumgvanrossumgvanrossum approved these changes

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

class object typed with Type[SomeAbstractClass] should not complain about instantiation

3 participants

@ilevkivskyi@elazarg@gvanrossum

[8]ページ先頭

©2009-2025 Movatter.jp