This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author pekka.klarck
Recipients pekka.klarck
Date 2018-09-03.13:47:34
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1535982454.49.0.56676864532.issue34568@psf.upfronthosting.co.za>
In-reply-to
Content
= Introduction =

In Python 3.5 and 3.6 types defined in the typing module are instances of `type` and also subclasses of the "real" type they represent. For example, both `isinstance(typing.List, type)` and `issubclass(typing.List, list)` return true. In Python 3.7 the former returns false and the latter causes a TypeError. I could find anything related to these changes in the Python 3.7 release notes or from the documentation of the typing module.

I explain my use case and the problems these changes have caused below.

= Use case =

I'm implementing automatic argument conversion to Robot Framework, a generic open source test automation framework, based on function annotations. The idea is that if a user has defined a keyword like

    def example(arg: int):
        # ...

we can convert argument passed in plain text test data like

    Example    42

into the correct type automatically. For more details see this issue in our tracker:
https://github.com/robotframework/robotframework/issues/2890


= Problem 1 =

I have implemented converters for different types and use annotations to find out the expected type for each argument. To exclude non-type annotations, my code uses `isinstance(annotation, type)` but in Python 3.7 this excludes also types defined in the typing module.

I could apparently use `isinstance(annoation, (type, typing._GenericAlias))`, but touching private parts like is fragile and feels wrong in general.

= Problem 2 =

Each converter I've implemented is mapped to a certain type (e.g. `list`) and, when applicable, also to an abc (e.g. `collections.abc.MutableSequence`). When finding a correct converter for a certain type, the code uses an equivalent of `issubclass(type_, (converter.type, converter.abc))`. In Python 3.5 and 3.6 this works also if the used type is defined in the typing module but with Python 3.7 it causes a TypeError.

I guess I could handle the types in the typing module by explicitly mapping converters also to these types (e.g. `typing.List`) and then using something like `type_ is converter.typing`. The problem is that although it would work with types like `List`, it wouldn't work if types are used like `List[int]`.
History
Date User Action Args
2018-09-03 13:47:34pekka.klarcksetrecipients: + pekka.klarck
2018-09-03 13:47:34pekka.klarcksetmessageid: <1535982454.49.0.56676864532.issue34568@psf.upfronthosting.co.za>
2018-09-03 13:47:34pekka.klarcklinkissue34568 messages
2018-09-03 13:47:34pekka.klarckcreate