How to detect an undefined method?

Avi Gross avigross at verizon.net
Sun Mar 27 16:02:21 EDT 2022


The question seems to be how or whether you can check Python code in
advance for any instances of a method for an object being called that is
not instantiated. Right?

As Kirill points out, Python can be quite dynamic. I can think of oodles
of ways checking would not work well in a static examination of the code.

Just to mention a few, I can have a collection of objects (such as a list)
and I may iterate on it to take each object and call a specific method.
Some objects may have the method and others may not. The dynamic
code may easily include objects that conform to expectations or not.
For example, you ay be reading in saved objects from a file or objects
are being created as the user interacts. 

And is it an error if a program is written to detect and perhaps correct
errors? What if I want to display an object and in a "try" block I ask to
run one of several methods and on failure, try the next. Some objects
may have a method for full_profile() while others just might have a name()
and yet others might have no known way and will be shown as ANONYMOUS.

There also seem to be ways to extend an existing object or the entire class after 
it has been created. Objects with multiple inheritance also may be  headache.

So I suspect warnings for some cases make sense but a tool that catches everything,
maybe not easy or even possible.

You can, of course, make your code a tad more bulletproof, there are ways you can
have your code deliberately check if the object has that method before trying to
invoke it, or you can handle exceptions generated if it doesn't.

-----Original Message-----
From: Kirill Ratkin via Python-list <python-list at python.org>
To: python-list at python.org
Sent: Sun, Mar 27, 2022 2:29 pm
Subject: Re: How to detect an undefined method?


I just started to think from your example with method 'err' of logger 
object.
In this particular case you can check method 'err' exists or not before 
call this.


But if you mean general case ... . If for example I use some library 
which uses another library and someone just 'typo' there ...


Here is example from my code:

     calc: Calculator = Calculator()

     ...

     actions: Dict[str, Callable] = {
                    "s.sysinfo":      calc.get_system_info,
                    "s.setloglevel":  calc.set_log_level,
                    ...
                 }

     ...

     def do_action(action: str, params: Dict[str, str]) -> ActionResult:
         return await actions[action](params)


And if I make mistake and type 'calc.get_s*i*stem_info' instead 
'calc.get_s*y*stem_info. Error appears on rutime stage only.

And neither 'mypy --strict' or 'pyre' can find such error.


I guess there  is not warranty to detect such sitations in huge codebase.

It's python dynamic nature.


May be dynamic checkers can help in such situations ...



27.03.2022 20:07, Manfred Lotz пишет:
> On 3/27/22 18:57, Kirill Ratkin wrote:
>> Hi
>>
>> You can get all methods of your object and check the method you want to call is
>> there or not.
>>
>> |methods = [method for method in dir(<your_object>) if
>> callable(getattr(<your_object>, method))] if 'method_you_need' in methods:
>> <your_object>.<method_you_need> // BR |
>>
> I don't understand how this may help. Assume somebody has a codebase of 15T
> lines of Python code. How do you want to apply your method?
>
> But perhaps I overlook things.
>
-- 
https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list