[Python-ideas] Lessons from typing hinting Whoosh (PEP484)

Guido van Rossum guido at python.org
Mon Nov 16 12:31:39 EST 2015


If you really want DocId to be as small as int you should add
`__slots__ = ()` to the class def. It's still a bit slower (some code
special-cases exactly int) and it also has some other imperfections --
e.g. DocId(2) + DocId(2) == int(4), not DocId(4). But that's probably
okay for a document ID (and the solution is way too messy to use).

But in general this is a pretty verbose option. If the main purpose is
for documentation maybe type aliases, only used on annotations and
other types are enough (`DocId = int`). But yeah, the type checker
won't track it. (That's a possible future feature though.)

On Mon, Nov 16, 2015 at 9:19 AM, Andrew Barnert via Python-ideas
<python-ideas at python.org> wrote:
> On Nov 15, 2015, at 22:17, Matt Chaput <matt at whoosh.ca> wrote:
>>
>> 2. It would be really nice if we could have "type aliasing" (or whatever it's called). I have a lot of types that are just something like "Tuple[int, int]", so type checking doesn't help much. It would be much more useful if I have a value that Python sees as (e.g.) an int, but have the type system track it as something more specific. Something like this:
>>
>>    DocId = typing.TypeAlias(int)
>>    DocLength = typing.TypeAlias(int)
>>
>>    def id_and_length() -> Tuple[DocId, Length]:
>>        docid = 5  # type: DocId
>>        length = 10  # type: Length
>>        return docid, length
>
> It sounds like you want a subtype that adds no new semantics or other runtime effects. Like this:
>
>     class DocId(int): pass
>     class DocLength(int): pass
>
>     def id_and_length() -> Tuple[DocId, DocLength]:
>         return DocId(5), DocLength(10)
>
> These will behave exactly like int objects, except that you can (dynamically or statically) type check them. It does mean that if someone uses "type(spam[0]) == int" it will fail, but I think if you care either way, you'd actually want it to fail. Meanwhile, "isinstance(spam[0], int)" or "spam[0] + eggs" or even using it in a function that requires something usable as a C long will work as expected. The object will also be the same size as an int in memory (although it will pickle a little bigger). It can't be optimized into a constant at compile time, but I doubt that's ever an issue. And it makes your intentions perfectly clear.
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/



-- 
--Guido van Rossum (python.org/~guido)


More information about the Python-ideas mailing list