Skip to content

Commit

Permalink
exekall: Fix handling of parametrized classes
Browse files Browse the repository at this point in the history
FIX

Some classes like dict can now be parametrized, e.g. dict[int, str].
They do not behave exactly like their deprecated typing counterpart
(e.g. typing.Dict), so ensure they are handled consistently by exekall.
  • Loading branch information
douglas-raillard-arm committed Jan 22, 2024
1 parent 7996874 commit 78c7e98
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions tools/exekall/exekall/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -2465,17 +2465,28 @@ def check_prototype(prototype):
# make sure we got some usable type annotations that only consist
# in classes, rather than things coming from the typing module
param_map, value_type = prototype

def is_hint(annot):
# Prevent things like typing.Dict[], since it will raise
# exceptions when being used along issubclass()
#
# Note: We cannot use isinstance/issubclass as this would
# result in: TypeError: Class typing.Generic cannot be used
# with class or instance checks
return (
typing.Generic in inspect.getmro(annot) or
# Normal classes like dict can now be parametrized, e.g.
# dict[int, str]. get_origin(dict) == None but
# get_origin(dict[str, int]) == dict
typing.get_origin(annot) is not None
)

if not all(
(hasattr(typing, 'Self') and annot == typing.Self) or
isinstance(annot, type) and
(
isinstance(annot, typing.TypeVar) or
# Prevent things like typing.Dict[], since it will raise
# exceptions when being used along issubclass()
#
# Note: We cannot use isinstance/issubclass as this would result in:
# TypeError: Class typing.Generic cannot be used with class or instance checks
not typing.Generic in inspect.getmro(annot)
not is_hint(annot)
)
for annot in {value_type, *param_map.values()}
):
Expand Down

0 comments on commit 78c7e98

Please sign in to comment.