[libcamera-devel,06/14] py: unittests: make typechecker happy
diff mbox series

Message ID 20220516141022.96327-7-tomi.valkeinen@ideasonboard.com
State Superseded
Headers show
Series
  • Misc Python bindings patches
Related show

Commit Message

Tomi Valkeinen May 16, 2022, 2:10 p.m. UTC
Add some annotations to reduce the typechecker warnings.

Signed-off-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
---
 test/py/unittests.py | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

Comments

Laurent Pinchart May 17, 2022, 8:19 a.m. UTC | #1
Hi Tomi,

Thank you for the patch.

On Mon, May 16, 2022 at 05:10:14PM +0300, Tomi Valkeinen wrote:
> Add some annotations to reduce the typechecker warnings.
> 
> Signed-off-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
> ---
>  test/py/unittests.py | 17 +++++++++++------
>  1 file changed, 11 insertions(+), 6 deletions(-)
> 
> diff --git a/test/py/unittests.py b/test/py/unittests.py
> index 288dcd48..e5591f3f 100755
> --- a/test/py/unittests.py
> +++ b/test/py/unittests.py
> @@ -10,6 +10,7 @@ import libcamera as libcam
>  import os
>  import selectors
>  import time
> +import typing
>  import unittest
>  import weakref
>  
> @@ -70,6 +71,9 @@ class SimpleTestMethods(BaseTestCase):
>  
>  
>  class CameraTesterBase(BaseTestCase):
> +    cm: typing.Any
> +    cam: typing.Any
> +
>      def setUp(self):
>          self.cm = libcam.CameraManager.singleton()
>          self.cam = next((cam for cam in self.cm.cameras if 'platform/vimc' in cam.id), None)
> @@ -131,8 +135,8 @@ class AllocatorTestMethods(CameraTesterBase):
>          self.assertTrue(ret > 0)
>          wr_allocator = weakref.ref(allocator)
>  
> -        buffers = allocator.buffers(stream)
> -        buffers = None
> +        buffers = allocator.buffers(stream)     # type: ignore
> +        buffers = None                          # type: ignore
>  
>          buffer = allocator.buffers(stream)[0]
>          self.assertIsNotNone(buffer)
> @@ -166,7 +170,8 @@ class SimpleCaptureMethods(CameraTesterBase):
>          self.assertTrue(camconfig.size == 1)
>  
>          streamconfig = camconfig.at(0)
> -        fmts = streamconfig.formats
> +        fmts = streamconfig.formats     # type: ignore
> +        fmts = None                     # type: ignore

Was this intended ? Actually, fmts doesn't seem to be used at all.

>  
>          ret = cam.configure(camconfig)
>          self.assertZero(ret)
> @@ -225,7 +230,7 @@ class SimpleCaptureMethods(CameraTesterBase):
>          self.assertTrue(camconfig.size == 1)
>  
>          streamconfig = camconfig.at(0)
> -        fmts = streamconfig.formats
> +        fmts = streamconfig.formats     # type: ignore
>  
>          ret = cam.configure(camconfig)
>          self.assertZero(ret)
> @@ -349,9 +354,9 @@ if __name__ == '__main__':
>          gc.unfreeze()
>          gc.collect()
>  
> -        obs_after = get_all_objects([obs_before])
> +        obs_after = get_all_objects([obs_before])   # type: ignore
>  
> -        before = create_type_count_map(obs_before)
> +        before = create_type_count_map(obs_before)  # type: ignore
>          after = create_type_count_map(obs_after)
>  
>          leaks = diff_type_count_maps(before, after)
Tomi Valkeinen May 17, 2022, 8:21 a.m. UTC | #2
On 17/05/2022 11:19, Laurent Pinchart wrote:
> Hi Tomi,
> 
> Thank you for the patch.
> 
> On Mon, May 16, 2022 at 05:10:14PM +0300, Tomi Valkeinen wrote:
>> Add some annotations to reduce the typechecker warnings.
>>
>> Signed-off-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
>> ---
>>   test/py/unittests.py | 17 +++++++++++------
>>   1 file changed, 11 insertions(+), 6 deletions(-)
>>
>> diff --git a/test/py/unittests.py b/test/py/unittests.py
>> index 288dcd48..e5591f3f 100755
>> --- a/test/py/unittests.py
>> +++ b/test/py/unittests.py
>> @@ -10,6 +10,7 @@ import libcamera as libcam
>>   import os
>>   import selectors
>>   import time
>> +import typing
>>   import unittest
>>   import weakref
>>   
>> @@ -70,6 +71,9 @@ class SimpleTestMethods(BaseTestCase):
>>   
>>   
>>   class CameraTesterBase(BaseTestCase):
>> +    cm: typing.Any
>> +    cam: typing.Any
>> +
>>       def setUp(self):
>>           self.cm = libcam.CameraManager.singleton()
>>           self.cam = next((cam for cam in self.cm.cameras if 'platform/vimc' in cam.id), None)
>> @@ -131,8 +135,8 @@ class AllocatorTestMethods(CameraTesterBase):
>>           self.assertTrue(ret > 0)
>>           wr_allocator = weakref.ref(allocator)
>>   
>> -        buffers = allocator.buffers(stream)
>> -        buffers = None
>> +        buffers = allocator.buffers(stream)     # type: ignore
>> +        buffers = None                          # type: ignore
>>   
>>           buffer = allocator.buffers(stream)[0]
>>           self.assertIsNotNone(buffer)
>> @@ -166,7 +170,8 @@ class SimpleCaptureMethods(CameraTesterBase):
>>           self.assertTrue(camconfig.size == 1)
>>   
>>           streamconfig = camconfig.at(0)
>> -        fmts = streamconfig.formats
>> +        fmts = streamconfig.formats     # type: ignore
>> +        fmts = None                     # type: ignore
> 
> Was this intended ? Actually, fmts doesn't seem to be used at all.

