Re: How to detect an undefined method?

2022-04-02 Thread anthony.flury via Python-list

On 27/03/2022 15:59, dn wrote:


What is code coverage?
In the simplest words, code coverage is a measure of exhaustiveness of a
test suite. 100% code coverage means that a system is fully tested.


Sorry, but that is a gross over-simplification.

100% coverage means that you have tested all of the branches in a given 
module, or a given class; it absolutely does not mean it is fully tested.


For example I can write code and unit-test cases for a trivial piece of 
code, and to achieve 100% coverage, but for the code to still fail on 
invalid input data, or for the code to fail due to exceptions from 
library code that my code doesn't handle.


To claim to be fully tested a piece of code has to be exercised against 
*every* possible input and *every* single possible event from *every* 
single source - that alone makes 100% testing impossible. If you think 
logically about it, you can only test a very small fraction of all 
possible test cases; the key is to pick those cases which represent a 
good range of possible inputs and events (including both valid and 
invalid data, exceptions, errors etc).


At best 100% coverage measure means that every branch through the code 
has been executed at least once by your test cases. It doesn't prove 
that your test cases are complete, or that your code takes into account 
all possible occurrences - 100% coverage doesn't mean it is fully tested.


In terms of coverage,  achieving 100% coverage is a laudable target, but 
it is often either unachievable or not worth the effort; aiming to 
achieve a high value (say > 80%) is sensible target.


If you achieve your high coverage count by doing black-box testing (ie. 
by testing to the specification of code and thinking what can right and 
what can go wrong), then the coverage is a more useful measure - if you 
write your unit-tests by looking at the code, then all that a high 
measurement means is that you are able (mostly) to read and understand 
your own code.


--
Anthony Flury
*Moble*: +44 07743 282707
*Home*: +44 (0)1206 391294
*email*: anthony.fl...@btinternet.com
--
https://mail.python.org/mailman/listinfo/python-list


Re: How to detect an undefined method?

2022-04-02 Thread dn
On 03/04/2022 02.28, anthony.flury wrote:
> On 27/03/2022 15:59, dn wrote:
> 
>> What is code coverage?
>> In the simplest words, code coverage is a measure of exhaustiveness of a
>> test suite. 100% code coverage means that a system is fully tested.
> 
> Sorry, but that is a gross over-simplification.


Please be careful how you trim messages/quote previous posts. Above
gives an incorrect impression and attribution!

The original text:
«
The following article includes both pros and cons of using coverage.py:

How to use code coverage in Python with pytest?
April 11, 2021 Sebastian python, testing software   
Basics
What is code coverage?
In the simplest words, code coverage is a measure of exhaustiveness of a
test suite. 100% code coverage means that a system is fully tested.
...
https://breadcrumbscollector.tech/how-to-use-code-coverage-in-python-with-pytest/
»

Readers will be able to come to their own conclusions - which may
very-well mirror your own.

Another consideration is the context of the OP and the problem he was
trying to solve.
-- 
Regards,
=dn
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to detect an undefined method?

2022-03-27 Thread Avi Gross via Python-list
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 
To: python-list@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() if
>> callable(getattr(, method))] if 'method_you_need' in methods:
>> . // 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

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to detect an undefined method?

2022-03-27 Thread Kirill Ratkin via Python-list
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() if
callable(getattr(, method))] if 'method_you_need' in methods:
. // 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


Re: How to detect an undefined method?

2022-03-27 Thread 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() if
> callable(getattr(, method))] if 'method_you_need' in methods:
> . // 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.

-- 
Manfred


> 27.03.2022 12:24, Manfred Lotz пишет:
>> Let's say I have a Python app and have used an undefined method somewhere. 
>> Let
>> us further assume I have not detected it thru my tests.
>>
>> Is there a way to detect it before deploying the app? pylint doesn't notice 
>> it.
>>
>>
>> Minimal example:
>>
>> #!/usr/bin/env python3
>>
>> import logging
>> from logging import Logger
>> from random import randrange
>>
>> def main():
>>  """
>>  Below logger.err gives
>>
>>  'Logger' object has no attribute 'err'
>>  """
>>
>>  logger = logging.getLogger('sample')
>>  logger.setLevel(logging.DEBUG)
>>  handler = logging.StreamHandler()
>>  logger.addHandler(handler)
>>
>>  num = randrange(0,1000)
>>  if num == 0:
>>  logger.err("got zero")
>>  else:
>>  logger.info(f'got a positive integer: {num}')
>>
>> if __name__ == "__main__":
>>  main()
>>
>>
>>

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to detect an undefined method?

2022-03-27 Thread Manfred Lotz
On 3/27/22 18:36, Skip Montanaro wrote:
>> Let's say I have a Python app and have used an undefined method somewhere.
>> Let
>> us further assume I have not detected it thru my tests.
>>
>> Is there a way to detect it before deploying the app? pylint doesn't
>> notice it.
>>
> 
> This is maybe not exactly what you're looking for, but writing a test to
> exercise that function call (if possible) would be top of my list. Next on
> my list would be to use coverage to get a good idea of what code is not
> tested. Writing more test cases to reduce the number of uncovered lines
> will also make it easier to manually examine the rest of your (untested) code
> to find calls to missing functions or methods.
> 

If the code base is large it might be difficult to have a test case for all
things. But I agree that coverage is surely a good thing to use.


-- 
Manfred

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to detect an undefined method?

