Add text about Type[C]. by gvanrossum · Pull Request #218 · python/typing (original) (raw)
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 andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Conversation13 Commits4 Checks0 Files changed
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
[ Show hidden characters]({{ revealButtonHref }})
This addresses #107 (but it doen't close it because the issue also
calls for an implementation in typing.py).
Guido van Rossum added 2 commits
This addresses #107 (but it doen't close it because the issue also calls for an implementation in typing.py).
@@ -861,6 +861,73 @@ allow all operations on it, and a value of type ``Any`` can be assigned |
---|
to a variable (or used as a return value) of a more constrained type. |
Meta-types |
---------- |
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think "Meta-types" is not a good name for this section for four reasons:
- This could add to the classes vs. types confusion: Unlike metaclasses that are quite different from classes, types do not have such hierarchy, since one should not type annotate types, one can only type annotate classes.
Type[C]
has normal semantics of type: This is just a set of objects that respondTrue
toissubclass(objc, C)
.- I think someone who is looking for a documentation of this feature will not look for "Meta-types", therefore I would propose to change this to a simpler title, like "Type annotating class objects".
- Finally, using such terminology could lead to proliferation of "meta": If we call annotations of values that are class objects by
Type[C]
or byABCMeta
meta-types, then how are we supposed to call annotations of values that are metaclasses (byType[ABCMeta]
for example)? Meta-meta-types? :-)
Miscellaneous things:
Type[T]
should likely be covariant, as discussed before.- Treatment of
__init__
signatures is going to be nontrivial, as discussed in the issue. Perhaps mention that may require a future PEP update? In particular, we discussed the variantType[[A, ...], C]
which would be likeType[C]
but with__init__
constrained to supporting positional argumentsA, ...
. This would be an alternative forCallable
that is also a type object. Is this potentially going to be included in phase 2?
Replaced section heading with "The type of class objects" and changed the opening sentence to match.
Drop "at runtime".
Replaced "is not allowed" with "is rejected by the type checker" (and then some).
Clarified that you can write Type[Union[...]] or Type[Any].
Mentioned that a future PEP revision may deal with constructor signatures.
Clarified behavior when Type[Any] is used.
Clarify that Type[T] is covariant (though I'm not sure I understand the concept well enough here to explain it well -- by contrast, if Type[T] were invariant, what would change?).
Sent another omnibus update; please review!
rejected by the type checker (in addition to failing at runtime |
---|
because you can't instantiate a union). |
Note that it *is* legal to use a union as the parameter for |
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I understand, you mean here "a union of classes", since in general Union
can accept Tuple
, Callable
etc. If yes, then maybe it is better to mention this?
It looks good to me. I have no further comments.
Looks good to me as well.