On 03/04/2020 06:24 AM, Steven D'Aprano wrote:

My earlier use-case still stands: feature detection where a function has
changed its parameter list. More on this below.

It seems like `inspect.signature` is the right way to do this kind of
feature detection.


On Wed, Mar 04, 2020 at 12:19:25PM +0200, Alex Hall wrote:
If you catch ParameterError, how do you know that it came directly from the
line you caught it from and not deeper within?

On 03/04/2020 06:24 AM, Steven D'Aprano wrote:
Does it matter?

If the call aardvark(*args, hovercraft=1) leaks a ParameterError from
deep inside its internals, then even if the feature is technically
available according to a version check, it is too buggy to use and I
need to use my wrapper.

This feels like a separate problem than "the function I'm calling changed
it's signature".


On Wed, Mar 04, 2020 at 12:19:25PM +0200, Alex Hall wrote:
If you want to check that you're passing parameters properly, I suggest
writing code like this:

import inspect

signature = inspect.signature(func)

And what if the function I'm checking doesn't support signature
metadata?

https://docs.python.org/3/library/inspect.html#inspect.signature

Looks like ValueError and TypeError can be raised, so it seems like the question
is how often?  If it only fails rarely or seldom then the case for more built-in
exceptions is weakened.


I can wrap the call to signature() in try...except, but what's my
fallback if I can't check the signature?

     py> inspect.signature(math.gcd)
     Traceback (most recent call last):
     [...]
     TypeError: 'feature_version' is an invalid keyword argument
     for compile()

In those cases you'll need to use the existing TypeError -- and with unit tests 
to
ensure things are working correctly on the different versions you're targeting
you'll know you're good to go.

By the way, `inspect.signature(math.gcd)` works for me on Python 3.8.2 -- which
version were you testing with?

--
~Ethan~
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/YUDDL4ZNFMZAEFX3IMY3FKUIBUF6E7TZ/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to