[PATCH RFC 13/32] python/qemu/lib: Adjust traceback typing

John Snow posted 32 patches 5 years, 6 months ago
Maintainers: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>, "Philippe Mathieu-Daudé" <philmd@redhat.com>, "Alex Bennée" <alex.bennee@linaro.org>, Fam Zheng <fam@euphon.net>, Eduardo Habkost <ehabkost@redhat.com>, Kevin Wolf <kwolf@redhat.com>, Markus Armbruster <armbru@redhat.com>, Max Reitz <mreitz@redhat.com>, Cleber Rosa <crosa@redhat.com>
[PATCH RFC 13/32] python/qemu/lib: Adjust traceback typing
Posted by John Snow 5 years, 6 months ago
mypy considers it incorrect to use `bool` to statically return false,
because it will assume that it could conceivably return True, and gives
different analysis in that case. Use a None return to achieve the same
effect, but make mypy happy.

Note: Pylint considers function signatures as code that might trip the
duplicate-code checker. I'd rather not disable this as it does not
trigger often in practice, so I'm disabling it as a one-off and filed a
change request; see https://github.com/PyCQA/pylint/issues/3619

Signed-off-by: John Snow <jsnow@redhat.com>
---
 python/qemu/lib/machine.py |  8 ++++++--
 python/qemu/lib/qmp.py     | 10 ++++++++--
 2 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/python/qemu/lib/machine.py b/python/qemu/lib/machine.py
index b2f0412197..2f94c851ed 100644
--- a/python/qemu/lib/machine.py
+++ b/python/qemu/lib/machine.py
@@ -24,6 +24,8 @@
 import shutil
 import socket
 import tempfile
+from typing import Optional, Type
+from types import TracebackType
 
 from . import qmp
 
@@ -127,9 +129,11 @@ def __init__(self, binary, args=None, wrapper=None, name=None,
     def __enter__(self):
         return self
 
-    def __exit__(self, exc_type, exc_val, exc_tb):
+    def __exit__(self,
+                 exc_type: Optional[Type[BaseException]],
+                 exc_val: Optional[BaseException],
+                 exc_tb: Optional[TracebackType]) -> None:
         self.shutdown()
-        return False
 
     def add_monitor_null(self):
         """
diff --git a/python/qemu/lib/qmp.py b/python/qemu/lib/qmp.py
index 73d49050ed..b91c9d5c1c 100644
--- a/python/qemu/lib/qmp.py
+++ b/python/qemu/lib/qmp.py
@@ -14,7 +14,9 @@
 from typing import (
     Optional,
     TextIO,
+    Type,
 )
+from types import TracebackType
 
 
 class QMPError(Exception):
@@ -146,10 +148,14 @@ def __enter__(self):
         # Implement context manager enter function.
         return self
 
-    def __exit__(self, exc_type, exc_value, exc_traceback):
+    def __exit__(self,
+                 # pylint: disable=duplicate-code
+                 # see https://github.com/PyCQA/pylint/issues/3619
+                 exc_type: Optional[Type[BaseException]],
+                 exc_val: Optional[BaseException],
+                 exc_tb: Optional[TracebackType]) -> None:
         # Implement context manager exit function.
         self.close()
-        return False
 
     def connect(self, negotiate=True):
         """
-- 
2.21.1


Re: [PATCH RFC 13/32] python/qemu/lib: Adjust traceback typing
Posted by Philippe Mathieu-Daudé 5 years, 5 months ago
On 5/14/20 7:53 AM, John Snow wrote:
> mypy considers it incorrect to use `bool` to statically return false,
> because it will assume that it could conceivably return True, and gives
> different analysis in that case. Use a None return to achieve the same
> effect, but make mypy happy.
> 
> Note: Pylint considers function signatures as code that might trip the
> duplicate-code checker. I'd rather not disable this as it does not
> trigger often in practice, so I'm disabling it as a one-off and filed a
> change request; see https://github.com/PyCQA/pylint/issues/3619
> 
> Signed-off-by: John Snow <jsnow@redhat.com>
> ---
>  python/qemu/lib/machine.py |  8 ++++++--
>  python/qemu/lib/qmp.py     | 10 ++++++++--
>  2 files changed, 14 insertions(+), 4 deletions(-)
> 
> diff --git a/python/qemu/lib/machine.py b/python/qemu/lib/machine.py
> index b2f0412197..2f94c851ed 100644
> --- a/python/qemu/lib/machine.py
> +++ b/python/qemu/lib/machine.py
> @@ -24,6 +24,8 @@
>  import shutil
>  import socket
>  import tempfile
> +from typing import Optional, Type
> +from types import TracebackType
>  
>  from . import qmp
>  
> @@ -127,9 +129,11 @@ def __init__(self, binary, args=None, wrapper=None, name=None,
>      def __enter__(self):
>          return self
>  
> -    def __exit__(self, exc_type, exc_val, exc_tb):
> +    def __exit__(self,
> +                 exc_type: Optional[Type[BaseException]],
> +                 exc_val: Optional[BaseException],
> +                 exc_tb: Optional[TracebackType]) -> None:
>          self.shutdown()
> -        return False
>  
>      def add_monitor_null(self):
>          """
> diff --git a/python/qemu/lib/qmp.py b/python/qemu/lib/qmp.py
> index 73d49050ed..b91c9d5c1c 100644
> --- a/python/qemu/lib/qmp.py
> +++ b/python/qemu/lib/qmp.py
> @@ -14,7 +14,9 @@
>  from typing import (
>      Optional,
>      TextIO,
> +    Type,
>  )
> +from types import TracebackType
>  
>  
>  class QMPError(Exception):
> @@ -146,10 +148,14 @@ def __enter__(self):
>          # Implement context manager enter function.
>          return self
>  
> -    def __exit__(self, exc_type, exc_value, exc_traceback):
> +    def __exit__(self,
> +                 # pylint: disable=duplicate-code
> +                 # see https://github.com/PyCQA/pylint/issues/3619
> +                 exc_type: Optional[Type[BaseException]],
> +                 exc_val: Optional[BaseException],
> +                 exc_tb: Optional[TracebackType]) -> None:
>          # Implement context manager exit function.
>          self.close()
> -        return False
>  
>      def connect(self, negotiate=True):
>          """
> 

Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>


Re: [PATCH RFC 13/32] python/qemu/lib: Adjust traceback typing
Posted by Philippe Mathieu-Daudé 5 years, 5 months ago
On 5/14/20 7:53 AM, John Snow wrote:
> mypy considers it incorrect to use `bool` to statically return false,
> because it will assume that it could conceivably return True, and gives
> different analysis in that case. Use a None return to achieve the same
> effect, but make mypy happy.
> 
> Note: Pylint considers function signatures as code that might trip the
> duplicate-code checker. I'd rather not disable this as it does not
> trigger often in practice, so I'm disabling it as a one-off and filed a
> change request; see https://github.com/PyCQA/pylint/issues/3619
> 
> Signed-off-by: John Snow <jsnow@redhat.com>
> ---
>  python/qemu/lib/machine.py |  8 ++++++--
>  python/qemu/lib/qmp.py     | 10 ++++++++--
>  2 files changed, 14 insertions(+), 4 deletions(-)

Thanks, applied to my python-next tree:
https://gitlab.com/philmd/qemu/commits/python-next