Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

TypeVar single constraint not allowed, why?

246 views
Skip to first unread message

oliver

unread,
May 2, 2017, 11:45:39 AM5/2/17
to
The documentation for typing.TypeVar gives these two examples:

T = TypeVar('T') # Can be anything
A = TypeVar('A', str, bytes) # Must be str or bytes

I was suprised to find out that the following does not work, exception says
that TypeVar is not definable with only one constraint:

A = TypeVar('A', type) # Must be a type, like class Foo, etc (rather
than an instance of a type)

The TypeVar source code explicitely forbids only one type constraint, what
is the rationale behind this?

For those who wonder, my function parameter expects a type rather than an
instance of a type, and returns a type.

--
Oliver
My StackOverflow contributions
My CodeProject articles
My Github projects
My SourceForget.net projects

Ned Batchelder

unread,
May 3, 2017, 8:31:58 AM5/3/17
to
On Tuesday, May 2, 2017 at 11:45:39 AM UTC-4, oliver wrote:
> The documentation for typing.TypeVar gives these two examples:
>
> T = TypeVar('T') # Can be anything
> A = TypeVar('A', str, bytes) # Must be str or bytes
>
> I was suprised to find out that the following does not work, exception says
> that TypeVar is not definable with only one constraint:
>
> A = TypeVar('A', type) # Must be a type, like class Foo, etc (rather
> than an instance of a type)
>
> The TypeVar source code explicitely forbids only one type constraint, what
> is the rationale behind this?
>
> For those who wonder, my function parameter expects a type rather than an
> instance of a type, and returns a type.

Couldn't you simply annotate it as ": type", without using a TypeVar?

--Ned.

Gregory Ewing

unread,
May 3, 2017, 6:33:04 PM5/3/17
to
Ned Batchelder wrote:
> Couldn't you simply annotate it as ": type", without using a TypeVar?

Not if you want to constrain two types to be the same type.

--
Greg

oliver

unread,
May 4, 2017, 11:00:45 AM5/4/17
to
On Wed, 3 May 2017 at 18:36 Gregory Ewing <greg....@canterbury.ac.nz>
wrote:

> Ned Batchelder wrote:
> > Couldn't you simply annotate it as ": type", without using a TypeVar?
>
> Not if you want to constrain two types to be the same type.
>
>
Exactly! It would be a lot more expressive to write something like (with
Type assumed imported from typing module):

def func(a: type, b: type) -> Tuple[Type(a), Type(b)]:
...

This will not work because a and b are not globals. I could use strings but
this is a hack / language wart as far as I'm concerned (the language should
be extended so we don't have to resort to a lowly string). I suppose I
could do:

def func(a: type, b: type) -> Tuple[TypeVar['a'], TypeVar['b']]:
...

but this is not pythonic (not clean, simple), and too easy to have typo.
Same with forward declarations, where a class references itself in an
annotation: we should not have to resort to a string to express this, very
clunky and hacky.



> --
> Greg
> --
> https://mail.python.org/mailman/listinfo/python-list
0 new messages