[Python-ideas] Add an option that allow check Type Hints in runtime
p.f.moore at gmail.com
Fri Jul 8 04:50:53 EDT 2016
On 8 July 2016 at 09:30, Chris Angelico <rosuav at gmail.com> wrote:
> On Fri, Jul 8, 2016 at 6:19 PM, Ma Lin <animalize81 at hotmail.com> wrote:
>> I mean real check by the interpreter, not an '__annotations__' attribute.
>> And I just had a look, mypy is a static checker, without actually running
>> the code.
>> My thought is modifying CALL_FUNCTION and RETURN_VALUE of the interpreter,
>> so that allow them to check in each function calling when option enalbed.
> Now all you have to do is write the type_valid function (takes an
> object and an annotation, returns True if it matches) and args_valid
> (takes the positional and keyword args, pairs them up against the
> function's params, and returns True if all(type_valid(...)) for the
> args). The only benefit of hacking the interpreter core for this is
> that you'd save the hassle of pairing up positional/keyword args
> against the parameters.
Just to clarify for the OP - the reason you're getting pushback on
making this a built in interpreter function is twofold - first, an
external facility could be used *now*, and would not need to be
limited to Python 3.6+, and second, adding checks to the function call
protocol is not free, and would therefore affect not just the minority
of people who want to add checks for debugging, but also all other
users (which include people for whom Python's performance is a key
issue). The Python interpreter's function calling process is known to
be slower than (some) people would like, and making it slower - even
marginally - isn't something we would want to do lightly.
With the ability to use external static analyzers like mypy, as well
as the possibility of writing custom solutions like Chris described
above, people wanting such checks have a number of options already,
and so the cost of adding the feature to the interpreter (and possibly
the language - do you see this as a CPython implementation detail, or
would you expect it to be a language-level facility, so other Python
implementations such as PyPy and Jython would be expected to have it
as well?) needs to be justified.
Personally, I'd view this feature as an interesting addition, but not
something I'd use myself, and so I'm inclined towards the "why should
I pay for something I won't use?" position.
More information about the Python-ideas