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

Revamped generic class behavior to conform to updated PEP 484#195

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 9 commits intomasterfromnew_generic
Apr 4, 2016

Conversation

@gvanrossum
Copy link
Member

Fixes#115

1st reacted with thumbs up emoji
@ilevkivskyi
Copy link
Member

It looks great! Still I have found something that I think could be confusing.

First, I would expect thatIterable[T][int] andIterable[int] is the same type, butIterable[T][int] == Iterable[int] gives meFalse.

Second (maybe related) issue is that now__repr__ shows the whole "history" of a type. For example,Iterable[Tuple[T,T]][int] is printed as__main__.Iterable<+T_co>[__main__.Tuple[~T, ~T]]<~T>[int]. This reads as first there was anIterable parameterized byT_co, then it was indexed byTuple[T, T] and we had something parameterized byT, and then it was indexed byint. The question is whether we need to expose all this information (that is rather internal) to the user? I think it would be more clear if__repr__ in this case will show something likeIterable[Tuple[int,int]].

@gvanrossum
Copy link
MemberAuthor

Ah, yes. I thought of makingIterable[T][int] == Iterable[int], but I ran out of steam on how to implement it. Then again, thinking about it I'm not sure that it's important to do this -- the runtime behavior of these things is of marginal interest and mypy doesn't let you write that anyway (the PEP is silent on whether this is allowed).

Ditto for repr(). The one important thing about repr() is that when you have

class C(Generic[T]): ...

then repr(C) showsC<~T> rather thanC[~T] as it used to. I think the new form is better. And thenC[Tuple[int, int]] will actually be printed asC<~T>[Tuple[int, int]]. The longer form (with alternating<...> and[...]) is only of theoretical importance.

Anyway, thanks for the encouragement. I hope others look over this important change too! I intend to land this in Python 3.5.2 (whenever it is released).

@ilevkivskyi
Copy link
Member

I am now thinking about one additional thing that might be not completely clear: covariance. If I initially haveIterable[T_co] covariant in its type parameter and write

classC(Iterable[Tuple[T,S]]):    ...

then one could be not sure whetherC is covariant in its variables or invariant. I think the natural answer is that covariance is determined only by the final list of type parameters, so that the class above is invariant in both variables. However, I didn't find anywhere in the PEP any explicit explanation of this point. Maybe it is obvious and it is only my problem of understanding, but if not then maybe one should add a short explanation to the PEP?

@gvanrossum
Copy link
MemberAuthor

gvanrossum commentedApr 3, 2016 via email

Good point. IMO nothing having to do with variance is obvious, so it wouldbe good to spell it out. Right now I'm too tired to reason this throughwith a few examples, but my intuition says that your "natural" answer isprobably correct. When I feel more rested I'll figure this out and add itto the PEP.

@ilevkivskyi
Copy link
Member

I just noticed that this PR also fixes#129 . In general, I think this new "revamped" behavior is much better than the old one.

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

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

3 participants

@gvanrossum@ilevkivskyi

[8]ページ先頭

©2009-2025 Movatter.jp