[Python-ideas] PEP 484 (Type Hints) -- first draft round
Guido van Rossum
guido at python.org
Fri Jan 16 22:30:49 CET 2015
On Fri, Jan 16, 2015 at 12:05 PM, Philipp A. <flying-sheep at web.de> wrote:
> hi, nice work!
> I like most of the ideas, except the semantic comments and some specific
> abstract classes.
> so criticism first:
> 1. i don’t think comments should ever have an impact on the code.
They don't. They also change the errors emitted by the static checker
(which is not invoked when you run the code -- it is like a linter).
> i’d prefer “assert isinstance(...)” for type annotations on variables.
But that *would* affect runtime, by slowing down execution. Also for
generic types the type checking could be either very slow (if checking
List[int] would check every item of a list) or always fail (if a type
variable is involved).
See discussion at https://github.com/ambv/typehinting/issues/35
> 2. also i don’t see why Dict, (non-abstract) Set, and List exist as long
> as we have Mapping and so on. Tuple is different because of its “Multiple
> return value” status and status as “is treated as Union[Class1, Class2]
> when used with instanceof(), except and friends”.
> but checking for instanceof(..., list) is unpytonic! imho List and Dict
> really go against python’s data model and protocol-based nature.
> i’d be for consistency: Set and MutableSet, Sequence and MutableSequence,
> Mapping and MutableMapping. no “abstract” (because all of this is
> abstract), no “frozen” (because frozen sets are just immutable sets and
> immutable behavior is a subset of the corresponding mutable behavior).
But there is a lot of code that really needs the concrete types. E.g.
list+list returns a list, but sequence+sequence may fail (as list+tuple
exemplifies). Or list.sort. Etc.
> 1. will we be able to use Union[...] in places like an except clause,
> where tuples are used to mean the same right now? would make sense imho
No. Remember TOOWTDI. (Also the PEP intentionally stays far away from
> 2. what are ItemsView and KeysView for? what makes them different from
> FrozenSet or Sequence?
They exist in collections.abc. Note that KeysView and ItemsView are
(abstract) Sets, but ValuesView is not (you can have multiple equal values).
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas