New submission from Gregory Beauregard <[email protected]>:
I propose removing the callable() check[1] from typing._type_check. This
restriction is usually met in typeform instances by implementing a __call__
method that raises at runtime[2]. _type_check is called at runtime in a few
disparate locations, such as in an argument to typing.Annotated or for certain
stringified annotations in typing.get_type_hints.
Because the requirement to be callable is unexpected and shows up in situations
not easily discoverable during development or common typing usage, it is the
cause of several existing cpython bugs and will likely continue to be the cause
of bugs in typeforms outside of cpython. Known cpython bugs caused by the
callable() check are bpo-46643, bpo-44799, a substantial contributing factor to
bpo-46642, and partly bpo-46511. I discovered bpo-46643 with only a cursory
check of typing.py while writing this proposal. Moreover, it doesn't make any
particular technical sense to me why it should be required to add an awkward
__call__ method.
Removing the callable() check fails 10 tests:
7 tests: checking that an int literal is not a type
2 tests: testing that list literals are not valid types (e.g. [3] raises a
TypeError because the literal [('name', str), ('id', int)] doesn't pass
callable())
1 test: bpo-46642
The responsibility of determining these invalid typeforms (e.g. int literals)
would need to be passed to a static type checker. If it's desired to do this at
runtime it's my opinion that a different check would be more appropriate.
Have I missed any reasons for the callable() check? Can I remove the check and
adjust or remove the tests?
[1]
https://github.com/python/cpython/blob/bf95ff91f2c1fc5a57190491f9ccdc63458b089e/Lib/typing.py#L183-L184
[2]
https://github.com/python/cpython/blob/bf95ff91f2c1fc5a57190491f9ccdc63458b089e/Lib/typing.py#L392-L393
[3]
https://github.com/python/cpython/blob/bf95ff91f2c1fc5a57190491f9ccdc63458b089e/Lib/test/test_typing.py#L4262-L4263
----------
components: Library (Lib)
messages: 412548
nosy: AlexWaygood, GBeauregard, Jelle Zijlstra, gvanrossum, kj, levkivskyi,
sobolevn
priority: normal
severity: normal
status: open
title: typing: remove callable() check from typing._type_check
type: enhancement
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue46644>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com