Yes. It's a test. I want to get formats and see that it gets released 
(or rather, the whole object chain gets released in the end, and we 
observe that camera & cam manager gets released).

  Tomi
Tomi Valkeinen May 17, 2022, 8:25 a.m. UTC | #3
On 17/05/2022 11:21, Tomi Valkeinen wrote:
> On 17/05/2022 11:19, Laurent Pinchart wrote:
>> Hi Tomi,
>>
>> Thank you for the patch.
>>
>> On Mon, May 16, 2022 at 05:10:14PM +0300, Tomi Valkeinen wrote:
>>> Add some annotations to reduce the typechecker warnings.
>>>
>>> Signed-off-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
>>> ---
>>>   test/py/unittests.py | 17 +++++++++++------
>>>   1 file changed, 11 insertions(+), 6 deletions(-)
>>>
>>> diff --git a/test/py/unittests.py b/test/py/unittests.py
>>> index 288dcd48..e5591f3f 100755
>>> --- a/test/py/unittests.py
>>> +++ b/test/py/unittests.py
>>> @@ -10,6 +10,7 @@ import libcamera as libcam
>>>   import os
>>>   import selectors
>>>   import time
>>> +import typing
>>>   import unittest
>>>   import weakref
>>> @@ -70,6 +71,9 @@ class SimpleTestMethods(BaseTestCase):
>>>   class CameraTesterBase(BaseTestCase):
>>> +    cm: typing.Any
>>> +    cam: typing.Any
>>> +
>>>       def setUp(self):
>>>           self.cm = libcam.CameraManager.singleton()
>>>           self.cam = next((cam for cam in self.cm.cameras if 
>>> 'platform/vimc' in cam.id), None)
>>> @@ -131,8 +135,8 @@ class AllocatorTestMethods(CameraTesterBase):
>>>           self.assertTrue(ret > 0)
>>>           wr_allocator = weakref.ref(allocator)
>>> -        buffers = allocator.buffers(stream)
>>> -        buffers = None
>>> +        buffers = allocator.buffers(stream)     # type: ignore
>>> +        buffers = None                          # type: ignore
>>>           buffer = allocator.buffers(stream)[0]
>>>           self.assertIsNotNone(buffer)
>>> @@ -166,7 +170,8 @@ class SimpleCaptureMethods(CameraTesterBase):
>>>           self.assertTrue(camconfig.size == 1)
>>>           streamconfig = camconfig.at(0)
>>> -        fmts = streamconfig.formats
>>> +        fmts = streamconfig.formats     # type: ignore
>>> +        fmts = None                     # type: ignore
>>
>> Was this intended ? Actually, fmts doesn't seem to be used at all.
> 
> Yes. It's a test. I want to get formats and see that it gets released 
> (or rather, the whole object chain gets released in the end, and we 
> observe that camera & cam manager gets released).

Actually, I should just assert that fmts != None. That both adds another 
test check, and should get rid of the typechecker warnings.

  Tomi

Patch
diff mbox series

diff --git a/test/py/unittests.py b/test/py/unittests.py
index 288dcd48..e5591f3f 100755
--- a/test/py/unittests.py
+++ b/test/py/unittests.py
@@ -10,6 +10,7 @@  import libcamera as libcam
 import os
 import selectors
 import time
+import typing
 import unittest
 import weakref
 
@@ -70,6 +71,9 @@  class SimpleTestMethods(BaseTestCase):
 
 
 class CameraTesterBase(BaseTestCase):
+    cm: typing.Any
+    cam: typing.Any
+
     def setUp(self):
         self.cm = libcam.CameraManager.singleton()
         self.cam = next((cam for cam in self.cm.cameras if 'platform/vimc' in cam.id), None)
@@ -131,8 +135,8 @@  class AllocatorTestMethods(CameraTesterBase):
         self.assertTrue(ret > 0)
         wr_allocator = weakref.ref(allocator)
 
-        buffers = allocator.buffers(stream)
-        buffers = None
+        buffers = allocator.buffers(stream)     # type: ignore
+        buffers = None                          # type: ignore
 
         buffer = allocator.buffers(stream)[0]
         self.assertIsNotNone(buffer)
@@ -166,7 +170,8 @@  class SimpleCaptureMethods(CameraTesterBase):
         self.assertTrue(camconfig.size == 1)
 
         streamconfig = camconfig.at(0)
-        fmts = streamconfig.formats
+        fmts = streamconfig.formats     # type: ignore
+        fmts = None                     # type: ignore
 
         ret = cam.configure(camconfig)
         self.assertZero(ret)
@@ -225,7 +230,7 @@  class SimpleCaptureMethods(CameraTesterBase):
         self.assertTrue(camconfig.size == 1)
 
         streamconfig = camconfig.at(0)
-        fmts = streamconfig.formats
+        fmts = streamconfig.formats     # type: ignore
 
         ret = cam.configure(camconfig)
         self.assertZero(ret)
@@ -349,9 +354,9 @@  if __name__ == '__main__':
         gc.unfreeze()
         gc.collect()
 
-        obs_after = get_all_objects([obs_before])
+        obs_after = get_all_objects([obs_before])   # type: ignore
 
-        before = create_type_count_map(obs_before)
+        before = create_type_count_map(obs_before)  # type: ignore
         after = create_type_count_map(obs_after)
 
         leaks = diff_type_count_maps(before, after)