Adam Williamson 8e0f17
From 38b9f3206d86cded04c52e52b0d627079ba44acc Mon Sep 17 00:00:00 2001
Adam Williamson 8e0f17
From: Adam Williamson <awilliam@redhat.com>
Adam Williamson 8e0f17
Date: Fri, 6 Jul 2018 15:04:39 -0700
Adam Williamson 8e0f17
Subject: [PATCH] Make pyanaconda.dbus.typing work with Python 3.7 (#1598574)
Adam Williamson 8e0f17
MIME-Version: 1.0
Adam Williamson 8e0f17
Content-Type: text/plain; charset=UTF-8
Adam Williamson 8e0f17
Content-Transfer-Encoding: 8bit
Adam Williamson 8e0f17
Adam Williamson 8e0f17
As reported in RHBZ#1598574, the internals of typing changed in
Adam Williamson 8e0f17
Python 3.7 such that it's no longer so simple to find the 'base'
Adam Williamson 8e0f17
type of a type hint (it's not just its `__origin__` any more).
Adam Williamson 8e0f17
There doesn't appear to be any particularly great fix for this,
Adam Williamson 8e0f17
but this suggestion from Miro HronĨok seems as good as any other
Adam Williamson 8e0f17
option we have for now. This should work with both 3.6 and 3.7.
Adam Williamson 8e0f17
Adam Williamson 8e0f17
Signed-off-by: Adam Williamson <awilliam@redhat.com>
Adam Williamson 8e0f17
---
Adam Williamson 8e0f17
 pyanaconda/dbus/typing.py | 18 ++++++++++++------
Adam Williamson 8e0f17
 1 file changed, 12 insertions(+), 6 deletions(-)
Adam Williamson 8e0f17
Adam Williamson 8e0f17
diff --git a/pyanaconda/dbus/typing.py b/pyanaconda/dbus/typing.py
Adam Williamson 8e0f17
index ea8a2999c..1cb1ea99a 100644
Adam Williamson 8e0f17
--- a/pyanaconda/dbus/typing.py
Adam Williamson 8e0f17
+++ b/pyanaconda/dbus/typing.py
Adam Williamson 8e0f17
@@ -149,24 +149,30 @@ class DBusType(object):
Adam Williamson 8e0f17
     @staticmethod
Adam Williamson 8e0f17
     def _is_container_type(type_hint):
Adam Williamson 8e0f17
         """Is it a container type?"""
Adam Williamson 8e0f17
-        # Try to get the "base" type of the container type.
Adam Williamson 8e0f17
+        # Try to get the "origin" of the hint.
Adam Williamson 8e0f17
         origin = getattr(type_hint, "__origin__", None)
Adam Williamson 8e0f17
-        return origin in DBusType._container_type_mapping
Adam Williamson 8e0f17
+        if origin:
Adam Williamson 8e0f17
+            # Return true if the "origin" is a subclass of a container type
Adam Williamson 8e0f17
+            # see https://bugzilla.redhat.com/show_bug.cgi?id=1598574
Adam Williamson 8e0f17
+            return any(issubclass(origin, contype) for contype in DBusType._container_type_mapping)
Adam Williamson 8e0f17
+        return False
Adam Williamson 8e0f17
 
Adam Williamson 8e0f17
     @staticmethod
Adam Williamson 8e0f17
     def _get_container_type(type_hint):
Adam Williamson 8e0f17
         """Return a container type."""
Adam Williamson 8e0f17
-        # Get the "base" type of the container.
Adam Williamson 8e0f17
-        origin = type_hint.__origin__
Adam Williamson 8e0f17
+        # Get the "base" type via the "origin" of the hint
Adam Williamson 8e0f17
+        # see https://bugzilla.redhat.com/show_bug.cgi?id=1598574
Adam Williamson 8e0f17
+        basetype = tuple(contype for contype in DBusType._container_type_mapping
Adam Williamson 8e0f17
+                       if issubclass(type_hint.__origin__, contype))[0]
Adam Williamson 8e0f17
         # Get the arguments of the container.
Adam Williamson 8e0f17
         args = type_hint.__args__
Adam Williamson 8e0f17
 
Adam Williamson 8e0f17
         # Check the typing.
Adam Williamson 8e0f17
-        if origin == Dict:
Adam Williamson 8e0f17
+        if basetype == Dict:
Adam Williamson 8e0f17
             DBusType._check_if_valid_dictionary(type_hint)
Adam Williamson 8e0f17
 
Adam Williamson 8e0f17
         # Generate string.
Adam Williamson 8e0f17
-        container = DBusType._container_type_mapping[origin]
Adam Williamson 8e0f17
+        container = DBusType._container_type_mapping[basetype]
Adam Williamson 8e0f17
         items = [DBusType.get_dbus_representation(arg) for arg in args]
Adam Williamson 8e0f17
         return container % "".join(items)
Adam Williamson 8e0f17
 
Adam Williamson 8e0f17
-- 
Adam Williamson 8e0f17
2.18.0.rc2
Adam Williamson 8e0f17