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 GBeauregard
Recipients AlexWaygood, GBeauregard, JelleZijlstra, gvanrossum, kj, levkivskyi, sobolevn
Date 2022-02-04.23:02:57
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1644015777.96.0.875814750019.issue46644@roundup.psfhosted.org>
In-reply-to
Content
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
History
Date User Action Args
2022-02-04 23:02:58GBeauregardsetrecipients: + GBeauregard, gvanrossum, levkivskyi, JelleZijlstra, sobolevn, kj, AlexWaygood
2022-02-04 23:02:57GBeauregardsetmessageid: <1644015777.96.0.875814750019.issue46644@roundup.psfhosted.org>
2022-02-04 23:02:57GBeauregardlinkissue46644 messages
2022-02-04 23:02:57GBeauregardcreate