TypeVar single constraint not allowed, why?

oliver oliver.schoenborn at gmail.com
Tue May 2 11:43:46 EDT 2017


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



More information about the Python-list mailing list