New submission from Pekka Klärck <[email protected]>:
= 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]`.
----------
messages: 324518
nosy: pekka.klarck
priority: normal
severity: normal
status: open
title: Types in `typing` not anymore instances of `type` or subclasses of
"real" types
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue34568>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com