How to detect an undefined method?
Kirill Ratkin
kirill.ratkin at devoteam.com
Sun Mar 27 14:29:31 EDT 2022
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.
>
More information about the Python-list
mailing list