2022-03-27 Thread Manfred Lotz
On 3/27/22 18:12, Peter Otten wrote:
> On 27/03/2022 11:24, Manfred Lotz wrote:
>> Let's say I have a Python app and have used an undefined method somewhere. 
>> Let
>> us further assume I have not detected it thru my tests.
>>
>> Is there a way to detect it before deploying the app? pylint doesn't notice 
>> it.
>>
>>
>> Minimal example:
>>
>> #!/usr/bin/env python3
>>
>> import logging
>> from logging import Logger
>> from random import randrange
>>
>> def main():
>>  """
>>  Below logger.err gives
>>
>>  'Logger' object has no attribute 'err'
>>  """
>>
>>  logger = logging.getLogger('sample')
>>  logger.setLevel(logging.DEBUG)
>>  handler = logging.StreamHandler()
>>  logger.addHandler(handler)
>>
>>  num = randrange(0,1000)
>>  if num == 0:
>>  logger.err("got zero")
>>  else:
>>  logger.info(f'got a positive integer: {num}')
>>
>> if __name__ == "__main__":
>>  main()
> 
> mypy --strict will find that one. Be warned though that this is a
> slippery slope: you may end up fixing quite a few non-errors...
> 

Great. I wasn't aware of --strict. Regarding 'slippery slope': I anyway would
only fix what I believe is an error.


Thanks.

-- 
Manfred
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to detect an undefined method?

2022-03-27 Thread Kirill Ratkin via Python-list

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() if 
callable(getattr(, method))] if 'method_you_need' in 
methods: . // BR |


27.03.2022 12:24, Manfred Lotz пишет:

Let's say I have a Python app and have used an undefined method somewhere. Let
us further assume I have not detected it thru my tests.

Is there a way to detect it before deploying the app? pylint doesn't notice it.


Minimal example:

#!/usr/bin/env python3

import logging
from logging import Logger
from random import randrange

def main():
 """
 Below logger.err gives

 'Logger' object has no attribute 'err'
 """

 logger = logging.getLogger('sample')
 logger.setLevel(logging.DEBUG)
 handler = logging.StreamHandler()
 logger.addHandler(handler)

 num = randrange(0,1000)
 if num == 0:
 logger.err("got zero")
 else:
 logger.info(f'got a positive integer: {num}')

if __name__ == "__main__":
 main()




--
https://mail.python.org/mailman/listinfo/python-list


Re: How to detect an undefined method?

2022-03-27 Thread Skip Montanaro
> Let's say I have a Python app and have used an undefined method somewhere.
> Let
> us further assume I have not detected it thru my tests.
>
> Is there a way to detect it before deploying the app? pylint doesn't
> notice it.
>

This is maybe not exactly what you're looking for, but writing a test to
exercise that function call (if possible) would be top of my list. Next on
my list would be to use coverage to get a good idea of what code is not
tested. Writing more test cases to reduce the number of uncovered lines
will also make it easier to manually examine the rest of your (untested) code
to find calls to missing functions or methods.

Skip

>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to detect an undefined method?

2022-03-27 Thread Peter Otten

On 27/03/2022 11:24, Manfred Lotz wrote:

Let's say I have a Python app and have used an undefined method somewhere. Let
us further assume I have not detected it thru my tests.

Is there a way to detect it before deploying the app? pylint doesn't notice it.


Minimal example:

#!/usr/bin/env python3

import logging
from logging import Logger
from random import randrange

def main():
 """
 Below logger.err gives

 'Logger' object has no attribute 'err'
 """

 logger = logging.getLogger('sample')
 logger.setLevel(logging.DEBUG)
 handler = logging.StreamHandler()
 logger.addHandler(handler)

 num = randrange(0,1000)
 if num == 0:
 logger.err("got zero")
 else:
 logger.info(f'got a positive integer: {num}')

if __name__ == "__main__":
 main()


mypy --strict will find that one. Be warned though that this is a
slippery slope: you may end up fixing quite a few non-errors...

--
https://mail.python.org/mailman/listinfo/python-list


Re: How to detect an undefined method?

2022-03-27 Thread dn
On 27/03/2022 22.24, Manfred Lotz wrote:
> Let's say I have a Python app and have used an undefined method somewhere. Let
> us further assume I have not detected it thru my tests.
> 
> Is there a way to detect it before deploying the app? pylint doesn't notice 
> it.


A competent IDE will 'find' such a fault, eg PyCharm indicates the usage
of an identifier for which it can't find a definition, by displaying a
wavy/colored line beneath it - but it won't complain or stop us from
running such code!


Similarly, the interpreter will not object to running the code, until
that line, that branch of logic, is actually executed!


The answer may be to improve the testing regime, to ensure that the
tests cover every line of logic in the code. Much of the time this is a
reasonable target. Sometimes it is too 'expensive'.

The following article includes both pros and cons of using coverage.py:

How to use code coverage in Python with pytest?
April 11, 2021 Sebastian python, testing software   
Basics
What is code coverage?
In the simplest words, code coverage is a measure of exhaustiveness of a
test suite. 100% code coverage means that a system is fully tested.
...
https://breadcrumbscollector.tech/how-to-use-code-coverage-in-python-with-pytest/
-- 
Regards,
=dn
-- 
https://mail.python.org/mailman/listinfo/python-list


How to detect an undefined method?

2022-03-27 Thread Manfred Lotz
Let's say I have a Python app and have used an undefined method somewhere. Let
us further assume I have not detected it thru my tests.

Is there a way to detect it before deploying the app? pylint doesn't notice it.


Minimal example:

#!/usr/bin/env python3

import logging
from logging import Logger
from random import randrange

def main():
"""
Below logger.err gives

'Logger' object has no attribute 'err'
"""

logger = logging.getLogger('sample')
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler()
logger.addHandler(handler)

num = randrange(0,1000)
if num == 0:
logger.err("got zero")
else:
logger.info(f'got a positive integer: {num}')

if __name__ == "__main__":
main()



-- 
Manfred
-- 
https://mail.python.org/mailman/listinfo/python-list