Skip to content

Types in typing not anymore instances of type or subclasses of "real" types #78749

Closed

Description

@pekkaklarck
BPO 34568
Nosy @gvanrossum, @ilevkivskyi, @JelleZijlstra

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2021-05-02.20:35:39.988>
created_at = <Date 2018-09-03.13:47:34.468>
labels = ['3.8', '3.7', 'library']
title = 'Types in `typing` not anymore instances of `type` or subclasses of "real" types'
updated_at = <Date 2021-05-02.20:35:39.987>
user = 'https://bugs.python.org/pekkaklarck'

bugs.python.org fields:

activity = <Date 2021-05-02.20:35:39.987>
actor = 'JelleZijlstra'
assignee = 'none'
closed = True
closed_date = <Date 2021-05-02.20:35:39.988>
closer = 'JelleZijlstra'
components = ['Library (Lib)']
creation = <Date 2018-09-03.13:47:34.468>
creator = 'pekka.klarck'
dependencies = []
files = []
hgrepos = []
issue_num = 34568
keywords = []
message_count = 9.0
messages = ['324518', '324520', '324606', '324634', '324642', '324647', '324680', '324684', '392728']
nosy_count = 4.0
nosy_names = ['gvanrossum', 'pekka.klarck', 'levkivskyi', 'JelleZijlstra']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue34568'
versions = ['Python 3.7', 'Python 3.8']

Activity

  1. pekkaklarck commented on Sep 3, 2018

    pekkaklarckmannequin
    MannequinAuthor

    = 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:
    robotframework/robotframework#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].

  2. pekkaklarck commented on Sep 3, 2018

    pekkaklarckmannequin
    MannequinAuthor

    Basically I'd like to get answers to these two questions:

    1. Are the changes deliberate and covered by the fact that typing is a provisional module, or could the old functionality be restored?

    2. If we cannot get the old functionality back, is there some other way to reliable detect is an annotation a type defined in the typing module? This should cover both the List and List[int] cases.

  3. added
    stdlib
    3.7 (EOL)
    3.8 (EOL)
    on Sep 3, 2018
  4. ilevkivskyi commented on Sep 4, 2018

    @ilevkivskyi
    Member

    It was a deliberate decision. You can find some motivation in PEP-560, and yes we used provisional status here. It was a hard decision, but we decided that giving up few percent of backwards compatibility is a reasonable price for up to 5x performance boost.

    It looks like some of your problems may be solved by https://github.com/ilevkivskyi/typing_inspect (use pip install typing_inspect) that aims at providing cross-version runtime introspection of typing objects by carefully wrapping some "hidden" internal API.

    There is a plan to include most used part of typing_inspect in typing itself, see for example python/typing#570.
    (it is hard to give an estimate about when, I really want to do this soon, but just don't have time).

  5. pekkaklarck commented on Sep 5, 2018

    pekkaklarckmannequin
    MannequinAuthor

    Thanks for the PEP-560 reference. It explains the reasoning for the underlying changes, performance, and also mentions backwards incompatibility problems, including issubclass(List[int], List) causing a TypeError. It doesn't mention that issubclass(List, list) also raises a TypeError, though, nor that isinstance(List, type) now returns False.

    I understand changing the implementation for performance reason, but I don't understand why that would require changing the behavior of isinstance and issubclass. The PEP explicitly mentions that the new types.resolved_base isn't called by them without explaining why. I guess that could be for performance reasons, but even then types in the typing could themselves implement __instancecheck__ and __subclasscheck__ and retain the old behavior. Or is there some actual reason for changing the behavior?

  6. pekkaklarck commented on Sep 5, 2018

    pekkaklarckmannequin
    MannequinAuthor

    While studying the types in the typing module more, I noticed they have a special __origin__ attribute which seems to contain the "real" type they represent. I was able to make my type conversion code to work by adding these lines:

        if hasattr(type_, '__origin__'):
            type_ = type_.__origin__

    All our tests pass with this simple fix, but I'm slightly worried using it because __origin__ doesn't seem to be documented. This means I'm not sure is my usage OK and, more importantly, makes me worried that another change in typing changes the behavior or removes the attribute altogether. Hopefully someone with more insight on this can comment my worries. Perhaps the attribute should also be documented as discussed earlier: python/typing#335

    I'd also be a little bit happier with the above fix if I could write it like

        if isinstance(type_, typing.SomeBaseType):
            type_ = type_.__origin__

    but apparently types in the typing module don't have any public base class. I guess odds that some unrelated class would have __origin__ defined is small enough that using hasattr(type_, '__origin__') is safe.

  7. pekkaklarck commented on Sep 5, 2018

    pekkaklarckmannequin
    MannequinAuthor

    My concerns with the behavior of __origin__ possibly changing in the future seem to valid. In Python 3.5 and 3.6 the behavior is

    List.__origin__ is None
    List[int].__origin__ is List
    

    while in Python 3.7

    List.__origin is list
    List[int].__origin__ is list
    

    Is it likely that this is going to change again or can I rely on the current behavior with Python 3.7+?

  8. ilevkivskyi commented on Sep 6, 2018

    @ilevkivskyi
    Member

    but even then types in the typing could themselves implement __instancecheck__ and __subclasscheck__ and retain the old behavior.

    It doesn't work that way. __instancecheck__ and __subclasscheck__ tweaks the behaviour of superclass (i.e. the right argument) in isinstance() and issubclass(). This is how isinstance([], typing.Iterable) works, you can't use the same to tweak isinstance(typing.Iterable, type).

    Hopefully someone with more insight on this can comment my worries. Perhaps the attribute should also be documented as discussed earlier: python/typing#335

    No, it is not safe to use it and will not be documented. You missed the point of my previous post, the idea is to add public wrappers in typing that will hide __origin__ (or whatever else) as an implementation detail. Using __origin__ is OK however as a temporary measure, if you don't want to use typing_inspect in the meantime.

  9. pekkaklarck commented on Sep 6, 2018

    pekkaklarckmannequin
    MannequinAuthor

    You are obviously right with how __instancecheck__ and __subclasscheck__ work. We'd either need something like __rinstancecheck__ and __rsubclasscheck__ or isinstance and issubclass needed to handle this using types.resolve_bases, __origin__, or something else.

    It's unfortunate that __origin__ cannot be considered to be part of the stable API and that no other suitable API exists. I don't want to add an external dependency only to handle this situation, so I guess I'll just use __origin__ with Python 3.7+. Hopefully it isn't changed in 3.7 minor versions and hopefully a public API exists in 3.8.

  10. JelleZijlstra commented on May 2, 2021

    @JelleZijlstra
    Member

    More recent versions of typing have added some helper functions that could be useful here, like typing.get_origin and typing.get_args. I'm going to close this issue because I don't think there's anything actionable.

  11. transferred this issue fromon Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.7 (EOL)3.8 (EOL)stdlib

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions