New submission from Dutcho <dut...@ziggo.nl>:
In Python 3.6, the argument to the register() method of a single-dispatch function (i.e. a function decorated by @functools.singledispatch) can be a 'type-like' class (pseudo-type?) from e.g. the typing module that supports isinstance(). The below demonstrates it works in Python 3.6: $ py -3.6 Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> from functools import singledispatch >>> from typing import Sequence >>> isinstance([1,2,3], Sequence) # 'pseudo-type' okay with isinstance True >>> @singledispatch ... def f(arg): ... print('unqualified', arg) ... >>> @f.register(Sequence) # 'pseudo-type' okay with register ... def _(arg): ... print('sequence', *arg) ... >>> f(1) unqualified 1 >>> f([1,2,3]) sequence 1 2 3 This code breaks in Python 3.7: $ py Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> from functools import singledispatch >>> from typing import Sequence >>> isinstance([1,2,3], Sequence) # 'pseudo-type' okay with isinstance True >>> @singledispatch ... def f(arg): ... print('unqualified', arg) ... >>> @f.register(Sequence) # 'pseudo-type' NOT okay with register ... def _(arg): ... print('sequence', *arg) ... Traceback (most recent call last): File "<stdin>", line 1, in <module> File "D:\Program Files\Python\Python37\lib\functools.py", line 801, in register f"Invalid first argument to `register()`: {cls!r}. " TypeError: Invalid first argument to `register()`: typing.Sequence. Use either `@register(some_class)` or plain `@register` on an annotated function. While agreeing a check on the register() arg IS required, the current check isinstance(cls, type) seems overly restrictive. This is especially true when considering the (welcome!) Python 3.7 use of annotations for single dispatch functions. ---------- components: Library (Lib) messages: 324061 nosy: Dutcho priority: normal severity: normal status: open title: Python 3.7 breaks on singledispatch_function.register(pseudo_type), which Python 3.6 accepted type: crash versions: Python 3.7 _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue34498> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com